我在打开文件时遇到了一些麻烦。好吧,我已经使用绝对路径知道文件的位置,但它仍然无法打开文件(文件未找到)
public void ReadFromFile() throws FileNotFoundException
{
/** Read the contents of the given file. */
String SourceID = new String();
String LogicalID = new String();
File fileDir = getFilesDir();
String s = new String();
s+=fileDir.getAbsolutePath()+"/Nodes.txt";
Scanner scanner = new Scanner(new FileInputStream(s));
try
{
while (scanner.hasNextLine())
{
SourceID = scanner.nextLine();
LogicalID = scanner.nextLine();
String ss = new String();
ss+=" ----------------> "+SourceID+" "+LogicalID+" ";
Log.v(TAG, ss);
ListaNodesSTART.add(new NodesToStart(SourceID,LogicalID));
}
}catch(Exception ee){//Log.v(TAG, "Could not read the file");
ERROR.setText("Could Not Read file Nodes.txt");
ErRorLog.setText("Could Not Read file Nodes.txt");}
finally{scanner.close(); }
}
我想问题是该设备没有该文件,但是,如何在应用启动时上传它?
提前致谢
答案 0 :(得分:0)
正如您所提到的,设备中没有文件!要使用静态文件,请将它们插入assets文件夹,然后:
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.d("tag", e.getMessage());
}
for(String filename : files) {
if( filename.equals("Nodes.txt") {
InputStream in = null;
try {
// Do your work with file
in = assetManager.open(filename);
// ...
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
}
}