我有这段代码在手机内部存储器中创建一个文件:
File fileDir = getFilesDir();
String filedir=fileDir.toString();
String strNewFileName = "information.csv";
String strFileContents = (mes + "," + lat + "," + lng);
File newFile = new File(fileDir, strNewFileName);
try{
boolean filestat = newFile.createNewFile();
FileOutputStream fo =
new FileOutputStream(newFile.getAbsolutePath());
fo.write(strFileContents.getBytes());
fo.close();
}
catch(IOException e)
{Log.v("TEST","Exception on create ");}
System.out.print(OpenFileDialog("information.csv"));
}
我有这个代码来读取和打印文件:
public ArrayList<String> OpenFileDialog(String file){
//Read file in Internal Storage
FileInputStream fis;
ArrayList<String> list = new ArrayList<String>();
try {
fis = openFileInput(file);
String line = "string";
BufferedReader buffreader = new BufferedReader(new InputStreamReader(fis));
while (( line = buffreader.readLine()) != null) {
list.add(line);
System.out.println(list);
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
然而,代码只返回了它在应用程序运行期间添加的一个字符串。我想多次运行应用程序并获取文件的所有行。我的代码是错误的还是应用程序关闭后文件被清空了?