0

So for an project, we need to have the program accept the file name to be read in from command line arguments of main method. That is, your program should be executable from the command line by calling:

~>java Project inputFile.txt

Which will output the modified contents of the file to the standard output.

But I am clueless on how to do this.

PS: We've covered how to use the command line arguments but not how to have a file read from this location. Any suggestions?

2
  • 2
    you don't read a file from the arguments, just it's name and/or path. that, you read as a String, then, in your code: File f = new File(args[0]); Commented Sep 10, 2015 at 7:03
  • Duplicated question doesn't answers this question. Commented Jul 26, 2018 at 15:39

4 Answers 4

2

Say, you invoke your program as

java MyMainClass /path/to/file

Then in the main() method just use

File f = new File(args[0])

Then, you might want to validate that

f.exists()
f.isFile()
f.canRead()

etc.

To actually read the file you can follow those instructions as suggested in the comment by @Kevin Esche

Sign up to request clarification or add additional context in comments.

2 Comments

Note that OP actually not asking about what is asked in the title :)
Indeed :) For completeness, I referenced comment by @Kevin Esche linking to the actual reading of the file.
0
java MainClass <list of arguments>

Now in main class you will receive all the argument in String array, which is passed in the main method.

public static void main(String args[]) {
   for(String argument : args) {
     System.out.println(argument);
   }
}

2 Comments

as in the other answers: So far, we've covered how to use the command line arguments but not how to have a file read from this location. Any suggestions?
I would suggest you revise those class notes. Because all the answer saying what you already know. Please don't take it otherwise. Just a polite suggestion to you.
0

When running a java program from the console you call:

java programName list of inputs separated by space

So in your case you have:

java Project inputFile.txt

When the JVM starts and calls main() it will take everything after your project name and create a String array of that separated by the spaces.

So in my first caommand line I would get:

{"list" + "of" + "inputs" + "separated" + "by" + "space"}

This string array will come into your program in the main function:

public static void main(String[] args) { ... }

Therefore, in your case, args[0] will be the file name you are looking for. You can then create a file reader of sorts. If you don't add any path to the front of the file name it will look for the file in the folder that your src folder is in.

Hope this helps.

Comments

0

Maybe this would help, it reads and prints the file to the console.

public static void main(String[] args) {
    File f=  new File(arg[0]);

    InputStream is = new FileInputStream(f);

    int byteReaded;

    while ((byteReaded = is.read())!=-1) {
       System.out.print((char)byteReaded);

    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.