Java 将文件作为命令行参数传递并读取其行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4302567/
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-08-14 15:37:52  来源:igfitidea点击:

Passing a file as a command line argument and reading its lines

javafilecommand-line

提问by user472221

this is the code that i have found in the internet for reading the lines of a file and also I use eclipse and I passed the name of files as SanShin.txt in its argument field. but it will print :

这是我在互联网上找到的用于读取文件行的​​代码,我还使用了 eclipse 并将文件名作为 SanShin.txt 在其参数字段中传递。但它会打印:

Error: textfile.txt (The system cannot find the file specified)

Code:

代码:

public class Zip {
    public static void main(String[] args){
        try{
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream("textfile.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
              // Print the content on the console
              System.out.println (strLine);
            }
            //Close the input stream
            in.close();
            }catch (Exception e){//Catch exception if any
              System.err.println("Error: " + e.getMessage());
            }


    }
}

please help me why it prints this error. thanks

请帮助我为什么它会打印此错误。谢谢

采纳答案by khachik

...
// command line parameter
if(argv.length != 1) {
  System.err.println("Invalid command line, exactly one argument required");
  System.exit(1);
}

try {
  FileInputStream fstream = new FileInputStream(argv[0]);
} catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

// Get the object of DataInputStream
...

> java -cp ... Zip \path\to\test.file

回答by T.J. Crowder

Your new FileInputStream("textfile.txt")is correct. If it's throwing that exception, there is no textfile.txtin the current directory when you run the program. Are you sure the file's name isn't actually testfile.txt(note the s, not x, in the third position).

new FileInputStream("textfile.txt")是对的。如果它抛出该异常,则textfile.txt在运行程序时当前目录中没有。您确定该文件的名称实际上不是testfile.txt(注意第三个位置的s,不是x)。



Off-topic: But your earlier deleted question asked how to read a file line by line (I didn't think you needed to delete it, FWIW). On the assumption you're still a beginner and getting the hang of things, a pointer: You probably don'twant to be using FileInputStream, which is for binary files, but instead use the Readerset of interfaces/classes in java.io(including FileReader). Also, whenever possible, declare your variables using the interface, even when initializing them to a specific class, so for instance, Reader r = new FileReader("textfile.txt")(rather than FileReader r = ...).

题外话:但是您之前删除的问题询问了如何逐行读取文件(我认为您不需要删除它,FWIW)。在假设你还是一个初学者,得到的东西挂起,指针:你可能希望被使用FileInputStream,这是二进制文件,而是使用Reader在集接口/类java.io(含FileReader)。此外,尽可能使用接口声明变量,即使将它们初始化为特定类,例如,Reader r = new FileReader("textfile.txt")(而不是FileReader r = ...)。

回答by daveb

When you just specify "textfile.txt"the operating system will look in the program's working directory for that file.

当您仅指定"textfile.txt"操作系统时,将在程序的工作目录中查找该文件。

You can specify the absolute path to the file with something like new FileInputStream("C:\\full\\path\\to\\file.txt")

您可以使用以下内容指定文件的绝对路径 new FileInputStream("C:\\full\\path\\to\\file.txt")

Also if you want to know the directory your program is running in, try this: System.out.println(new File(".").getAbsolutePath())

另外,如果您想知道程序运行所在的目录,请尝试以下操作: System.out.println(new File(".").getAbsolutePath())