安卓文件管理
我使用下面的代码将文件写入 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();
}
问题是:如何写入手机内存并从中读取内容?
i use the code below to write a file to the sd card and read the content of it:
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();
}
Question is: how can i write to the phone memory and read from it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要root您的手机才能获得对任何手机文件夹的写入权限。
如果您想要写入特定于应用程序的内部存储,那么您可以遵循以下指南:
You will need to root your phone in order to get write access to any phone folder.
If what you want is to write in the app-specific internal storage, then you can follow this guide: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
首先,您必须在清单文件中添加以下行:
如果还不够,您能更详细地解释问题吗?
First of all you have to put in your manifest file this line:
If it is not enough, can you explain the problem with more details?