0

I have a .txt file assume it name My. I need to read this file content.I pass the file through command Line argument. What should I do in the Command prompt to pass the file to args[] array. Please help..............

  • 2
    Does this help? http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html – e.dan Jun 23 '14 at 08:54

3 Answers3

3

You can get the file name with command line arguments:

 java -jar myapp.jar abc.txt

or

 java a.b.c.MyApplication abc.txt

will place abc.txt into args[0] of

 public static void main(String[] args)

Then you can open the File and read it from Java code.


If you want the file contents (and not write Java code to read them), you could get them via System.in:

 java -jar myapp.jar < abc.txt

(the above depends a bit on what kind of shell and OS you are using)

 InputStream data = System.in;
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • I have tried java a.b.c Myapplication abc.txt ultimatly it shows the error msg:Error could not find or load main class Thanks!!!!!!!!! – user2791673 Jun 23 '14 at 09:58
  • what you typed in the command line it should be 'java YourMainClassName pathOfFile' – SparkOn Jun 23 '14 at 10:39
0

Sometimes it's difficult to know where to start, but this question in its parts has been answered many times. However, perhaps to help you, it's best to point you in the right direction.

The steps you want are:

1) Get the String which represents the files path from the args array, perhaps something like:

String filePath = args[0];

However, you will want to also check that there is an item in the args to prevent the possibility of getting an exception when reading from the args (if nothing was passed from the command line) e.g:

String filePath;

if(args.length > 0){
    filePath = args[0];
}else{
 // do something  to handle the error, probably exit the program?
}

For any more details on this, see here: Link to java tutorial

2) Then, you want to make a File object from that string using the File constructor. Link to javadoc

File inputFile = new File(filePath);

3) Then you need to read that file, line by line. For that I will point you to another SO answer, as there are many ways to do this: Reading from a file - SO question

Hope this helps.

Community
  • 1
  • 1
ThePerson
  • 3,048
  • 8
  • 43
  • 69
0

this is the code you probably want

    String path =arr[0];  //first approach
  /*Scanner in = new Scanner(System.in);
    System.out.println("Enter the path of the file..."); // whole block command second approac
    String path = in.nextLine(); */
    File file = new File(path);
  //apply your logic i just did some reading and writing to a file
  /*BufferedReader inputStream = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    PrintStream out = new PrintStream(new FileOutputStream("a.txt"));
    while(true)
    {
        String line = inputStream.readLine();
        if(line == null)
            break;
        else
            out.println(line);

    }
    */

if you use the first approach run the program as

java className path_of_file

In second approach it will ask you for file path at runtime then you can enter the path of the file. just run like java className

SparkOn
  • 8,806
  • 4
  • 29
  • 34