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

时间:2017-09-29 14:29:08

标签: java input buffered

这是我在尝试解析传入文件时遇到的错误:

  

解析文件时出错。       java.io.FileNotFoundException:input.txt(系统无法找到该文件       指定)       at java.io.FileInputStream.open0(Native Method)       在java.io.FileInputStream.open(未知来源)       在java.io.FileInputStream。(未知来源)       在java.io.FileInputStream。(未知来源)       在java.io.FileReader。(未知来源)       在MovieDatabaseManager.parseInputFile(MovieDatabaseManager.java:47)       在MovieDatabaseManager。(MovieDatabaseManager.java:32)       在MovieDatabaseManager.main(MovieDatabaseManager.java:206)

以下是代码:

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

1 个答案:

答案 0 :(得分:-1)

您需要使用资源的完整限定路径名来获取它。

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

否则,相对路径可用于执行的jar文件的同一层次结构中的文件。