简体   繁体   English

通过命令行在Java中传递输入文件

[英]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 为了使这项工作有效,我在Java代码和命令行(Windows)中修改了哪些更改

You use the String[] args in your main method, 您在主要方法中使用String[] args

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". 使用上面的代码,第一个命令将使用“ MY_FILE”,第二个命令将使用默认的“ textfile.txt”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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