In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to carry out data storage and analysis in android. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
There are mainly the following ways to store data in Android:
1. Use SharedPreferences: this storage method is mainly used for applications to have a small amount of data to save, and the format of the data is very simple.
2. File storage: allows us to easily access files on mobile phone memory (mobile memory card or user's SD card)
3. SQLite database: Android system integrates a lightweight database.
4. Network: save the data on the network platform for storage
Here I will show you how to use them:
1. With SharedPreferences, we need to obtain two important objects, SharedPreferences and SharedPreferences.Editor, and then we can add or extract the corresponding data:
1) add data:
/ / get the SharedPreferences instance, indicating that the SharedPreferences can only be read and written by this application / / MODE_WORLD_READABLE: can only be read by other programs, but cannot be written / / MODE_WORLD_WRITEABLE: can be read and written by other applications SharedPreferences preferences=getSharedPreferences ("myFile", MODE_PRIVATE); SharedPreferences.Editor editor=preferences.edit () / / get the SharedPreferences.Editor object SimpleDateFormat sdf=new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss"); / / store the specified key data editor.putString ("time", sdf.format (new Date () into SharedPreferences through editor Editor.putInt ("random", (int) (Math.random () * 100)); editor.commit (); / / submit the stored data
2) take out the corresponding data:
SharedPreferences preferences=getSharedPreferences ("myFile", MODE_PRIVATE); / / gets the value of the specified key. If it does not exist, the default value String time=preferences.getString ("time", null); int random=preferences.getInt ("random", 0); String result=time==null? " You haven't written the data yet ":" write time is: "+ time+"\ nLast generated random number is: "+ random; Toast.makeText (SharedPreferencesTestActivity.this, result, 5000). Show ()
The above code has comments, I believe it is easy for you to understand!
2. File write and read
In android, we can write and read files in the memory card of our mobile phone or in the SD card inserted by the user.
In essence, it is to establish an input and output stream, and then read and write data in it, which is the IO operation in java!
1) store the data in the built-in storage space of our phones
/ * this method stores the data in the built-in storage space of our phone. The main method is to call the Context.openFileOutput (fileName,MODE) method to get the corresponding output stream * fileName: we cannot use our commonly used delimiter ("/"), for example: aaa/test.txt, we can only use test.txt * to put the saved files in the / data/data/ package name / files/ directory For example: / data/data/com.xin.activity/files/test.txt * MODE: there are the following: * context.MODE_PRIVATE: private data under the current application, other applications cannot read or write access, otherwise there will be a permission denni error, * and if the file already exists, the rewritten file will overwrite the data in the previous file * context.MODE_APPEND: private data under the current application, which cannot be read or written by other applications, otherwise a permission denni error will occur. * and if the file already exists, the rewritten data will be appended to the data of the source file. * context.MODE_WORLD_READABLE: other applications can read the file, but cannot write, otherwise there will be a permission denni error; * context.MODE_WORLD_WRITEABLE: other applications can write to the file, but not read it, otherwise a permission denni error will occur * if you want to enable other applications to both read and write to the file You can use * context.MODE_WORLD_READABLE+context.MODE_WORLD_WRITEABLE to pass parameters * the saved file will not garble in Chinese * We can view the file in the system * @ param fileName * @ param fileContent * @ throws Exception * / public void save (String fileName) via window-- > android-- > file explorer. String fileContent) throws Exception {FileOutputStream fos=context.openFileOutput (fileName, context.MODE_WORLD_WRITEABLE) Fos.write (fileContent.getBytes ());}
2) read data in the built-in storage space of the phone
/ * read data from the storage space built into the phone, mainly by calling the context.openFileInput (fileName) method, passing in the parameter file name * @ return * / public String read (String fileName) throws Exception {FileInputStream fis=context.openFileInput (fileName); byte [] buffer=new byte [1024]; int len=0 / / keep it in memory, or output ByteArrayOutputStream baos=new ByteArrayOutputStream (); / / StringBuilder sb=new StringBuilder (); while ((len=fis.read (buffer))! =-1) {baos.write (buffer,0,len); / / sb.append (new String (buffer,0,len)) } byte [] b=baos.toByteArray (); return new String (b); / / return sb.toString ();}
3) Save the file to the external storage device SD card of the phone
/ * Save the file to the external storage device SD card of the phone * to access the hardware device, we need to configure the corresponding permission to access the SD card in the AndroidMainifest.xml * Note: due to the different version of the android system, the root directory of the external storage device may be different. For example, version 2.1 / sdcard/, and version 2.2 / mnt/sdcard/, so we can't write it dead, use Environment.getExternalStorageDirectory () to get different versions of the root directory, so in this way if the file already exists The content will cover * @ param fileName * @ param fileContent * @ throws Exception * / public void saveToSDCard (String fileName,String fileContent) throws Exception {/ / Environment.getExternalStorageDirectory ()-- > / sdcard File file=new File (Environment.getExternalStorageDirectory (), fileName) FileOutputStream fos=new FileOutputStream (file); fos.write (fileContent.getBytes ());}
4) read out the contents of the SD card file
/ * read out the contents of the SD card file * @ param fileName * @ return * @ throws Exception * / public String readSDCard (String fileName) throws Exception {File file=new File (Environment.getExternalStorageDirectory () + File.separator+fileName); FileInputStream fis=new FileInputStream (file); byte [] buffer=new byte [1024]; int len=0 ByteArrayOutputStream baos=new ByteArrayOutputStream (); while ((len=fis.read (buffer))! =-1) {baos.write (buffer, 0, len);} byte [] b=baos.toByteArray (); return new String (b);}
The above code has comments, I believe it is easy for you to understand! The file from the phone's own memory card to read and write is mainly used in android to provide the current context of the two classes Context openFileInput and openFileOutput to get the corresponding byte input stream and byte output stream, while the operation of the SD card above the file is mainly to get the SD card where the file path, we through Environment.getExternalStorageDirectory () to get its root directory, and then the files inside the corresponding IO stream operation can be, file storage is not so easy!
On how to carry out android data storage analysis to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.