我正在尝试将.txt文件保存在Android Studio的res\raw文件中并读取/解析该文件.我在"res"目录中创建了一个名为"raw"的文件夹中的文件"my_file.txt"(我没有创建).
以下是我认为我的主要问题:创建File对象(与Scanner对象一起使用)时,我应该为我的文本文件传入什么路径?
这是我的代码:
private void readFile(){
Scanner scanner = null;
try {
scanner = new Scanner(new File("\\res\\raw\\my_file.txt")); //I've also tried without the .txt extension
Log.v("readFile->try","Trying to read file.");
}catch(Exception e)
{
//Send error message.
}
if(scanner != null) {
while (scanner.hasNext()) {
String[] line = scanner.nextLine().split("\t");
//Process string here
}
}
}
Run Code Online (Sandbox Code Playgroud)