【问题标题】:Android where file is saved FileOutputStream保存文件的Android FileOutputStream
【发布时间】:2015-10-21 23:03:14
【问题描述】:

这里我如何将字节写入文件。我正在使用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();
                    }

【问题讨论】:

  • 您可能正在崩溃,因为new FileOutputStream("myFile.xml") 在 Android 上没有任何意义。如果您希望将myFile.xml 存储在internal storage 中,请使用类似:new FileOutputStream(new File(getActivity().getFilesDir(), "myFile.xml"))。如果您希望将myFile.xml 存储在external storage 中,请使用类似:new FileOutputStream(new File(getActivity().getExternalFilesDir(null), "myFile.xml"))

标签: android file save


【解决方案1】:

我建议用它来写作:

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

所以要读取位置:

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

读取路径:

file.getAbsolutePath();

【讨论】:

    【解决方案2】:

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

    所以使用this.getFileDir() + "myFile.xml" 来读取文件。

    【讨论】:

      【解决方案3】:

      如何在开发人员指南中报告它,您必须指定要保存文件的位置。您可以选择:

      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. 或者在第二个实例中,您可以将文件保存在外部存储中:

        // Checks if external storage is available to at least read
        public boolean isExternalStorageReadable() {
          String state = Environment.getExternalStorageState();
          if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
          }
          return false;
        }
        

      记得设置权限!!!!

      这里有完整的文档:Documentation

      【讨论】:

        猜你喜欢
        • 2015-03-05
        • 2021-04-02
        • 1970-01-01
        • 1970-01-01
        • 2016-03-20
        • 2021-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多