1, the file read
Io Stream reads the file and displays
Package Com.helloword;import Java.io.bufferedreader;import Java.io.file;import java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstreamreader;public class ReadFile { Private String Text = null;private stringbuffer strbuf=null;public void ReadFile (file file) {//TODO auto-generated Co Nstructor stub//get file output stream FileInputStream fis;try {fis = new FileInputStream (file);//Convert a byte stream to a character stream bufferedreader br = new BufferedReader (New InputStreamReader (FIS)); try {text = Br.readline ();} catch (IOException e) {//TODO auto-generated CATC H Blocke.printstacktrace ();}} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Strbuf.append (text); System.out.println (text);}}
Where the "data/data/com.helloword/file" setup process for accessing Android is as follows
Open file explore to see your Android files
Open cmd, go to SDK Platform-tool
>ADB Shell
$ su
# chmod 777/data
# chmod 777/data/data
public class Mainactivity extends Activity {public Button bt =null; @Overrideprotected void OnCreate (Bundle savedinstances Tate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); bt = (Button) Findviewbyid ( R.id.btcon); Bt.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubfile File = new file ("Data/data/com.helloword/file"); ReadFile ReadFile = new ReadFile ();});}
2. SD card file read and write operation
(1) Register in Manifest.xml, get the Read and write permission of SD card
< Create and delete file permissions!--sdcard---<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <!--write data to SDcard permissions-- <uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/ >
(2) The following static methods are used in the environment class when reading and writing using SDcard:
1:getdatadirectory () Get to the data directory in Android (Data folder in SD card)
2:getdownloadcachedirectory () gets to the downloaded cache directory (the download folder in the SD card)
3:getexternalstoragedirectory () Gets the directory to the external store generally refers to SDcard (/STORAGE/SDCARD0)
4:getexternalstoragestate () Gets the current state of the external settings generally refers to sdcard, the more commonly used should be media_mounted (sdcard exist and can read and write) and some other states, you can find in the document.
/** * Determine if sdcard exists [when there is no external SD card, the built-in ROM is also recognized as an existing SD card] * * @return */public static Boolean issdcardexist () {return environmen T.getexternalstoragestate (). Equals (environment.media_mounted);}
* Use API to get the real path of SD card, some phone brands will change the path of SD card
Environment.getexternalstoragedirectory ();
Read the contents of the SD card
Read SD card Contents//Use FileInputStream to read file public string readflieinputstring (String FileName) throws ioexception{string result = Null File File = new file (FileName); try {fileinputstream isfile = new FileInputStream (file); byte[] B = new BYTE[ISFILE.AVAILABL E ()];isfile.read (b); result = new String (b); System.out.print ("Read succeeded:" + result);} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return result;} Use Bufferread to read file public string Filebufferread (String FileName) throws IOException {string result = Null;try {Bufferedre Ader Breader = new BufferedReader (new FileReader (FileName)); String oneline = ""; StringBuffer sb = new stringbuffer (); while ((Oneline = Breader.readline ()) = null) {sb.append (oneline);} result = Sb.tostring (); Breader.close (); System.out.println ("read succeeded");} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return result;}
Writing files to the SD card
//write to SD card//Use FileOutputStream to write to file public Boolean writesdfile (String FileName, String content) {Boolean result = false;try {File File = new file (Environment.getexternalstoragedirectory (), FileName);// Get output stream FileOutputStream fos = new FileOutputStream (file); Fos.write (Content.getbytes ()); Fos.close (); System.out.println ("Write succeeded:"); result = true;} catch (Exception e) {e.printstacktrace ();} return result;} Use Buffread to write SD card public Boolean bufferwritefile (string FileName, String content) {Boolean result = false;try {File File = N EW File (Environment.getexternalstoragedirectory (), FileName);//The second parameter meaning whether to add content in Append mode bufferedwriter bw = new BufferedWriter (New FileWriter (file, True)), bw.write (content); Bw.flush (); System.out.println ("write success"); result = true;} catch (Exception e) {e.printstacktrace ();} return result;}
The above content is purely oneself practice to play! Basically, it's all reference http://blog.csdn.net/mad1989/article/details/37568667.
Android Tour-Start