【问题标题】:How to write to a textfile in one Activity & read that file in another Activity?如何在一个活动中写入文本文件并在另一个活动中读取该文件?
【发布时间】:2015-05-13 07:21:06
【问题描述】:

我能够在 SAME 活动中写入然后读取文本文件,但在从另一个 Activity 写入文本文件后我无法读取该文件。

例如: Activity A 创建并写入文本文件。 活动 B 读取该文本文件。

我使用此代码写入 Activity A 中的文本文件:

FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try 
        {
            fos = openFileOutput("user_info.txt", Context.MODE_WORLD_WRITEABLE);
            osw = new OutputStreamWriter(fos);
            osw.write("text here");
            osw.close();
            fos.close();
        } 
        catch (FileNotFoundException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

然后我使用此代码尝试读取 Activity A 创建的相同文本文件,但我得到了 FileNotFoundException:

            try 
            {
                FileInputStream fis = openFileInput("user_info.txt");
                InputStreamReader isr = new InputStreamReader(fis);
                BufferedReader buff = new BufferedReader(isr);
                String line;
                while((line = buff.readLine()) != null)
                {

                    Toast.makeText(this, line, Toast.LENGTH_LONG).show();
                }
            } 
            catch (FileNotFoundException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

有人知道我为什么收到FileNotFoundException 吗?

是路径问题吗?

【问题讨论】:

    标签: android file read-write


    【解决方案1】:

    真的不知道你的应用程序是如何构建的,但是,你得到的错误似乎是一个路径问题,你确定两个活动都在同一个文件夹中吗? 如果没有,您需要为文本文件设置绝对路径(如:“/home/user/text.txt”)或相对路径(如:“../text.txt”)。 如果您不确定,请尝试使用诸如

    之类的命令打印 Activity 的当前路径
    new File(".").getAbsolutePath();
    

    而且,虽然我不能说我是 Android 专家,但您确定您的文件需要 Context.MODE_WORLD_WRITEABLE 吗?如果除了您的应用程序之外没有其他应用程序正在读取或写入它,那应该没有必要,对吧?

    【讨论】:

      【解决方案2】:

      这肯定是路径问题。 你可以这样写

      fpath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"yourdirectory";
      
      File custdir=new File(fpath);
      if(!custdir.exists())
              {
                  custdir.mkdirs();
      
              }
                          File savedir=new File(custdir.getAbsolutePath());
                          File file = new File(savedir, filename);
      
                          if(file.exists())
                          {
                              file.delete();
                          }
                          FileOutputStream fos;
                          byte[] data = texttosave.getBytes();
                          try {
                              fos = new FileOutputStream(file);
                              fos.write(data);
                              fos.flush();
                              fos.close();
                              Toast.makeText(getBaseContext(), "File Saved", Toast.LENGTH_LONG).show();
                              finish();
                          } catch (FileNotFoundException e) {
                              Toast.makeText(getBaseContext(), "Error File Not Found", Toast.LENGTH_LONG).show();
                              Log.e("fnf", ""+e.getMessage());
                              // handle exception
                          } catch (IOException e) {
                              // handle exception
                              Toast.makeText(getBaseContext(), "Error IO Exception", Toast.LENGTH_LONG).show();
                          }
      

      你可以像这样阅读

      String locatefile=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"yourdirectory"+"/filename";
      
      
      
      
      try {
                  br=new BufferedReader(new FileReader(locatefile));
                           while((text=br.readLine())!=null)
          {
              body.append(text);
              body.append("\n");
      
          }
       } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
      
       } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-23
        • 1970-01-01
        • 2016-08-14
        • 1970-01-01
        • 2019-04-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多