Jump to content

Help with Command Line inputs

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Sh4d0ws

Sh4d0ws

    Newbie

  • Members
  • PipPip
  • 13 posts
Hello,

I'm currently working on an assignment and I am having a lot of trouble with figuring out how to work this. I don't know how to read the files from the command prompt like this question is asking for. Here is the question, if you can give me any suggestions or snippets of source code it would be very helpful. Or even just point me in the right direction. Thanks.

"" said:

Develop an application in Java called myCat.java for displaying/concatenating text files. When you run this application with a command line parameter(s) specifying the name(s) of a file(s)

java myCat file_name

java myCat file_name1 file_name2

it should print to the terminal the contents of the text file(s) file_name in a way similar to invoking the Unix utility

cat file_name

If the user does not specify any file name or asks for a non exiting file, your myCat application should display a descriptive message reminding the user to fix the command line parameter, and then myCat should close (Hint: try this with cat)

Test your program on the source code of your solution to this assignment itself, i.e. on the file myCat.java, and then on three other files at the same.

(Hint: Read the description of the cat utility http://en.wikipedia.org/wiki/Cat_(Unix) . On a Linux workstation view the manual page for the cat utility (type man cat in the terminal window.)


Submit (to the assignment bin, printed copy) the source code and the output with the response to java myCat file1 file2 file3 for the case
(i) when all three files exist, and for the case when
(ii)one of them does not exist.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What do you have so far?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Sh4d0ws

Sh4d0ws

    Newbie

  • Members
  • PipPip
  • 13 posts
Never mind, I have completed it. It's for an assignment, and the due-date is past, so I see no harm in posting it.

import java.io.*;
public class CatFile {
	public static void main(String arg[]) {
		try {
			printToFile(arg[0], arg[1], arg[2]);
			System.out.println("The files " + arg[0] + " and " + arg[1]
			+ " have been copied to " + arg[2] + ".");
		} catch (Exception e){
		System.out.println("ERROR: You input " + e.getMessage() + " parameter(s).");
		System.out.println("Please try again!");
		}// end of catch
	} // end of main() method

/**
 * This is a method designed to take input from two
 * files and save it to a third method.
 * @param input first input file
 * @param inputTwo second input file
 * @param inputThree the destination file
 */
	public static void printToFile(String input, String inputTwo, String inputThree){
		try{
			String line;
			BufferedReader br = new BufferedReader(new FileReader(input));
			BufferedReader br2 = new BufferedReader(new FileReader(inputTwo));
			FileOutputStream out = new FileOutputStream(inputThree);
			line = br.readLine();
			while (line != null){
				System.out.println(line);
				line = br.readLine();
			}//while
			line = br2.readLine();
			while (line != null){
				System.out.println(line);
				line = br.readLine();
			}//while
			br.close();
			br2.close();
			out.close();
		} catch (IOException e){
			System.out.println("ERROR:  " + e.getMessage());
			System.exit(0) ;
		}//end of catch
	}//end of printToFile() method
}//end of CatFile class