Milky Way
Milky Way

Reputation: 287

How to read from internal storage?

If I have this:

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

                FileOutputStream fos = null;
                try {
                    fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    fos.write(string.getBytes());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

What's the code to read that hello_file and put it in a normal string? I tried to find out myself with the dev website but I failed,it doesn't actually show anything and no website actually shows it how to read it... Please help!

Upvotes: 0

Views: 222

Answers (2)

Arun
Arun

Reputation: 1668

First you need to pass the absolete path and the file name. Try below

File image = new File("absolete path", "name of your file");
byte[] data = image.getPath().getBytes();
system.out.println(new String(data));

Upvotes: 1

Nikola Despotoski
Nikola Despotoski

Reputation: 50538

You need to use InputStream because you are doing reading not writing. Specify the new File instance with the file path and open the input stream, then use StringBuilder to get the data from the input stream and create string. Look around for examples if you are not familiar with InputStream and File class.

Upvotes: 0

Related Questions