Read and write files in Android
There is only one disk in Android, a forward slash/root directory.
The location of our Common SDK is:/mnt/sdcard
Two of the most common ways to store data:
One, memory
Second, local
1. Internal phone storage
2. External storage device (SD card)
It is not necessary to read data in SD card, but to write data in SD card is to be privileged:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
The way to read and write files is to use the Java file input stream and output stream, and read and write files in Java almost exactly the same way.
Code:
Com.example.readwrite.MainActivity
1 PackageCom.example.readwrite;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 8 Importandroid.app.Activity;9 ImportAndroid.os.Bundle;Ten Importandroid.os.Environment; One ImportAndroid.util.Log; A - /** - * Forward slash represents the root directory two of the most common data storage methods the * - * One, memory two, local 1. Phone internal storage 2. External storage device (SD card) - * */ - Public classMainactivityextendsActivity { + - @Override + protected voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.activity_main); - //Existsdcard (); - //write (); - //Listpath (); - read (); - } in - Private voidwrite () { to ///mnt/sdcard +File File =environment.getexternalstoragedirectory (); -FileOutputStream out =NULL; the Try { *out =NewFileOutputStream (File.getpath () + "/bihu.txt"); $ //out = new FileOutputStream (Panax Notoginseng //"/data/data/com.example.readwrite/bihu.txt"); -Out.write ("12345". GetBytes ()); the}Catch(IOException e) { + e.printstacktrace (); A}finally { the if(Out! =NULL) { + Try { - out.close (); $}Catch(IOException e) { $ //TODO auto-generated Catch block - e.printstacktrace (); - } the } - }Wuyi } the - Private voidRead () { WuFileInputStream in =NULL; - Try { About //In = new FileInputStream ("/mnt/sdcard/bihu.txt"); $in =NewFileInputStream ( -"/data/data/com.jiguang.test/databases/rep.db"); - byte[] bytes =New byte[2014]; - intLen =in.read (bytes); AString str =NewString (Bytes, 0, Len); +LOG.D ("Bihu", "---------" +str); the}Catch(IOException e) { -LOG.D ("Bihu", "error" +e.tostring ()); $}finally { the if(In! =NULL) { the Try { the in.close (); the}Catch(IOException e) { - e.printstacktrace (); in } the } the } About } the the /** the * Check to see if the SD card is mounted + * */ - Private voidExistsdcard () { the //get the status of an SD cardBayiString State =environment.getexternalstoragestate (); the the if(Environment.MEDIA_MOUNTED.equals (state)) { -LOG.D ("Bihu", "SD card"); -}Else { theLOG.D ("Bihu", "No SD card"); the } the } the - /** the * Get path through API the * */ the Private voidListpath () {94 //get the SD card directory theFile file1 =environment.getexternalstoragedirectory (); theLOG.D ("Bihu", "SD card----" +File1.getpath ()); the //get the file directory for your phone's internal storage space98File file2 =Getfilesdir (); AboutLOG.D ("Bihu", "Internal Store file----" +File2.getpath ()); - //gets the cache directory for the internal storage space101File File3 =Getcachedir ();102LOG.D ("Bihu", "Internal storage cache Directory----" +File3.getpath ());103 }104}
Main interface
/read/write local file/androidmanifest.xml
1<?xml version= "1.0" encoding= "Utf-8"?>2<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"3 Package= "Com.example.readwrite"4Android:versioncode= "1"5Android:versionname= "1.0" >6 7<!--permissions written on an external device (SD card)--8<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>9 Ten<uses-SDK OneAndroid:minsdkversion= "8" AAndroid:targetsdkversion= "/>" - -<Application theAndroid:allowbackup= "true" -android:icon= "@drawable/ic_launcher" -Android:label= "@string/app_name" -Android:theme= "@style/apptheme" > +<Activity -Android:name= ". Mainactivity " +Android:label= "@string/app_name" > A<intent-filter> at<action android:name= "Android.intent.action.MAIN"/> - -<category android:name= "Android.intent.category.LAUNCHER"/> -</intent-filter> -</activity> -</application> in -</manifest>
configuration file
Read and write files in Android