安卓文件管理

发布于 2024-11-07 23:30:54 字数 1426 浏览 0 评论 0原文

我使用下面的代码将文件写入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

得不到的就毁灭 2024-11-14 23:30:54

您需要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

寄人书 2024-11-14 23:30:54

首先,您必须在清单文件中添加以下行:

...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>

如果还不够,您能更详细地解释问题吗?

First of all you have to put in your manifest file this line:

...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>

If it is not enough, can you explain the problem with more details?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文