【问题标题】:Is it possible to read a file from internal storage (Android)?是否可以从内部存储(Android)读取文件?
【发布时间】:2016-06-18 00:05:02
【问题描述】:

我想在内部存储中保存一个文件。下一步是我想读取该文件。 该文件是使用 FileOutputStream 在内部存储中创建的,但读取文件时出现问题。

是否可以访问内部存储来读取文件?

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    是的,您可以从内部存储中读取文件。

    你可以用这个来写文件

    String FILENAME = "hello_file";
    String string = "hello world!";
    
    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();
    

    要读取文件,请使用以下命令:

    从内部存储读取文件:

    调用openFileInput() 并将要读取的文件名传递给它。这将返回一个FileInputStream。使用read() 从文件中读取字节。然后用close()关闭流。

    代码:

    StringBuilder sb = new StringBuilder();
    try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            is.close();
        } catch(OutOfMemoryError om) {
            om.printStackTrace();
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        String result = sb.toString();
    

    参考这个link

    【讨论】:

    • 您好,我有一个图像文件,我如何获取该文件,在您的情况下 resultString
    【解决方案2】:

    可以从内部存储中写入和读取文本文件。如果是内部存储,则无需直接创建文件。使用FileOutputStream 写入文件。 FileOutputStream 将自动在内部存储中创建文件。无需提供任何路径,只需要提供文件名即可。现在读取文件使用FileInputStream。它将自动从内部存储中读取文件。下面我提供了读写文件的代码。

    写入文件的代码

    String FILENAME ="textFile.txt";
    String strMsgToSave = "VIVEKANAND";
    FileOutputStream fos;
    try
    {
        fos = context.openFileOutput( FILENAME, Context.MODE_PRIVATE );
        try
        {
            fos.write( strMsgToSave.getBytes() );
            fos.close();
    
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    

    }

    读取文件的代码

    int ch;
    StringBuffer fileContent = new StringBuffer("");
    FileInputStream fis;
    try {
        fis = context.openFileInput( FILENAME );
        try {
            while( (ch = fis.read()) != -1)
                fileContent.append((char)ch);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    
    String data = new String(fileContent);
    

    【讨论】:

    • 是的,因为上下文是必要的,没有这个你可以读取不相关的数据。所以必须使用 Context context = getApplicationContext();在调用 openFileInput 或 openFileOutput 之前。
    【解决方案3】:

    这个帖子似乎正是你要找的Read/write file to internal private storage

    有一些很好的提示。

    【讨论】:

      【解决方案4】:

      绝对是的,

      阅读此http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

      String FILENAME = "hello_file";
      String string = "hello world!";
      
      FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
      fos.write(string.getBytes());
      
      fos.close();
      

      【讨论】:

        猜你喜欢
        • 2020-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-29
        • 1970-01-01
        相关资源
        最近更新 更多