我使用下面的代码将文件写入 sd 卡并读取它的内容:
try {
if (root.canWrite())
{
File rootdir = Environment.getExternalStorageDirectory();
File yourFile = new File(rootdir, "tomato50.txt");
FileWriter filewriter = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(filewriter);
for (int k=0; k<assignArr.size(); k++)
{
out.write(assignArr.get(k) + "\n");
Toast.makeText(MainActivity.this, "out: " + assignArr.get(k), Toast.LENGTH_LONG).show();
}
out.close();
}
} catch (IOException e) {
Log.e("TAG", "Could not write file " + e.getMessage());
try {
File rootdir = Environment.getExternalStorageDirectory();
File yourFile = new File(rootdir, "tomato50.txt");
FileReader filereader = new FileReader(yourFile);
BufferedReader br = new BufferedReader(filereader);
String line;
while((line = br.readLine()) != null)
{
assignArrBe.add(line);
Toast.makeText(MainActivity.this, "Read from file: " + line, Toast.LENGTH_LONG).show();
}
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
问题是:我如何写入手机内存并从中读取?