如何在一个活动中写入文本文件并在另一个活动中读取该文件?



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

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

我使用此代码写入活动 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();
        }

然后我使用此代码尝试读取活动 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

是路径问题吗?

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

new File(".").getAbsolutePath();

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

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

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();
    }

相关内容

最新更新