安卓中文件保存在哪里FileOutputStream

6

这是我如何将字节写入文件的方法。我使用 FileOutputStream

    private final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                FragmentActivity activity = getActivity();
                        byte[] readBuffer = (byte[]) msg.obj;
                        FileOutputStream out = null;
                        try {
                            out = new FileOutputStream("myFile.xml");
                            out.write(readBuffer);
                            out.close();
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
           }
}

现在我想打开那个文件,所以我需要这个文件的路径。那么我该如何打开这个文件呢?

编辑:

这是我读取文件的代码,但是我看不到任何内容...

BufferedReader reader = null;
                    FileInputStream s = null;
                    try {
                        s = new FileInputStream("mano.xml");
                        reader = new BufferedReader(new InputStreamReader(s));

                        String line = reader.readLine();
                        Log.d(getTag(), line);
                        while (line != null) {
                            Log.d(getTag(), line);
                            line = reader.readLine();
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

3
你可能会出现问题,因为在Android上new FileOutputStream("myFile.xml")没有意义。如果你想将myFile.xml存储在内部存储中,请使用类似以下的语句:new FileOutputStream(new File(getActivity().getFilesDir(), "myFile.xml"))。如果你想将myFile.xml存储在外部存储中,请使用类似以下的语句:new FileOutputStream(new File(getActivity().getExternalFilesDir(null), "myFile.xml")) - CommonsWare
3个回答

4

我建议使用以下内容进行编写:

OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/yourfilename");

因此,要读取位置:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+transaction.getUniqueId()+".pdf");

读取路径:

file.getAbsolutePath();

0

您的文件保存在路径 /Data/Data/您的包名/files/myFile.xml 中,您可以使用 this.getFileDir() 方法获取应用程序中文件夹的路径。

因此,使用 this.getFileDir() + "myFile.xml" 来读取文件。


0

在开发者指南中,报告方式是您必须指定要保存文件的位置。您可以在以下选项之间进行选择:

  1. 将文件保存在内部存储器中:

    String filename = "myfile";
    String string = "Hello world!";
    FileOutputStream outputStream;
    
    try {
      outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
      outputStream.write(string.getBytes());
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    
  2. 或者您可以将文件保存在外部存储器中:

    // 检查是否至少可以读取外部存储器
    public boolean isExternalStorageReadable() {
      String state = Environment.getExternalStorageState();
      if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
      }
      return false;
    }
    

记得设置权限!!!

这里有完整的文档:文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接