【发布时间】:2017-07-05 07:31:42
【问题描述】:
当我尝试在我的 Android 应用程序中读取一些文件时,它们似乎不存在,这是我的代码:
public class ReadFiles1 {
//Static Scanner and File Objects
static Scanner s;
// Static method that returns an ArrayList
static ArrayList<String> words (String filename){
//Instantiate File with file name within parameters
File n = new File(filename);
//Instantiate Scanner s with f variable within parameters
//surround with try and catch to see whether the file was read or not
try {
s = new Scanner(n);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("Problem here");
}
//Instantiate a new ArrayList of String type
ArrayList<String> theWord = new ArrayList <String>();
//while it has next ..
while(s.hasNext()){
//Initialise str with word read
String str=s.next();
//add to ArrayList
theWord.add(str);
}
//return ArrayList
return theWord;
}
不知道是什么问题,我把txt文件和.java文件放在同一个包里。 这是我在运行时遇到的错误: 检查此链接的图片(https://ibb.co/cn3QCv)
W/System.err: java.io.FileNotFoundException: build/numbers.txt: open failed: ENOENT (No such file or directory)
【问题讨论】:
-
在 Eclipse 中运行良好
-
请检查Android Mainfest文件是否有权限,如果没有添加权限
-
检查文件的完整路径,我认为这是唯一的问题..它只是意味着没有这样的文件。
-
@ELITE 我确实写了完整的路径,但是没有用。
-
显示完整路径并添加您如何调用
words方法..
标签: android file android-studio