簡體   English   中英

安裝文件的Android FileOutputStream

[英]Android where file is saved FileOutputStream

我在這里如何將字節寫入文件。 我正在使用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();
                    }

我建議用它來寫:

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

所以要閱讀位置:

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

閱讀路徑:

file.getAbsolutePath();

如何在開發人員指南中報告,您必須指定要保存文件的位置。 你可以選擇:

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

只記得設置權限!!!!

這里有整個文檔: 文檔

您的文件保存在路徑/Data/Data/Your package Name/files/myFile.xml您可以使用/Data/Data/Your package Name/files/myFile.xml this.getFileDir()方法獲取Application上文件夾的路徑。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM