Friday, March 23, 2012

Saving TextFile to SDCARD in android?


Saving TextFile to SDCARD in android?

Hello android lovers,
In today’s tutorial I will show you how to
1. Create a text file and save a textfile in your SDCARD in your preferred path.

2. Read the contents from the same file and show it in a TextView.
For writing a file we use the FileWriter class and for reading the textfile we use the FileReader class.
Now Let’s check out how we do this .
First create a fresh project and name it “saveFileDemo”.
In the “saveFileDemo.java” file copy this contents.
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.coderzheaven.pack;
 
import java.io.FileReader;
import java.io.FileWriter;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class saveFileDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        createFile();
        String file_contents = readFile();
        System.out.println("FILE CONTENTS :  "  + file_contents);
 
        TextView tv = (TextView)findViewById(R.id.tv);
        tv.setText("Read File contents from SDCARD : \n" + file_contents);
    }
    public void createFile(){
        FileWriter fWriter;
        try{
             fWriter = new FileWriter("/sdcard/myfile.txt");
             fWriter.write("My file contents");
             fWriter.flush();
             fWriter.close();
         }catch(Exception e){
                  e.printStackTrace();
         }
    }
    public String readFile(){
        char buf[] = new char[512];
        FileReader rdr;
        String contents = "";
        try {
            rdr = new FileReader("/sdcard/myfile.txt");
            int s = rdr.read(buf);
            for(int k = 0; k < s; k++){
                contents+=buf[k];
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return contents;
    }
}
This is the main.xml file contents.
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:id = "@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>
Here is the Strings.xml file contents
?
Drag and copy the code
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, saveFileDemo!</string>
    <string name="app_name">saveFileDemo from Coderzheaven</string>
</resources>
Now the AndroidManifest.xml file contents.
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
      package="com.coderzheaven.pack"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".saveFileDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

You can actually see the file by going to the FileExplorer and expanding the mnt folder(or simply SDCARD in older versions) and the SDCARD folder.
See the screenshot.

No comments: