如何从内部存储读取文件内容- Android应用程序

dvtswwa3  于 2023-01-19  发布在  Android
关注(0)|答案(6)|浏览(192)

我是一个安卓新手,已经在data/data/myapp/files/hello.txt位置创建了一个文件;这个文件的内容是“hello”。2我如何读取这个文件的内容?

k4emjkb1

k4emjkb11#

看看如何在android中使用存储http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
要从内部存储读取数据,您需要您的应用程序文件文件夹,并从此处读取内容

String yourFilePath = context.getFilesDir() + "/" + "hello.txt";
File yourFile = new File( yourFilePath );

您也可以使用此方法

FileInputStream fis = context.openFileInput("hello.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
    sb.append(line);
}
b91juud3

b91juud32#

读取一个文件作为字符串完整版本(处理异常,使用UTF-8,处理新行):

// Calling:
/* 
    Context context = getApplicationContext();
    String filename = "log.txt";
    String str = read_file(context, filename);
*/  
public String read_file(Context context, String filename) {
        try {
            FileInputStream fis = context.openFileInput(filename);
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            return sb.toString();
        } catch (FileNotFoundException e) {
            return "";
        } catch (UnsupportedEncodingException e) {
            return "";
        } catch (IOException e) {
            return "";
        }
    }

注意:你不需要担心文件路径,只有文件名.

kyks70gy

kyks70gy3#

调用以下函数,参数为文件路径:

private String getFileContent(String targetFilePath) {
      File file = new File(targetFilePath);
      try {
        fileInputStream = new FileInputStream(file);
      } catch (FileNotFoundException e) {
        Log.e("", "" + e.printStackTrace());
      }

      StringBuilder sb;
      while (fileInputStream.available() > 0) {
        if (null == sb) {
           sb = new StringBuilder();
        }
        sb.append((char) fileInputStream.read());
      }

      String fileContent;
      if (null != sb) {
        fileContent = sb.toString();
        // This is your file content in String.
      }
      try {
        fileInputStream.close();
      } catch (Exception e) {
        Log.e("", "" + e.printStackTrace());
      }
      return fileContent;
  }
9lowa7mx

9lowa7mx4#

String path = Environment.getExternalStorageDirectory().toString();
    Log.d("Files", "Path: " + path);
    File f = new File(path);
    File file[] = f.listFiles();
    Log.d("Files", "Size: " + file.length);
    for (int i = 0; i < file.length; i++) {
        //here populate your listview
        Log.d("Files", "FileName:" + file[i].getName());

    }
esbemjvw

esbemjvw5#

我更喜欢使用java.util.Scanner

try {
    Scanner scanner = new Scanner(context.openFileInput(filename)).useDelimiter("\\Z");
    StringBuilder sb = new StringBuilder();

    while (scanner.hasNext()) {
        sb.append(scanner.next());
    }

    scanner.close();

    String result = sb.toString();

} catch (IOException e) {}
li9yvcax

li9yvcax6#

对于其他人寻找一个答案,为什么一个文件是不可读的,特别是在一个sdcard上,写这样的文件第一。.注意MODE_WORLD_READABLE

try {
            FileOutputStream fos = Main.this.openFileOutput("exported_data.csv", MODE_WORLD_READABLE);
            fos.write(csv.getBytes());
            fos.close();
            File file = Main.this.getFileStreamPath("exported_data.csv");
            return file.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

相关问题