【发布时间】:2020-06-02 12:03:40
【问题描述】:
我的代码每秒生成数据并显示在屏幕上。 这一切都很好,但我想创建一个包含所有数据的日志文件以供以后分析。
我可以在每次创建数据时打开/写入/关闭文件,但我不确定这使用了多少处理能力,因为它会不断打开和关闭文件
String data= reading1","+reading2+","+time +"/n";
try {
FileOutputStream out = openFileOutput("data.csv", Context.MODE_PRIVATE);
out.write(data.getBytes());
out.close();
} catch (Exception e) {
e.printStackTrace();
我希望在单击开始按钮时打开文件。
if ( v.getId() == R.id.start ){
// checks which button is clicked
Log.d("dennis", "Scan working"); //logs the text
// open a file
try {
FileOutputStream out = openFileOutput("data.csv", Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
但在关闭文件时,输入 out 时不会出现 .close() 选项
if ( v.getId() == R.id.stop ){
// checks which button is clicked
out. // no valid options appear
messageValue.setText(R.string.stopButtonText);// changes the hallo world text
readNoRead=false;
}
所有的打开/写入/关闭是否需要在一起或者是否有可能
***open file***
-----
Cycle through all the data
-----
***Close file***
【问题讨论】:
-
您可以随时关闭输出流,只要您有引用即可。
标签: java android file io fileoutputstream