Working with Text Files
Persistent data ေတြကို သိမ္းဆည္း၊ ထုတ္သံုးတာမ်ိဳးလုပ္ေပးႏုိင္တဲ့ application မ်ိဳးလည္းလို္အပ္တတ္ပါတယ္။ Freeform text file (text notes) or organized data (data table) ေတြကိုသံုးျပီးလုပ္ႏုိင္ပါတယ္။ (၁) Text file ေတြကုိ application ရဲ႕အစိတ္အပိုင္းအျဖစ္တြဲျပီး deliver လုပ္ႏုိင္တယ္။ ဒီနည္းနဲ႕လုပ္ရင္ေတာ့ အဲဒီ text file ေတြဟာ read-only ဘဲရမယ္။ User ကျပင္လို႕ေတာ့မရဘူး။ (၂) ေနာက္တစ္နည္းကေတာ့ user က text file ေတြကို create, modify, and delete လုပ္တဲ့နည္းပါ။ နည္းလမ္း ၂ မ်ိဳးလံုကိုရွင္းျပသြားပါမယ္။
Application ကအသံုးျပဳမယ့္ text file ကုိ ထားဘုိ႕ res folder ေအာက္မွာ folder အသစ္တစ္ခုလုပ္ရပါမယ္။ Package Explorer မွာ res folder ကုိ right click လုပ္ပါ။ Pup Up Menu ေပၚလာရင္ New -> Folder ကုိေရြးပါ။ Folder name ကုိ raw လုိ႕အမည္ေပးပါ။ media file နဲ႕ text file ေတြကို raw folder ထဲမွာသာသိမ္းရပါမယ္။ raw folder ကုိ right click လုပ္ျပီး New -> File ကိုေရြးပါ။ File name ကုိ params.txt လို႕ေပးပါ။ params.txt file ထဲမွာ ၾကိဳက္ရာစာအနည္းငယ္ေရးပါ။ R.java file ထဲမွာ အခု create လုပ္လိုက္တဲ့ raw folder ထဲက params.txt file အတြက္ generated လုပ္ထားတဲ့ code ကုိေအာက္မွာျပထားတဲ့အတုိင္း ေတြ႕ျမင္ႏုိင္ပါတယ္။
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.*/
package com.sheusi.FileWerx;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int FileViewer=0x7f060000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class raw {
public static final int params=0x7f040000;
}
public static final class string {
public static final int app_name=0x7f050001;
public static final int hello=0x7f050000;
}
}
ဒီတစ္ခါ main.xml file ကုိေရးပါမယ္။ ဒီဖုိင္ထဲမွာ TextView တစ္ခုထည့္ပါမယ္။ Application ကုိ run လုိက္ရင္ params.txt file မွာေရးထားတဲ့ text ေတြကို TextView မွာျပမွာမုိ႕ပါ။ TextView ထဲကို parameter ေတြထည့္ပါမယ္။ မူလထဲက TextView တစ္ခုရွိျပီးျဖစ္ပါတယ္။ ခု ေနာက္တစ္ခုထပ္ထည့္လုိက္ေတာ့ TextView ၂ခုျဖစ္ပါမယ္။ ေအာက္ပါအတုိင္္းပါ။
<?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:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello”
/>
<TextView
Persistent data ေတြကို သိမ္းဆည္း၊ ထုတ္သံုးတာမ်ိဳးလုပ္ေပးႏုိင္တဲ့ application မ်ိဳးလည္းလို္အပ္တတ္ပါတယ္။ Freeform text file (text notes) or organized data (data table) ေတြကိုသံုးျပီးလုပ္ႏုိင္ပါတယ္။ (၁) Text file ေတြကုိ application ရဲ႕အစိတ္အပိုင္းအျဖစ္တြဲျပီး deliver လုပ္ႏုိင္တယ္။ ဒီနည္းနဲ႕လုပ္ရင္ေတာ့ အဲဒီ text file ေတြဟာ read-only ဘဲရမယ္။ User ကျပင္လို႕ေတာ့မရဘူး။ (၂) ေနာက္တစ္နည္းကေတာ့ user က text file ေတြကို create, modify, and delete လုပ္တဲ့နည္းပါ။ နည္းလမ္း ၂ မ်ိဳးလံုကိုရွင္းျပသြားပါမယ္။
Application ကအသံုးျပဳမယ့္ text file ကုိ ထားဘုိ႕ res folder ေအာက္မွာ folder အသစ္တစ္ခုလုပ္ရပါမယ္။ Package Explorer မွာ res folder ကုိ right click လုပ္ပါ။ Pup Up Menu ေပၚလာရင္ New -> Folder ကုိေရြးပါ။ Folder name ကုိ raw လုိ႕အမည္ေပးပါ။ media file နဲ႕ text file ေတြကို raw folder ထဲမွာသာသိမ္းရပါမယ္။ raw folder ကုိ right click လုပ္ျပီး New -> File ကိုေရြးပါ။ File name ကုိ params.txt လို႕ေပးပါ။ params.txt file ထဲမွာ ၾကိဳက္ရာစာအနည္းငယ္ေရးပါ။ R.java file ထဲမွာ အခု create လုပ္လိုက္တဲ့ raw folder ထဲက params.txt file အတြက္ generated လုပ္ထားတဲ့ code ကုိေအာက္မွာျပထားတဲ့အတုိင္း ေတြ႕ျမင္ႏုိင္ပါတယ္။
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.*/
package com.sheusi.FileWerx;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int FileViewer=0x7f060000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class raw {
public static final int params=0x7f040000;
}
public static final class string {
public static final int app_name=0x7f050001;
public static final int hello=0x7f050000;
}
}
ဒီတစ္ခါ main.xml file ကုိေရးပါမယ္။ ဒီဖုိင္ထဲမွာ TextView တစ္ခုထည့္ပါမယ္။ Application ကုိ run လုိက္ရင္ params.txt file မွာေရးထားတဲ့ text ေတြကို TextView မွာျပမွာမုိ႕ပါ။ TextView ထဲကို parameter ေတြထည့္ပါမယ္။ မူလထဲက TextView တစ္ခုရွိျပီးျဖစ္ပါတယ္။ ခု ေနာက္တစ္ခုထပ္ထည့္လုိက္ေတာ့ TextView ၂ခုျဖစ္ပါမယ္။ ေအာက္ပါအတုိင္္းပါ။
<?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:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello”
/>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:minLines=”3″
android:maxLines=”3″
android:textColor=”#000000″
android:textSize=”12px”
android:background=”#ff0000″
android:text=”"
android:id=”@+id/FileViewer”
/>
</LinearLayout>
ဒီတစ္ခါ Java file ကုိၾကည့္ရေအာင္။
Activity class ရဲ႕ method တစ္ခုျဖစ္တဲ့ getResources( ) method က Resources class variable ေတြကို assign လုပ္ေပးပါတယ္။
myResources=this.getResources();
ဒီ Java file ကေန raw folder ကုိသံုးတဲ့အခါ findViewByID( ) ကိုသံုးလို႕မရပါဘူး။ ဘာေၾကာင့္လဲဆုိေတာ့ raw က View class ရဲ႕ object မဟုတ္လုိ႕ပါ။ Resources class ရဲ႕ method တစ္ခုျဖစ္တဲ့ openRawResource( ) method ကိုသာအသံုးျပဳရပါမယ္။
openRawResource() ကေတာ့ params.txt file ရဲ႕ InputStream ကို return ျပန္ပါမယ္။
is=myResources.openRawResource(R.raw.params);
params.txt file ရဲ႕ content ေတြကို retrieve လုပ္လို႕ရေအာင္ DataInputStream object တစ္ခုကို create လုပ္ပါတယ္။
dis=new DataInputStream(is);
ေနာက္ဆံုးမွာ file ရဲ႕ contents ေတြကို String တစ္ခုမွာ assign လုပ္ပါတယ္။
String someText=null;
try{
someText=dis.readLine();
}catch(IOException ioe){
someText=”Couldn’t read the file”;
}
အဲဒီ string ကုိ TextView မွာ diaply လုပ္ျပပါတယ္။
tv.setText(someText);
Java File ရဲ႕ code မ်ားကိုေအာက္မွာၾကည့္ပါ။
package com.sheusi.FileWerx;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import java.io.*;
import android.content.res.Resources;
public class FileWerx extends Activity {
/** Called when the activity is first created. */
InputStream is=null;
DataInputStream dis=null;
Resources myResources=null;
TextView tv=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myResources=this.getResources();
tv=(TextView)findViewById(R.id.FileViewer);
is=myResources.openRawResource(R.raw.params);
dis=new DataInputStream(is);
String someText=null;
try{
someText=dis.readLine();
}catch(IOException ioe){
someText=”Couldn’t read the file”;
}
tv.setText(someText);
}
}
Media files ေတြကုိလည္း ဒီနည္းအတုိင္းလုပ္ႏုိင္ပါတယ္။ တစ္ခုရွိတာက ဒီလို compile time မွာထည့္လိုက္တဲ့ static resource ေတြဟာ read-only file ေတြသာျဖစ္ပါတယ္။
User ေတြအေနနဲ႕ text file ေတြကုိ create, read, modify and write လုပ္ခ်င္တယ္ဆိုရင္ေတာ့ လုပ္ႏုိင္တဲ့နည္းလမ္းရွိပါတယ္။ ဒီတစ္ခါ Text Editor ေလးတစ္ခုလုပ္ၾကည့္ရေအာင္။ ခုနထည့္ခဲ့တဲ့ TextView အစား EditText တစ္ခုထည့္ရေအာင္။ Button ၃ခုလည္းထည့္မယ္။ EditText မွာ user ကေရးလိုက္တဲ့ text ေတြကို file တစ္ခုမွာ save လုပ္ဘို႕၊ အဲဒီဖုိင္မွာသိမ္းထားတဲ့ text ကုိ retrieve လုပ္ဘုိ႕နဲ႕ clear လုပ္ဘို႕ဆိုျပီးစုစုေပါင္း button ၃ခုလုိမယ္ေလ။ သိမ္းတဲ့အခါ ဖုိင္တစ္ခုနဲ႕သိမ္းရမယ္ေလ။ အဲဒီဖုိင္အမည္ကို mystuff.txt လို႕ေပးမယ္။ ဒီသင္ခန္းစာမွာေတာ့ ဖုိင္အမည္ေပးတာကို Java code ထဲမွာဘဲအေသေရးေပးလုိက္တယ္။ လက္ေတြ႕သံုးတဲ့အခါမွာ user ကုိယ္တုိင္ၾကိဳက္ရာအမည္ေပးျပီးသံုးႏုိင္ေအာင္လုပ္လို႕ရပါတယ္။
main.xml file မွာေအာက္ပါအတုိင္း လုိအပ္တဲ့ View ေတြထည့္မယ္။
<?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:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello”
/>
<EditText
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:minLines=”3″
android:maxLines=”3″
android:textColor=”#000000″
android:textSize=”12px”
android:background=”#ffffff”
android:text=”"
android:id=”@+id/FileViewer”
/>
<TableLayout
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
>
<TableRow
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” >
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_weight=”.5″
android:text=”Load File”
android:id=”@+id/loadbutton”/>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_weight=”.5″
android:text=”Clear Viewer”
android:id=”@+id/clearbutton”/>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_weight=”.5″
android:text=”Save File”
android:id=”@+id/savebutton”/>
</TableRow>
</TableLayout>
</LinearLayout>
Java code ကိုေအာက္ပါအတိုင္းေရးမယ္။
package com.sheusi.FileWerx;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import java.io.*;
import android.content.*;
import android.view.*;
public class FileWerx extends Activity {
/** Called when the activity is first created. */
Context myContext=null;
EditText tv=null;
Button load=null;
Button clear=null;
Button save=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myContext=this.getApplicationContext();
tv=(EditText)findViewById(R.id.FileViewer);
load=(Button)findViewById(R.id.loadbutton);
clear=(Button)findViewById(R.id.clearbutton);
save=(Button)findViewById(R.id.savebutton);
load.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
try{
StringBuffer sBuffer=new StringBuffer();
FileInputStream fis=myContext.openFileInput(“mystuff.txt”);
DataInputStream dis=new DataInputStream(fis);
String someText=null;
while((someText=dis.readLine())!=null)
sBuffer.append(someText+”\n”);
dis.close();
tv.setText(sBuffer.toString());
}catch(IOException ioe){
Toast.makeText(myContext,”Couldn’t open the
file.”,Toast.LENGTH_LONG).show();
}
}
});
clear.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
tv.setText(null);
}
});
save.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
try{
FileOutputStream
fos=myContext.openFileOutput(“mystuff.txt”,MODE_PRIVATE);
DataOutputStream dos=new DataOutputStream(fos);
String someText=null;
someText=tv.getText().toString();
dos.write(someText.getBytes());
dos.close();
}catch(IOException ioe){
Toast.makeText(myContext,”Couldn’t save the file.”,Toast.LENGTH_LONG).show();
}
}
}
}
});
PC မွာသံုးတဲ့ Java applications ေတြလိုဘဲ input-output error ေတြကို ဖမ္းဘုိ႕အတြက္ try-catch block ကိုသံုးထားပါတယ္။ input/output (I/O) error တစ္ခုခုျဖစ္ခဲ့ရင္ screen မွာ Toast message တစ္ခုေပၚလာျပီး user ကုိအသိေပးပါလိမ့္မယ္။ Toast message ဆိုတာ ခဏေလးေပၚလာျပီး အလုိလုိျပန္္ျပန္သြားတဲ့ message မ်ိဳးကိုေျပာတာပါ။
Toast.makeText(myContext,”Couldn’t open the file.”,Toast.LENGTH_LONG).show();
Toast.makeText(myContext,”Couldn’t save the file.”,Toast.LENGTH_LONG).show();
User create လုပ္လုိက္တဲ့ဖုိင္ေတြဟာ Android device ရဲ႕ /data/data/<package name of application>/files/ မွာရွိေနမွာျဖစ္ပါတယ္။ Android OS ဟာဆိုရင္ application တစ္ခုစီကို User Account အျဖစ္မွတ္ယူထားပါတယ္။ Context class မွာဆုိရင္ file management methods မ်ားစြာပါ၀င္တာကိုသတိထားမိမွာပါ။ ဥပမာ
openFileInput( )
openFileOutput( ) Context.deleteFile( )
Context.fileList( )
Context.getFilesDir( )
Context.getCacheDir( )
Context.getDir( )
Credit to: http://computerandmobiletech.com
No comments:
Post a Comment