[英]Android where file is saved FileOutputStream
我在这里如何将字节写入文件。 我正在使用FileOutputStream
private final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
FragmentActivity activity = getActivity();
byte[] readBuffer = (byte[]) msg.obj;
FileOutputStream out = null;
try {
out = new FileOutputStream("myFile.xml");
out.write(readBuffer);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
现在我想打开那个文件,所以我需要有该文件的路径。 那么我需要打开那个文件呢?
编辑:
我在这里如何从文件中读取,但我看不到任何东西......
BufferedReader reader = null;
FileInputStream s = null;
try {
s = new FileInputStream("mano.xml");
reader = new BufferedReader(new InputStreamReader(s));
String line = reader.readLine();
Log.d(getTag(), line);
while (line != null) {
Log.d(getTag(), line);
line = reader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我建议用它来写:
OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/yourfilename");
所以要阅读位置:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+transaction.getUniqueId()+".pdf");
阅读路径:
file.getAbsolutePath();
如何在开发人员指南中报告,您必须指定要保存文件的位置。 你可以选择:
将文件保存在内部存储中:
String filename = "myfile"; String string = "Hello world!"; FileOutputStream outputStream; try { outputStream = openFileOutput(filename, Context.MODE_PRIVATE); outputStream.write(string.getBytes()); outputStream.close(); } catch (Exception e) { e.printStackTrace(); }
或者在第二个实例中,您可以将文件保存在外部存储中:
// Checks if external storage is available to at least read public boolean isExternalStorageReadable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { return true; } return false; }
只记得设置权限!!!!
这里有整个文档: 文档
您的文件保存在路径/Data/Data/Your package Name/files/myFile.xml
您可以使用/Data/Data/Your package Name/files/myFile.xml
this.getFileDir()
方法获取Application上文件夹的路径。
所以使用this.getFileDir() + "myFile.xml"
来读取文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.