这里只是一个快速点.虽然我的代码似乎可以使用在内部或外部(SD卡)存储中写入mytext.txt文件的标准技术将String数据等存储在新文件中,但对我来说,对我来说更有用通过重复允许用户重复该过程(例如用户输入和按钮保存)或关闭应用程序并重新开始,将更多数据添加到同一目录中的同一文件中,因此除非用户选择手动删除,否则数据将永久保留那个数据.使用条件语句检查文件或目录是否存在似乎对情况没有影响,我得到与以前相同的结果(一个答案).还要改变写入方法,如osw.write(myStr); to osw.append(myStr); 保持原样.代码或应用程序中没有错误.以下是MainActivity Java文件的部分典型代码
//earlier code
try
{
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() +
"/MyFiles");
if (!directory.exists())
{
directory.mkdirs();
}
File file = new File(directory, "mytext.txt");
if (!file.exists())
{
file.createNewFile();
}
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new
OutputStreamWriter(fOut);
//---write the string to the file---
osw.write(myStr);
osw.flush();
osw.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
// continued code ...
Run Code Online (Sandbox Code Playgroud)
PS.在我的设备上,mytext.txt文件存储在内部存储(可能名称为sdcard0?)中,而不是期望它位于可访问/可移动SD卡中.