File storage (internal storage)
once the program is installed in the device, the data/data/package name/That is the internal storage space, external confidentiality.
The context provides 2 ways to open an input, output stream
- FileInputStream openfileinput (String name)
- FileOutputStream openfileoutput (String name, int mode)
public class Mainactivity extends activity {private TextView show;
Private EditText et;
Private String filename = "Test";
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Show = (TextView) Findviewbyid (r.id.show);
ET = (edittext) Findviewbyid (r.id.et); Findviewbyid (R.id.write). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v)
{try {FileOutputStream fos = openfileoutput (filename, context.mode_private); FileOutputStream is a byte stream, if it is written text, the need to further FileOutputStream packaging UTF-8 is encoded outputstreamwriter OSW = new Outputstreamwrite
R (Fos, "UTF-8");
Write Osw.write (Et.gettext (). toString ());
Osw.flush ();
Fos.flush ();
Osw.close ();
Fos.close ();
catch (FileNotFoundException e) {e.printstacktrace (); catch (UnsupporteDencodingexception e) {//TODO auto-generated catch block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}
}
}); Findviewbyid (R.id.read). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v)
{try {FileInputStream FIS = openfileinput (filename);
When the input and output are specified character set encoding, there will be no garbled situation inputstreamreader ISR = new InputStreamReader (FIS, "UTF-8");
Gets the available length of the file, constructs a character array char[] input = new char[fis.available ()];
Isr.read (input);
Isr.close ();
Fis.close ();
String readed = new string (input);
Show.settext (readed);
catch (FileNotFoundException e) {e.printstacktrace ();
catch (Unsupportedencodingexception e) {//TODO auto-generated catch block E.printstacktrace (); Catch(IOException e)
{//TODO auto-generated catch block E.printstacktrace ();
}
}
});
}
}
Data/data/packagename/files/test is the file we write.
SD storage (External storage)
The Mnt/sdcard directory is the mount point of the SD card (just a point).
Storage/sdcard: The real SD card operation directory.
First, File download
Android Development, sometimes need to download some resources from the Internet for users to use, the Android API has provided a lot of directly available classes for everyone to use, general file download requires three steps:
1. Create a HttpURLConnection object
Creates a URL object that contains an IP address, urlstr refers to the network IP address
url = new URL (urlstr);
Create a HttpURLConnection object through the URL object
httpurlconnection urlconn = (httpurlconnection) url.openconnection ();
2. Get a InputStream object
InputStream input = Urlconn.getinputstream ();
3. Set permissions to access the network
Add permission information to the Androidmanifest.xml configuration file
<uses-permission android:name= "Android.permission.INTERNET"/>
Second, access and write SD card
1. To determine whether the cell phone inserted SD card, and the application has read and write permissions
Environment.getexternalstoragestate (). Equals (environment.media_mounted);
2. Get the current SD card directory
Environment.getexternalstoragedirectory ();
3. You must also set permissions in the configuration file before accessing the SD card so that you can access the most SD card
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
The following is a commonly used in the SD operation of the encapsulation class, if the need for the SD card operation, directly can be brought over with
public class FileUtils {private String sdpath;
Public String Getsdpath () {return sdpath; }//constructor, get the directory of SD card, this line function gets the directory name is actually called "/sdcard" public FileUtils () {Sdpath = Environment.getexternalstoragedirectory ()
+"/"; ///create file on SD card public file Createsdfile (String fileName) throws ioexception{file = new file (Sdpath + fileName
);
File.createnewfile ();
return file;
///Create directory Public File createsddir (String dirname) {file dir = new file (Sdpath + dirname) on SD card;
Dir.mkdir ();
return dir;
//Determine if the folder on the SD card exists public boolean isfileexist (String fileName) {File File = new file (Sdpath + fileName);
return file.exists (); ///write the data in a InputStream to the SD card//write the input to the filename file in the directory of path, public file Write2sdfrominput (string path, String fi
Lename, InputStream Input) {file file = null;
OutputStream output = null;
try{createsddir (path);
File = createsdfile (path + fileName); FileInputStream is reading data, FileoUtputstream is written to data, written to file to output = new FileOutputStream (files);
byte buffer [] = new byte[4 * 1024];
while ((Input.read (buffer))!=-1) {output.write (buffer);
} output.flush ();
catch (Exception e) {e.printstacktrace ();
} finally{try{output.close ();
catch (Exception e) {e.printstacktrace ();
} return file;
}
}