Data access in Android-Method 2: File)

Source: Internet
Author: User

In Android, these files are stored in/Data/Package_name/FilesDirectory.

Data Reading

 
Public static string read (context, string file) {string data = ""; try {fileinputstream stream = context. openfileinput (File); stringbuffer sb = new stringbuffer (); int C; while (C = stream. read ())! =-1) {sb. append (char) C);} stream. close (); Data = sb. tostring ();} catch (filenotfoundexception e) {} catch (ioexception e) {} return data ;}

SlaveCodeThe only difference seems to be the file opening method: context. openfileinput (File); In Android, file read/write has permission control, so context (parent class of activity) is used to open the file, which is shared in the same package. The Package concept here is different from the package mentioned in preferences.

Data Writing

Public static void write (context, string file, string MSG) {try {fileoutputstream stream = context. openfileoutput (file, context. mode_world_writeable); stream. write (MSG. getbytes (); stream. flush (); stream. close ();} catch (filenotfoundexception e) {} catch (ioexception e ){}}

When opening the file here, the method of opening the file is declared.

In general, it may be difficult to directly use files. In particular, if we want to store some trivial data, we need to generate some trivial files or define the format in the same file. In fact, you can also package it into properties for use:

Public static properties load (context, string file) {properties Properties = new properties (); try {fileinputstream stream = context. openfileinput (File); properties. load (Stream);} catch (filenotfoundexception e) {} catch (ioexception e) {} return properties;} public static void store (context, string file, properties Properties) {try {fileoutputstream stream = context. openfileoutput (file, context. mode_world_writeable); properties. store (stream, "");} catch (filenotfoundexception e) {} catch (ioexception e ){}}
 
 
 
/Chapter09_data_02/src/COM/Amaker/test/mainactivity. Java
 Code 
   Package Com. Amaker. test;

Import Java. Io. fileinputstream;
Import Java. Io. fileoutputstream;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. edittext;

Public Class Mainactivity Extends Activity {
Private Static Final String file_name = " Temp.txt " ;
Private Button B1, B2;
Private Edittext ET1, et2;
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
B1 = (Button) findviewbyid (R. Id. button01 );
B2 = (Button) findviewbyid (R. Id. button02 );

ET1 = (Edittext) findviewbyid (R. Id. edittext01 );
Et2 = (Edittext) findviewbyid (R. Id. edittext02 );

B1.setonclicklistener ( New Onclicklistener (){
@ Override
Public Void Onclick (view v ){
Write (et1.gettext (). tostring ());
}
});

B2.setonclicklistener ( New Onclicklistener (){
@ Override
Public Void Onclick (view v ){
Et2.settext (read ());
}
});
}

Private String read (){
Try {
Fileinputstream = Openfileinput (file_name );
Byte [] Buffer = New Byte [FCM. Available ()];
FS. Read (buffer );
Return New String (buffer );
} Catch (Exception e ){
E. printstacktrace ();
}
Return Null ;
}

Private Void Write (string content ){
Try {
Fileoutputstream fos = Openfileoutput (file_name, mode_append );
FOS. Write (content. getbytes ());
FOS. Close ();
} Catch (Exception e ){
E. printstacktrace ();
}
}
}

 

/Chapter09_data_02/RES/layout/Main. xml

 Code 
    <?  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 = " File Test " />

< Edittext Android: Text = "" Android: ID = " @ + ID/edittext01 "
Android: layout_width = " Fill_parent " Android: layout_height = " Wrap_content " Android: Height = " 100px " > </ Edittext >

< Button Android: ID = " @ + ID/button01 " Android: layout_width = " Wrap_content "
Android: layout_height = " Wrap_content " Android: Text = " Write " > </ Button >

< Edittext Android: Text = "" Android: ID = " @ + ID/edittext02 "
Android: layout_width = " Fill_parent " Android: layout_height = " Wrap_content " Android: Height = " 100px " > </ Edittext >

< Button Android: ID = " @ + ID/button02 " Android: layout_width = " Wrap_content "
Android: layout_height = " Wrap_content " Android: Text = " Read " > </ Button >

</ Linearlayout >

 

/Chapter09_data_02/androidmanifest. xml

 Code 
     <?  XML version = "1.0" encoding = "UTF-8"  ?>  
< Manifest Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Package = "Com. Amaker. test"
Android: versioncode = "1"
Android: versionname = "1.0" >
< Application Android: icon = "@ Drawable/icon" Android: Label = "@ String/app_name" >
< Activity Android: Name = ". Mainactivity"
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 >
< Uses-SDK Android: minsdkversion = "3" />

</ Manifest >

 

 
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.