读取和写入Android内部存储器中的文件(Reading and writing to files in internal storage on Android)

我正在学习从Android的内部存储器读写文件。 我有这样的代码:

String fileName = "MyFile"; String content = "hello world"; FileOutputStream outputStream = null; try { outputStream = openFileOutput(fileName, Context.MODE_PRIVATE); outputStream.write(content.getBytes()); outputStream.close(); } catch (Exception e) { e.printStackTrace(); }

我的文件存储在哪里以及如何阅读?

I'm learning to write and read files from the internal storage on Android. I have this code:

String fileName = "MyFile"; String content = "hello world"; FileOutputStream outputStream = null; try { outputStream = openFileOutput(fileName, Context.MODE_PRIVATE); outputStream.write(content.getBytes()); outputStream.close(); } catch (Exception e) { e.printStackTrace(); }

Where is my file stored and how to read it?

最满意答案

文件应保存在/ data / data / Android / urpackagename /文件夹下。

读书

FileInputStream in = openFileInput("filename.txt"); InputStreamReader inputStreamReader = new InputStreamReader(in); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder sb = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { sb.append(line); }

更多详细信息,使用相同的代码写入和读取openFile ...()api

The File should be saved under /data/data/Android/urpackagename/ folder.

To read

FileInputStream in = openFileInput("filename.txt"); InputStreamReader inputStreamReader = new InputStreamReader(in); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder sb = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { sb.append(line); }

For More Detail with same piece of code Write and Read openFile...() api

更多推荐