我在android上编写了这段代码来了解.txt文件是否存在。如何知道文件是否存在?
File file_a =new File("a.txt");
InputStream in3 = getResources().openRawResource(R.raw.b);
FileOutputStream out3 = null;
try { out3=openFileOutput("a.txt",Context.MODE_WORLD_WRITEABLE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] buff3 = new byte[1024];
int read3 = 0;
try {
while ((read3 = in3.read(buff3)) > 0) {
out3.write(buff3, 0, read3);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
in3.close();
out3.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
boolean a=file_a.exists();
它总是返回false
。
我该如何解决这个问题?
您是否有权限读取存储文件的位置? –
我怀疑你是被允许在你的android设备的根目录上写的。因此返回的值是正确的。 – WarrenFaith
我不明白?文件存在。我读了它的值。但布尔a是错误的?那个怎么样?文件存在 –