Write to File
String content1 = Edt_file.gettext (). toString ();
Write Operations for files
FileOutputStream Fos=null;
Buffered input stream
BufferedWriter writer = null;
try {
If the file exists, open the file directly, if it does not exist, create the file
Openfileoutput (String name,int mode) method: Used to output the file to the current application folder and return the FileOutputStream output stream.
FOS = Openfileoutput ("test.txt", context.mode_private);
The OutputStreamWriter function is the association of buffered input streams with file write operations
writer = new BufferedWriter (new OutputStreamWriter (FOS));
The original bufferedwriter is a buffered input stream, meaning when you call BufferedWriter's Write method.
The data is written to the buffer and is not written directly to the destination file. You must call the Flush () method of BufferedWriter.
This method refreshes the buffer stream, which is where the data is written to the destination file. Or you can call BufferedWriter's close () method,
This method refreshes the buffered stream before closing the input stream. It also writes the data to the destination file.
Writer.write (CONTENT1); Write buffers
Writer.flush (); Must be written, otherwise it will not be saved in the file, only in the buffer.
Toast.maketext (Sharedperferencesactivity.this, "Write file Complete", Toast.length_long). Show ();
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Finally
{
try {
if (writer! = null)
{
Writer.close (); Close buffered input stream
}
if (fos! = null)
{
Fos.close (); Turn off write operations
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Read the file
Buffered output stream
BufferedReader Reader=null;
StringBuilder sb = new StringBuilder ();
try {
Openfileinput (String name) method: Used to read the file under the current Application folder and return the FileInputStream input stream.
reader = new BufferedReader (New InputStreamReader (Openfileinput ("test.txt"));
String Content2 = Reader.readline ();//Read travel data
while (Content2! = null)//loop-read data
{
Sb.append (Content2);
Content2 = Reader.readline ();
}
Edt_file.settext (Sb.tostring ());
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Finally
{
try {
if (reader! = null)//close buffered output stream data
{
Reader.close ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Raw resource reads:
The original resource file is located in the Res/raw directory, and the program is fully packaged into the APK when it is deployed, accessible through the resource ID in the program.
InputStream is = Getresources (). Openrawresource (R.raw.rawtest);
BufferedReader Reader2 =null;
StringBuffer SB2 = new StringBuffer ();
try {
Reader2 = new BufferedReader (new InputStreamReader (IS, "GBK"));
String Content2 = Reader2.readline ();
while (Content2! = null)
{
Sb2.append (Content2);
Content2 = Reader2.readline ();
}
Edt_file.settext (Sb2.tostring ());
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Finally
{
try {
if (reader2! = null)
{
Reader2.close ();
}
if (is = null)
{
Is.close ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Write to SD card
1. Determine the SD status
FileOutputStream fos1 = null;
try {
String statue = environment.getexternalstoragestate ();
if (Statue.equals (environment.media_mounted))
{
SD card available
Toast.maketext (sharedperferencesactivity.this, "current phone's SD card available ...", Toast.length_long). Show ();
Get the path
File dir = environment.getexternalstoragedirectory ();
Toast.maketext (Sharedperferencesactivity.this, Dir.getcanonicalpath (), Toast.length_long). Show ();
File File = new file (dir, "test.txt");
FOS1 = new FileOutputStream (file);
Write (FOS1);
}
Else
{
Toast.maketext (Sharedperferencesactivity.this, "there is a problem with the current phone's SD card ...", Toast.length_long). Show ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Finally
{
try {
if (fos1!=null)
{
Fos1.close ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
SD-Readable
1. Determine the SD status
FileInputStream FIS = null;
try {
String statue1 = Environment.getexternalstoragestate ();
if (Statue1.equals (environment.media_mounted))
{
SD card available
Toast.maketext (sharedperferencesactivity.this, "current phone's SD card available ...", Toast.length_long). Show ();
InputStream
File dir2= environment.getexternalstoragedirectory ();
File File2 = new file (Dir2, "test.txt");
FIS = new FileInputStream (file2);
Read (FIS);
}
Else
{
Toast.maketext (Sharedperferencesactivity.this, "there is a problem with the current phone's SD card ...", Toast.length_long). Show ();
}
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Finally
{
try {
if (FIS! = null)
{
Fis.close ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
File storage for Android