Follow me to learn Android 12 file parsing and processing

Source: Internet
Author: User

Chapter Content


1th section File Explorer Operations

2nd Section Sharedpreferences

Section 3rd ordinary file operation

4th section SD card read and write operation

This chapter aims

Master of Skills FileExplorer usage of

Master of Skills sharedpreference file operations.

Proficiency in reading and writing of common files.

Master of Skills SD the method of card read and write operation.


FileExplorer operation

view file structure

Create folder

guide Span style= "font-family: Arial" > Enter file

export files and folders

Span style= "font-family: Arial" > delete file


sharedpreferences Overview

Sharedpreferences is primarily used to hold content similar to configuration information, Sharedpreferences saves data in XML format, and is stored in the/data/data/<package>/shared_prefs directory. Similar to the information in properties, mainly key-value pairs

Read Sharedpreferences

First through the context. Getsharedpreferences method gets object

The first parameter is a file name and needs to include the suffix name (automatically set to XML)

The second parameter is access mode, same as normal file access mode

through The method in sharedpreferences reads data


Sharedpreferences sp = getsharedpreferences("config",

Context.mode_private);

Stringusername = sp.getString("username", "root");

Stringpassword = sp.getString("Password", "123456");


Write Sharedpreferences

First through the context. Getsharedpreferences method to get the object

Open with write mode when getting objects

Get editor objects through Sharedpreferences

The Editor object contains methods for writing data

Be sure to execute the commit method after the data is written to make the data effective


Sharedpreferences sp = getsharedpreferences("config",

Context.mode_private);

Editor Editor = Sp.edit ();

Editor.put ("username", "root");

Editor.put ("Password", "123456");

Editor.commit ();

Pe:solid;mso-style-textfill-fill-themecolor:text1;mso-style-textfill-fill-color:black; mso-style-textfill-fill-alpha:100.0% ' > method to make Data effective

External Access sharedpreferences

Access another app's sharedpreferences data through the context in one app. Createpackagecontext creating an instance of a context the first parameter is another app's package name the second parameter is the option flag context_include_codecontext_ignore_ Security accesses sharedpreferences through the established context object

Contextcontext = Createpackagecontext(Pkgname,

context_ignore_security);

Sharedpreferences cfg = context. getsharedpreferences (

Preference_name, MODE);


Access sharedpreferences data for another app in one appuSeveral points of attention on permissions?The content of the android:shareduserid of two apps should be the same?Both use mode_world_readable or mode_world_writeable mode to read and write content

Use Openfileinput Read File

Using Openfileinput to read filesuThis is one of the ways in the context?Ability to open a file input stream from an application-related pathuFile location?/data/data/<package>/filesuThe return value is an FileInputStream object?This is a file input byte stream

Using Openfileinput to read filesuAn example of reading a file

FileInputStream InputStream = this. Openfileinput (FileName);

Byte[]bytes = new byte[1024];

Bytearrayoutputstream os= new Bytearrayoutputstream ();

while (InputStream. Read(bytes)! =-1) {

Os.write (bytes, 0, bytes.length);

}

Inputstream.close ();

Os.close ();

Stringcontent = new String (Os.tobytearray ());

Showtextview.settext (content);


Writing files using the Openfileoutput methoduAutomatically created when a file does not existuThe second parameter of the method is open mode?Mode_private can only create its app access, overwriting files when repeated writes?Mode_append private access, which is appended at the end of the file when repeated writes?Mode_world_readable Common mode, readable?Mode_world_writeable Common mode, can read and writeuIt is generally recommended to use private mode?Working with files in common mode is dangerous, because once concurrency brings bugs to the program

Writing files using the Openfileoutput methoduWrite File Example

FileOutputStream outputstream = openfileoutput(FileName,

Activity.mode_private);

Outputstream.write (Content.getbytes ());

Outputstream.flush ();

Outputstream.close ();

Rational use of cachinguRead and write files are access to the storage device?Low rate of input and output?Performance is impacted when accessed frequentlyuUse cache submission Efficiency appropriately?Read into memory the contents of a file that require frequent access?Operation of data in memory?Write files periodically or when needed?Reduce the number of file input and outputuHowever, the cache cannot be too large to consume too many resources to cause degraded system performance browse files on SD carduSD card is usually the extended storage of the phoneuMount to the mobile OS under the file system/mnt/sdcard/uThe directory on the SD card is writable for all applicationsuUse the file class to browse the contents of the directory on the SD card mso-color-index:1;mso-font-kerning:12.0pt;language:zh-cn;mso-style-textfill-type:solid; Mso-style-textfill-fill-themecolor:text1;mso-style-textfill-fill-color:black;mso-style-textfill-fill-alpha : Operating files in 100.0% ' > Common mode is dangerous, because if concurrency brings a bug in the program

Filedir = new File ("/mnt/sdcard");

String[]s = dir.list ();

Read the files on the SD carduUse FileInputStream or FileReader for file reading


FileInputStream in = new FileInputStream ("/mnt/sdcard/...");  byte[] bytes = new byte[1024];  Bytearrayoutputstream os= new Bytearrayoutputstream ();  while (In.read (bytes)! =-1) {          os.write (bytes, 0, bytes.length);  }  In.close ();  Os.close ();  String content = new String (Os.tobytearray ());  Showtextview.settext (content);


Save the file to an SD carduFile write using FileOutputStream or FileWriter
FileOutputStream out = new FileOutputStream ("/mnt/sdcard/..");  Out.write (Content.getbytes ());  Out.flush ();  Out.close ();


Follow me to learn Android 12 file parsing and processing

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.