0

我使用以下方法将数据写入一个 android 应用程序中的文件

 private void writeFileToInternalStorage() {

    String eol = System.getProperty("line.separator");
    BufferedWriter writer = null;

 try{
    writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("myFile.txt", MODE_WORLD_WRITEABLE|MODE_WORLD_READABLE)));
      writer.write("Hello world!");
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    } 
    finally 
    {
      if (writer != null) 
      {
        try 
        {
            writer.close();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
      }
    }
}

然后我尝试使用此方法从另一个 android 应用程序中读取该文件

 private void readFileFromInternalStorage(){
    String eol = System.getProperty("line.separator");
    BufferedReader input = null;
    try 
    {
        input = new BufferedReader(new InputStreamReader(openFileInput("myFile1.txt")));
         String line;
        StringBuffer buffer = new StringBuffer();
        while ((line = input.readLine()) != null) 
        {
            buffer.append(line + eol);


        }

       TextView tv = (TextView) findViewById(R.id.textView);  
        tv.setText(buffer.toString().trim());

    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    } 
    finally 
    {
        if (input != null) 
        {
            try 
            {
                input.close();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    }
}

第二种方法无法读取文件。我也添加了读写权限,但它只显示空白屏幕。可能是什么错误,我该如何纠正?我是 Android 编程新手,需要您的帮助。

谢谢!

4

2 回答 2

1

问题是

openFileOutput("myFile.txt", MODE_WORLD_WRITEABLE|MODE_WORLD_READABLE))

文档说:

该文件被写入相对于您的应用程序的路径中

所以情况是你在相对于应用程序 1 的路径中写入文件,并试图从相对于应用程序 2 的路径中读取它。

您应该能够调用 Environment.getExternalStorageDirectory() 来获取 SD 卡的根路径并使用它来创建 FileOutputStream。从那里,只需使用标准的 java.io 例程。

查看下面的代码段以将文件写入 SD 卡。

private void writeToSDCard() {


try
        {
            File file = new File(Environment.getExternalStorageDirectory(),
                    "filename");
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            writer.write("Hello World");
            writer.close();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

查看下面的片段以读取保存在 SD 卡上的文件。

private void readFileFromSDCard() {
    File directory = Environment.getExternalStorageDirectory();
    // Assumes that a file article.rss is available on the SD card
    File file = new File(directory + "/article.rss");
    if (!file.exists()) {
        throw new RuntimeException("File not found");
    }
    Log.e("Testing", "Starting to read");
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(file));
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
于 2012-06-08T09:35:18.020 回答
0

您最好的选择是将其放入 scdcard 中,例如/sdcard/Android/data/package/shared/

于 2012-06-08T09:31:49.250 回答