FileReader找不到我的文本文件 [英] The FileReader cannot find my text file

查看:437
本文介绍了FileReader找不到我的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的文本文件,由于某种原因,它无法找到。我没有看到代码有什么问题,因为我是从网站上得到的,我开始认为我没有将文本文件放在正确的位置。有什么建议吗?

I have a simple text file and for some reason, it cannot be found. I don't see anything wrong with the code because I got it from a site, and I am starting to think that I didn't place the text file in the right place. Any suggestion please?

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MainFavorites {
    public static void main(String[] args) throws IOException {
        /**
        * finds pathway to the file
        */
        //      File file = new File("icecreamTopping.txt");
        //      System.out.println(file.getCanonicalPath());

        BufferedReader reader = null;
        ArrayList <String> myFileLines = new ArrayList <String>();
        try {
            String sCurrentLine;
            reader = new BufferedReader(new FileReader("icecreamTopping.txt"));
            while ((sCurrentLine = reader.readLine()) != null) {
                //System.out.println(sCurrentLine);
                myFileLines.add(sCurrentLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.print(e.getMessage());
        } finally {
            try {
                if (reader != null)reader.close();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
                ex.printStackTrace();
            }
        }
        int numElements = myFileLines.size();
        System.out.println ("there are n lines in the file:" + numElements);

        for (int counter = numElements-1; counter >= 0; counter--) {
            String mylineout = myFileLines.get(counter);
            System.out.println (mylineout);
        }
    }
}



文件内容:



File content:

1- Blueberry 
2- Banana Buzz
3- Cookie Batter

我的堆栈跟踪是这样的:

My stack trace is this:

java.io.FileNotFoundException: C:\Users\homar_000\workspace\RankFavorites\icecreamTopping.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at MainFavorites.main(MainFavorites.java:28)


推荐答案

发现问题所在。我没有必要放置文件扩展名,所以我删除了.txt,因为当我保留它时,它将其读作icecreamTopping.txt.txt

Found out what was the problem. It was unnecessary for me to put the file extension so I removed the .txt because when I kept it, it read it as "icecreamTopping.txt.txt"

这篇关于FileReader找不到我的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆