【发布时间】:2015-10-21 23:03:14
【问题描述】:
这里我如何将字节写入文件。我正在使用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();
}
【问题讨论】:
-
您可能正在崩溃,因为
new FileOutputStream("myFile.xml")在 Android 上没有任何意义。如果您希望将myFile.xml存储在internal storage 中,请使用类似:new FileOutputStream(new File(getActivity().getFilesDir(), "myFile.xml"))。如果您希望将myFile.xml存储在external storage 中,请使用类似:new FileOutputStream(new File(getActivity().getExternalFilesDir(null), "myFile.xml"))。