Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Android Development

File Storage
Content

Android Internal Storage


Byte and Character Streams
Example

2
Rahul Talreja, Neusoft
Android Internal Storage
Storage of the private data on the device memory.

By default, saving and loading files to the internal storage are private
to the application and other applications will not have access to these
files.

When the user uninstalls the applications the internal stored files
associated with the application are also removed.

However, note that some users root their Android phones, gaining


superuser access. These users will be able to read and write whatever
files they wish.
3
Rahul Talreja, Neusoft
Streams

4
Rahul Talreja, Neusoft
Streams

5
Rahul Talreja, Neusoft
Streams
 A way of sequentially accessing a file. In Streams you can
process the data one at a time as bulk operations are unavailable
with them.

 But, streams supports a huge range of source and destinations


including disk file, arrays, other devices, other programs etc.

 In Java, a byte is not the same thing as a char .

 Therefore a byte stream is different from a character stream. So,


Java defines two types of streams:
 Byte stream
 Character stream 6
Rahul Talreja, Neusoft
Byte Streams
 Read a file byte by byte.

 Java programs use byte streams to perform input and output of 8-


bit bytes.

 It is suitable for any kind of file, however not quite appropriate for
text files.

7
Rahul Talreja, Neusoft
Byte Streams

 Byte oriented streams do not use any encoding scheme while


Character oriented streams use character encoding
scheme(UNICODE).

 All byte stream classes are descended from InputStream and


OutputStream .

8
Rahul Talreja, Neusoft
Character Streams

Read a file character by character.

That means, a character stream needs to be given the file's


encoding in order to work properly.

Character stream can support all types of character sets ASCII,


Unicode, UTF-8, UTF-16 etc. All character stream classes are
descended from Reader and Writer .

9
Rahul Talreja, Neusoft
Reading & Writing

Reading and Writing Text File in Android Internal Storage


Android offers openFileInput and openFileOutput from the Java I/O
classes to modify reading and writing streams from and to local files.

FileOutputStream fOut = openFileOutput("filename",Context.MODE_PRIVATE);


String str = "test data";
fOut.write(str.getBytes());
fOut.close();

10
Rahul Talreja, Neusoft
Reading & Writing

openFileInput(): This method is used to open a file and read it.


It returns an instance of FileInputStream. Its syntax is given
below:
FileInputStream fin = openFileInput(file);
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
fin.close();

11
Rahul Talreja, Neusoft
Example

12
Rahul Talreja, Neusoft
13
Rahul Talreja, Neusoft

You might also like