在按下按钮时的其中一个活动中我想在extran存储上创建一个文件。所以我写了 以下代码是这样做的。
但最后创建了文件,但是空了......为什么?
码:
public void tx(byte[] data) {
Log.w(TAG, CSubTag.bullet("tx"));
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
OutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
try {
bos.write("data_stream".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
请关闭finally块内的OutputStream和BufferedOutputStream。否则,将不会写入数据。