0

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.

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

sloth
  • 99,095
  • 21
  • 171
  • 219
Tirtha
  • 351
  • 2
  • 6
  • 22

4 Answers4

7

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

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

Community
  • 1
  • 1
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
6

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 FileOutputStream to write to file. FileOutputStream will 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.

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);
bobby
  • 2,629
  • 5
  • 30
  • 56
Tirtha
  • 351
  • 2
  • 6
  • 22
  • yes good because context is necessary without this you can read irrelevant data. so must use Context context = getApplicationContext(); before calling openFileInput or openFileOutput. – Muhammad Mubashir Mar 19 '13 at 09:57
1

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

Has some good tips.

Community
  • 1
  • 1
Pjrat111
  • 213
  • 1
  • 3
  • 9
0

Absolutely Yes,

Read this 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();
Tai Tran
  • 1,406
  • 3
  • 15
  • 27