是否可以从内部存储(Android)读取文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10860357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:15:55  来源:igfitidea点击:

Is it possible to read a file from internal storage (Android)?

androidandroid-intent

提问by Tirtha

I want to save a file in internal storage.The next step is i want to read the file. The file is created in the internal storage using FileOutputStream but there is problem in reading the file.

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

Is it possible to access internal storage to read the file?

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

回答by Shankar Agarwal

Yes you can read file from internal storage.

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

for writing file you can use this

对于写入文件,您可以使用它

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

to read a file use the below:

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

To read a file from internal storage:

从内部存储读取文件:

Call openFileInput()and pass it the name of the file to read. This returns a FileInputStream. Read bytes from the file with read(). Then close the stream with close().

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

Code:

代码:

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();

Refer this link

参考这个链接

回答by Tirtha

It is possible to write and read the text file from internal storage. In case of internal storage there is no need to create the file directly. Use FileOutputStreamto write to file. FileOutputStreamwill create the file in internal storage automatically. There is no need to provide any path, you only need to provide the file name. Now to read the file use FileInputStream. It will automatically read the file from internal storage. Below I am providing the code to read and write to file.

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

Code to write the file

写入文件的代码

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

}

}

CODE TO READ THE FILE

读取文件的代码

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

回答by Pjrat111

This thread seems to be exactly what you are looking for Read/write file to internal private storage

该线程似乎正是您要查找的内容读取/写入文件到内部私有存储

Has some good tips.

有一些很好的提示。

回答by Tai Tran

Absolutely Yes,

绝对没错,

Read this http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

阅读此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();