Top Banner
File Handling in Android How to read & write file in Android
17

File Handling

Jul 07, 2016

Download

Documents

Nick

Android File Handling Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: File Handling

File Handlingin AndroidHow to read & write file in Android

Page 2: File Handling

Basic Steps Create new project. Create interface(main_activity)

File Name File Text Write file (Button) Read file (Button) Edit text where read data is placed

Page 3: File Handling

Basic Steps (cont.) Create a FileOperations class

FileWriter BufferedWriter FileReader BufferedReader

Create MainActivity.java file Add permission to manifest

Page 4: File Handling

Step 1: done Project and Interface is created

Page 5: File Handling

Step 2: Create FileOperations

java Class

=> Create String path.

=> Create FileReader/FileWriter with path.

Create BufferedReader/ BufferedWriter.

Read file / write file.

Page 6: File Handling

Step 3: Create MainActivity java file

Use Object of FileOperations class

for reading and writing purposes

from in.

Page 7: File Handling

Step 4: Add permission to

manifest

Page 8: File Handling

Run AVD and install you .apk file

Page 9: File Handling
Page 10: File Handling
Page 11: File Handling

Difference between FileOutputSream & FileWriterFile fout = new File(file_location_string);

FileOutputStream fos = new FileOutputStream(fout);

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos));

out.write("something");

FileWriter fstream = new FileWriter(file_location_string);

BufferedWriter out = new BufferedWriter(fstream);

out.write("something");

Page 12: File Handling

Difference between FileOutputSream & FileWriter FileOutputStream is meant for writing

streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

There is actually no difference per se, FileWriter is just a convenience class. It extends OutputStreamWriter and creates the needed FileOutputStream itself.

Page 13: File Handling

Uses of FileOutputSream & FileWriter If you are dealing with binary data (e.g.

an image) use Streams. If you are using non-ASCII Unicode

characters, e.g. Chinese, use Readers/Writers.

If you are using ordinary ASCII text (the traditional 0-127 characters) you can (usually) use either.

Page 14: File Handling

Simple Example of FileOutputStream

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

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

fos.write(string.getBytes());fos.close();

Page 16: File Handling

How to append data to existing file Just one change. FileOutputStream fOut =

openFileOutput("savedData.txt", MODE_APPEND | MODE_WORLD_READABLE );

Page 17: File Handling

Delete a file You should always delete files that you no

longer need. The most straightforward way to delete a file is to have the opened file reference call delete() on itself.

myFile.delete(); If the file is saved on internal storage, you

can also ask the Context to locate and delete a file by calling deleteFile():

myContext.deleteFile(fileName);