-1
 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

i.e. 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

user3952416
  • 109
  • 1
  • 2
  • 8
  • 2
    http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html – pratZ Aug 19 '14 at 19:28
  • possible duplicate of [Running Command Line in Java](http://stackoverflow.com/questions/8496494/running-command-line-in-java) – mpromonet Aug 19 '14 at 19:44

1 Answers1

2

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".

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249