简体   繁体   English

尝试使用缓冲读取器在 java 中输入文件。 获取 FileNotFoundError

[英]Trying to input a file in java using a buffered-reader. Getting a FileNotFoundError

This is the error I experience when trying to parse a incoming file:这是我在尝试解析传入文件时遇到的错误:

Error in Parsing file.解析文件时出错。 java.io.FileNotFoundException: input.txt (The system cannot find the file specified) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(Unknown Source) at java.io.FileInputStream.(Unknown Source) at java.io.FileInputStream.(Unknown Source) at java.io.FileReader.(Unknown Source) at MovieDatabaseManager.parseInputFile(MovieDatabaseManager.java:47) at MovieDatabaseManager.(MovieDatabaseManager.java:32) at MovieDatabaseManager.main(MovieDatabaseManager.java:206) java.io.FileNotFoundException: input.txt (系统找不到指定的文件) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(Unknown Source) at java.io.FileInputStream.( Unknown Source) at java.io.FileInputStream.(Unknown Source) at java.io.FileReader.(Unknown Source) at MovieDatabaseManager.parseInputFile(MovieDatabaseManager.java:47) at MovieDatabaseManager.(MovieDatabaseManager.java:32) at MovieDatabaseManager.main (电影数据库管理器.java:206)

Here is the code:这是代码:

public MovieDatabaseManager(String file)
{
    this();
    parseInputFile(file); //populates database with list of movies
}

/**
 * Parses the input file so that you can add all of items found in the list 
in alphabetical order by title.
 */
private void parseInputFile(String file)
{
    //Create a file input stream
    Movie m;
    String instr;

    try 
    {
        //Create input reader
        BufferedReader in = new BufferedReader(new FileReader(file));
        while (in.ready())
        {
            instr = in.readLine();

            //Try to parse the movie using the appropriate movie 
            //constructor.  If it fails, an exception is caught
            try 
            {
                m = new Movie(instr);

                ////////////////////////////////////////////////////
                //Add code to insert m here into your list
                ////////////////////////////////////////////////////

            } 
            catch (InvalidMovieException e) 
            {
                System.out.println("Invalid movie string " + instr + " in 
`enter code here`file " + file);
            }

        }
    } 
    catch (IOException io) 
    {
        System.err.println("Error in Parsing file.");
        io.printStackTrace();   
    }
    }

You need to use the full qualified path name of your resource to get it.您需要使用资源的全限定路径名来获取它。

Ex: C:/dur/dir/file.txt例如:C:/dur/dir/file.txt

Else, relative path can be used for files in the same hierarchy of the jar file executed.否则,相对路径可用于执行的 jar 文件的同一层次结构中的文件。

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

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