I. File Methods
Directly run the Code:
Package COM. fileop. demo. service; import Java. io. bytearrayoutputstream; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import android. content. context; import android. OS. environment;/***** business class * @ author sky **/public class fileservice {private context; Public fileservice (context) {This. context = context;}/*** save the file in private mode * and save it to/data/application package name/files/FI. Lename * @ Param filename * @ Param content * @ throws exception */Public void save (string filename, string content) throws exception {/* context. mode_private is the default operation mode context. mode_append is also private data, but the file context can be appended. mode_world_readable indicates that the current file can be read by other applications. mode_world_writeable indicates that the current file can be written by other applications. If you want the file to be read and written by other applications, You can input openfileoutput (filename, context. mode_world_readable + context. mode_world_writeable); */File Outputstream outstream = context. openfileoutput (filename, context. mode_private); outstream. write (content. getbytes (); outstream. close ();}/*** Read File * @ Param filename * @ return * @ throws exception */Public String readfile (string filename) throws exception {fileinputstream instream = context. openfileinput (filename); byte [] buffer = new byte [1024]; int Len = 0; bytearrayoutputstream outstream = new bytearr Ayoutputstream (); While (LEN = instream. Read (buffer ))! =-1) {outstream. write (buffer, 0, Len);} byte [] DATA = outstream. tobytearray (); outstream. close (); instream. close (); return new string (data);}/*** save the file to sdcard * permission to wear and delete files in sdcard: * <uses-Permission Android: name = "Android: Name =" android. permission. mount_unmount_filesystems "/> * permission to write data to sdcard: * <uses-Permission Android: Name =" Android: Name = "android. permission. write_external_storage "/> * @ Param filename * @ Param content */Public void savetosdcard (string filename, string content) throws exception {file = new file (environment. getexternalstoragedirectory (), filename); fileoutputstream outstream = new fileoutputstream (File); outstream. write (content. getbytes (); outstream. close ();}/*** determines whether the sdcard exists on the mobile phone, and no write protection * @ return true is available; false is unavailable */Public Boolean issdcardavailable () {If (environment. getexternalstoragedirectory (). equals (environment. media_mounted) {return true;} return false ;}}
Ii. sharedpreferences
Package COM. fileop. demo; import android. app. activity; import android. content. context; import android. content. sharedpreferences; import android. content. sharedpreferences. editor; import android. OS. bundle; public class sharedpreferencesdemo extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); sharedpreferences sp = This. getsharedpreferences ("FILENAME", context. mode_private); Editor editor = sp. edit (); Editor. putint ("Age", 12); Editor. putstring ("name", "sky"); Editor. putboolean ("sex", true); Editor. putfloat ("money", 11.20f); Editor. putlong ("maxlife", 0000000000l); // call commit () the previous data is stored in the memory // stored in the editor under/data/application package name/shared_prefs/after the call. commit (); // read the sharedpreferences data int age = sp. getint ("Age", 0); string name = sp. getstring ("name", ""); Boolean sex = sp. getboolean ("sex", true); float money = sp. getfloat ("money", 0.00f); long maxlife = sp. getlong ("maxlife", 999999999999l );}}
Iii. Database
Package COM. DB. demo; import android. content. context; import android. database. SQLite. sqlitedatabase; import android. database. SQLite. sqlitedatabase. cursorfactory; import android. database. SQLite. sqliteopenhelper; public class dbopenhelper extends sqliteopenhelper {Private Static final string db_name = "demo. DB "; Private Static final int db_version = 1; Public dbopenhelper (context) {super (context, db_name, null, db_version) ;}@ overridepublic void oncreate (sqlitedatabase dB) {// The database is called when it is created for the first time. Therefore, string SQL = "CREATE TABLE person (personid integer primary key autoincrement, name varchar (20) is created here )) "mongodb.exe csql (SQL);} @ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// call when the database version is changed }}
Package COM. DB. demo; import Java. util. arraylist; import Java. util. list; import android. content. context; import android. database. cursor; import android. database. SQLite. sqlitedatabase; public class personservice {private dbopenhelper; Public personservice (context) {dbopenhelper = new dbopenhelper (context);} public void insert (person) {// if you want to change the data, call the getwritabledatabase () method to obtain the operation Database instance. This method enables the sqlitedatabase DB = dbopenhelper.getwritabledatabase(mongomongodb.exe csql ("insert into person (name) values (?) ", New object [] {person. getname ()});} public void Update (person) {sqlitedatabase DB = dbopenhelper.getwritabledatabase(#mongodb.exe csql (" Update person set name =? Where person ", new object [] {person. getname (), person. GETID ()});} public void Delete (integer ID) {sqlitedatabase DB = dbopenhelper.getwritabledatabase(mongomongodb.exe csql ("delete from person where personid =? ", New object [] {ID. tostring ()});} public person find (integer ID) {// if you only want to read data, we recommend that you use getreadabledatabase () sqlitedatabase DB = dbopenhelper. getreadabledatabase (); cursor = dB. rawquery ("select * From person where personid =? ", New string [] {ID. tostring ()}); If (cursor. movetofirst () {int personid = cursor. getint (cursor. getcolumnindex ("personid"); string name = cursor. getstring (cursor. getcolumnindex ("name"); return new person (personid, name);} return NULL;} public list <person> getscrolldata (integer offset, integer maxresult) {list <person> Persons = new arraylist <person> (); sqlitedatabase DB = dbopenhelper. getreadabledata Base (); cursor = dB. rawquery ("select * From person limit ?,? ", New string [] {offset. tostring (), maxresult. tostring ()}); While (cursor. movetonext () {int personid = cursor. getint (cursor. getcolumnindex ("personid"); string name = cursor. getstring (cursor. getcolumnindex ("name"); person = new person (personid, name); persons. add (person) ;}return persons;} public long getcount () {sqlitedatabase DB = dbopenhelper. getreadabledatabase (); cursor = dB. rawquery ("select count (*) from person", null); // there must be a record, so if is not used to judge cursor. movetofirst (); Return cursor. getlong (0 );}}
Transaction operations:
Public void payment () {sqlitedatabase DB = dbopenhelper. getwritabledatabase (); dB. begintransaction (); // Affairs Notice try mongodb.exe csql ("Update person set amount = amount-10 where personid =? ", New objectprofileid %1}mongodb.exe csql (" Update person set amount = Amount + 10 where personid =? ", New object [] {2}); dB. settransactionsuccessful (); // set the transaction flag to successful. When the transaction ends, the transaction will be committed} finally {dB. endtransaction (); // end transaction }}
To be updated ........