我有一个简单的文本文件,由于某种原因,它无法找到。我没有看到代码有什么问题,因为我是从网站上得到的,我开始认为我没有将文本文件放在正确的位置。有什么建议吗?
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);
}
}
}
1- Blueberry
2- Banana Buzz
3- Cookie Batter
我的堆栈跟踪是这样的:
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)
答案 0 :(得分:8)
替换下面的行
reader = new BufferedReader(new FileReader("icecreamTopping.txt"));
带
reader = new BufferedReader(new FileReader("resources/icecreamTopping.txt"));
并将该文件放在与src文件夹并行的资源文件夹下。
示例代码:
从资源文件夹
中读取文件abc.txt
reader = new BufferedReader(new FileReader("resources/abc.txt"));
这是项目结构
尝试以下代码,找出它指向文件icecreamTopping.txt
的位置。
File f=new File("icecreamTopping.txt");
System.out.println(f.getAbsolutePath());
获取绝对路径后,只需将文件放在那里。
- 编辑 -
根据您上次的评论,将icecreamTopping.txt
文件直接放在项目RankFavorites
中,如下面的快照所示,它肯定会解决您的问题。
答案 1 :(得分:4)
发现问题所在。我没有必要放置文件扩展名,所以我删除了.txt,因为当我保留它时,它将其读作&#34; icecreamTopping.txt.txt&#34;
答案 2 :(得分:-1)
尝试使用File类检测存储中的文件:
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
答案 3 :(得分:-2)
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
JSONParser parser = new JSONParser();
ObjectMapper mapper = new ObjectMapper();
try {
Object obj = parser.parse(new FileReader("/home/sahan/Desktop/data.json"));
ObjectNode objNode = mapper.convertValue(obj, ObjectNode.class);
System.out.println(objNode);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}