java 如何输入在命令行参数中读取的文件名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32495236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:17:14  来源:igfitidea点击:

How do I input a file name that is read in command line arguments?

javatext-filescommand-line-arguments

提问by Ryan Dorman

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:

因此,对于一个项目,我们需要让程序接受要从 main 方法的命令行参数中读取的文件名。也就是说,您的程序应该可以通过调用以下命令从命令行执行:

~>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?

PS:我们已经介绍了如何使用命令行参数,但没有介绍如何从此位置读取文件。有什么建议?

回答by Adam Michalik

Say, you invoke your program as

说,你调用你的程序

java MyMainClass /path/to/file

Then in the main()method just use

然后在main()方法中只需使用

File f = new File(args[0])

Then, you might want to validate that

然后,您可能想要验证

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

etc.

等等。

To actually readthe file you can follow those instructionsas suggested in the comment by @Kevin Esche

要实际读取的文件,你可以按照这些指示为在@Kevin Esche的意见建议

回答by YoungHobbit

java MainClass <list of arguments>

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

现在在主类中,您将收到String数组中的所有参数,这些参数在main 方法中传递。

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

回答by Krzysztof Cichocki

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);

    }
}

回答by Nicholas Robinson

When running a java program from the console you call:

从控制台运行 java 程序时,您调用:

java programName list of inputs separated by space

java programName list of inputs separated by space

So in your case you have:

所以在你的情况下,你有:

java Project inputFile.txt

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.

当 JVM 启动并调用时main(),它将获取项目名称之后的所有内容,并创建一个由空格分隔的字符串数组。

So in my first caommand line I would get:

所以在我的第一个 caommand 行中,我会得到:

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

因此,在您的情况下,args[0]将是您要查找的文件名。然后,您可以创建各种文件阅读器。如果您没有在文件名前面添加任何路径,它将在您的 src 文件夹所在的文件夹中查找该文件。

Hope this helps.

希望这可以帮助。