SP, file and sdcard Storage

Source: Internet
Author: User

XML:

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: Orientation = "vertical">

<Button
Android: Id = "@ + ID/btnwrite"
Android: layout_width = "match_parent"
Android: layout_margintop = "20dp"
Android: layout_height = "wrap_content"
Android: text = "SP storage"/>

<Button
Android: Id = "@ + ID/btnread"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "SP read"/>

<Button
Android: Id = "@ + ID/btnfilewrite"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "file storage"/>

<Button
Android: Id = "@ + ID/btnfileread"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "File Reading"/>

<Button
Android: Id = "@ + ID/btnfilewritetosd"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "sdcard storage"/>

<Button
Android: Id = "@ + ID/btnfilereadtosd"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "sdcard read"/>
</Linearlayout>

 

Activity:

Public class extends activity implements onclicklistener {

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. sharepreference );
Findviewbyid (R. Id. btnwrite). setonclicklistener (this );
Findviewbyid (R. Id. btnread). setonclicklistener (this );
Findviewbyid (R. Id. btnfilewritetosd). setonclicklistener (this );
Findviewbyid (R. Id. btnfilewrite). setonclicklistener (this );
Findviewbyid (R. Id. btnfileread). setonclicklistener (this );
Findviewbyid (R. Id. btnfilereadtosd). setonclicklistener (this );
}
@ Override
Public void onclick (view v ){
Switch (V. GETID ()){
Case R. Id. btnwrite:
Sharedpreferences sp = getsharedpreferences ("setting ",
Context. mode_private );
Sharedpreferences. Editor editor = sp. Edit ();
Editor. putstring ("name", "James ");
Editor. putint ("Age", 25 );
Editor. putint ("weight", 120 );
Editor. Commit ();
Break;
Case R. Id. btnread:
Sharedpreferences spread = getsharedpreferences ("setting ",
Context. mode_private );
String name = spread. getstring ("name", "null ");
Int age = spread. getint ("Age", 18 );
Int Weight = spread. getint ("weight", 15 );
Toast. maketext (this, name + "--" + age + "--" + weight,
Toast. length_long). Show ();
Break;
Case R. Id. btnfilewrite:
Writefile ();
Break;
Case R. Id. btnfileread:
Readfile ();
Break;
Case R. Id. btnfilewritetosd:
Writefilestosdcard ();
Break;
Case R. Id. btnfilereadtosd:
Readfilesfromsdcard ();
Break;
}

}
// Write an object
Public void writefile (){
Fileoutputstream OS = NULL;
Try {
OS = This. openfileoutput ("jerei.txt", context. mode_private );
OS. Write ("Name: James". getbytes ());
OS. Write ("Age: 25". getbytes ());
OS. Write ("Body Weight: 100". getbytes ());
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (OS! = NULL ){
Try {
OS. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
OS = NULL;
}
}

}
// Read the file
Public void readfile (){
Fileinputstream is = NULL;
Stringbuilder sb = new stringbuilder ();
Try {
Is = This. openfileinput ("jerei.txt ");
Byte [] buffer = new byte [1024];
Int Len = 0;
While (LEN = is. Read (buffer ))! =-1 ){
String TMP = new string (buffer, 0, Len );
SB. append (TMP );
}
} Catch (filenotfoundexception e ){

E. printstacktrace ();
} Catch (ioexception e ){

E. printstacktrace ();
} Finally {
If (is! = NULL ){
Try {
Is. Close ();
} Catch (ioexception e ){

E. printstacktrace ();
}
}
}
Toast. maketext (this, SB. tostring (), Toast. length_long). Show ();
}
// Save it to the SD card
Public void writefilestosdcard (){
String filepath = NULL;
If (environment. getexternalstoragestate (). Equals (
Environment. media_mounted )){
// Obtain the sdcard root path
Filepath = environment. getexternalstoragedirectory (). tostring ();
Filepath = filepath + file. Separator + "jerei" + file. Separator
+ "Edu ";
File fileparent = new file (filepath );
If (! Fileparent. exists ()){
Fileparent. mkdirs ();
}
Fileoutputstream OS = NULL;
Try {
OS = new fileoutputstream (new file (fileparent, "a.txt "));
OS. Write ("Write File to sdcard !! ". Getbytes ());
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (OS! = NULL ){
Try {
OS. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}
}
}

// Read the SD card
Public void readfilesfromsdcard (){
Fileinputstream is = NULL;
Stringbuilder sb = new stringbuilder ();
Try {
Is = This. openfileinput ("a.txt ");
Byte [] buffer = new byte [1024];
Int Len = 0;
While (LEN = is. Read (buffer ))! =-1 ){
String TMP = new string (buffer, 0, Len );
SB. append (TMP );
}
} Catch (filenotfoundexception e ){

E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (is! = NULL ){
Try {
Is. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}
Toast. maketext (this, SB. tostring (), Toast. length_long). Show ();
}

}

SP, file and sdcard Storage

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.