简体   繁体   English

如何使用命令行 arguments 作为 Java 中的 Scanner 参数

[英]How to use command line arguments as Scanner parameter in Java

I am writing a program that requires that I use java command line arguments to open a file and find some information within that file.我正在编写一个程序,要求我使用 java 命令行 arguments 打开文件并在该文件中查找一些信息。 The files we want to access already exist in my projects file directory.我们要访问的文件已经存在于我的项目文件目录中。 The way I am trying to access them is by using the Scanner and just passing args[0] which should be the name of the file that we are trying to access.我尝试访问它们的方式是使用Scanner并传递args[0] ,它应该是我们尝试访问的文件的名称。

Below is the code that I have come up with so far.以下是我到目前为止提出的代码。 Right now my code will always throw现在我的代码总是会抛出

java.io.FileNotFoundException: {The Files Name} (The system cannot find the file specified) java.io.FileNotFoundException: {The Files Name} (系统找不到指定的文件)

public static void main(String[] args) {
    try {
        File inputedFile = new File(args[0]);
        Scanner fileReader = new Scanner(inputedFile);
        while (fileReader.hasNextLine()) {
            String fileData = fileReader.nextLine();
            System.out.println(fileData);
        }
        fileReader.close();
    } catch (FileNotFoundException e) {
        System.out.println("An error occurred when trying to open the file.");
        e.printStackTrace();
    }
}

I believe the reason I am getting this exception is because Java is not able to find the file off of just the file's name.我相信我收到此异常的原因是因为 Java 无法仅从文件名中找到文件。 Java needs the location of the file on my system for it to work so for example: Java 需要文件在我的系统上的位置才能正常工作,例如:

D:\coding-stuff\Java\CS-2420\src\comprehensive\super_simple.txt

would work while会工作,而

super_simple.txt

would not.不会。

This is an issue for me because I need this code to be run on multiple machines not just my own.这对我来说是个问题,因为我需要这段代码在多台机器上运行,而不仅仅是我自己的。 So my question is how can I create a "relative link" to the file within my file structure?所以我的问题是如何在我的文件结构中创建到文件的“相对链接”?

Either you know the location of the file or you can obtain the location of the file or you can search for the file.您可以知道文件的位置,也可以获取文件的位置,也可以搜索文件。

The System properties contain the path to the working directory, the user's home directory and the temporary directory, so you could place your file in one of those three. 系统属性包含工作目录的路径、用户的主目录和临时目录,因此您可以将文件放在这三个目录之一中。 Alternatively, you can use the -D option of the java command to define your own [System] property.或者,您可以使用java命令的-D选项来定义您自己的 [System] 属性。

You could also define an environment variable and obtain its value via method getenv .您还可以定义一个环境变量并通过方法getenv获取它的值。

Another option would be to make the file a resource .另一种选择是使该文件成为资源

If you don't know or can't obtain the path to the file, then you can search for it, either using the Java API or, via class ProcessBuilder , execute an operating system command to search for the file.如果您不知道或无法获取文件的路径,则可以使用 Java API 或通过 class ProcessBuilder执行操作系统命令来 搜索文件。 [Note that the System properties can also tell you the host operating system.] [请注意, System属性还可以告诉您主机操作系统。]

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

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