简体   繁体   中英

passing input file in java through command line

 FileInputStream fstream = new FileInputStream("textfile.txt");

This is above code to get the input file , i want a input file to give from command line

ie pseudo command line code

java  filename giveinputfile("textfile.txt")

What change i modify in my java code and command line(windows) to make this work

You use the String[] args in your main method,

public static void main(String[] args) {
  String fileName = "textfile.txt";
  if (args.length > 0) { 
    fileName = args[0];
  }
  System.out.println("fileName: " + fileName);
}

Then you run your program with

java myProgram MY_FILE

or

java myProgram

With the code above the first command would use "MY_FILE" and the second would use the default "textfile.txt".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM