Chapter 5 data-centric-Data Access (1), Chapter 5 Access

Source: Internet
Author: User
Tags sqlite database

Chapter 5 data-centric-Data Access (1), Chapter 5 Access
Chapter 5 data-centric-Data Access

A typical desktop operating system provides a public file system that any application can use to store and read files, this file can also be read by other application software (with some permission control settings ). Android uses a different system. In Android, all application software data (including files) is private to the application software. However, Android also provides a standard way to use software to open private data to other applications. This section describes how an application stores and obtains data, opens data to other applications, requests data from other applications, and opens them up.

Available storage methods include file storage, SQLite database, SharedPreferences, and content provider ). We will introduce it in detail in this chapter.

5.1 File Operations 5.1.1 read and write general text files

Let's take a look at the common Java APIs for file or folder operations:

String path = File. getPath (); // relative path

String path = File. getAbsoultePath (); // absolute path

String parentPath = File. getParent (); // obtain the parent directory of a File or folder

String Name = File. getName ();

File. mkDir (); // create a folder

File. createNewFile (); // create a File

File. isDirectory (); // identifies a File or folder

File [] files = File. listFiles (); // list all files and folder names in the folder

File. renameTo (dest); // modify the folder and File name

File. delete (); // delete a folder or File

 

In Android, files under the SD card are often operated:

Environment. getExternalStorageState (). equals (android. OS. Environment. MEDIA_MOUNTED );

// Determine whether the SD card is inserted

File skRoot = Environment. getExternalStorageDirectory (); // get the root directory of the SD card

File fileRoot = Context. getFilesDir () + "//"; // obtain the private root directory

 

Similar to the traditional Java program that implements I/O, in Android, it provides the openFileInput and openFileOuput methods to read files on the device.

The example code is as follows:

String FILE_NAME = "tempfile. tmp"; // determine the file name to operate on

FileOutputStream fos = openFileOutput (FILE_NAME, Context. MODE_PRIVATE); // Initialization

FileInputStream FCM = openFileInput (FILE_NAME); // create a write stream

 

Experience Sharing:

By default, files created using the openFileOutput () method can only be used by the called applications. Other applications cannot read the files. If you need to share data in different applications, you can use Content Provider. We will introduce Content Provider later.

For Android files, you must have the following permissions:

<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>

 

In Android development, where are resource files generally stored?

If your application requires some additional resource files, such as MP3 files used to test whether the music player you write works properly, you can put these files under/res/raw/of the application, for example, mydatafileworkflow. In this case, you can use getResources in your application to obtain resources and open the file using the openRawResource method (resource file name without a suffix). The implementation code is as follows:

Resources myResources = getResources ();

InputStream myFile = myResources. openRawResource (R. raw. myfilename );

---------------------------------------- It is difficult for programmers to make money. Therefore, they must learn financial management. The annual ROI of the p2p platform affiliated to Ping An group is 7%-9%. This is the preferred personal experience to replace bank financial management. We recommend investing in don't invest in security, which is almost impossible to transfer. It is very difficult to withdraw the website link in advance. Don't make it in white --------------------------------------------

Next let's take a look at the next example to read the resource file test.txt under the/res/raw/directory.

// Import omitted

Public class ReadTextTest extends Activity {

 

Public static final int REFRESH = 0x000001;

Private TextView text = null;

 

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

Text = new TextView (this );

Text. setBackgroundColor (0xff000000 );

Text. setTextColor (0 xffffffff );

Text. setGravity (Gravity. CENTER );

InputStreamReader inputStreamReader = null;

InputStream inputStream = getResources (). openRawResource (R. raw. test );

Try {

InputStreamReader = new InputStreamReader (inputStream, "UTF-8 ");

} Catch (UnsupportedEncodingException e1 ){

E1.printStackTrace ();

}

BufferedReader reader = new BufferedReader (inputStreamReader );

StringBuffer sb = new StringBuffer ("");

String line;

Try {

While (line = reader. readLine ())! = Null ){

Sb. append (line );

Sb. append ("\ n ");

}

} Catch (IOException e ){

E. printStackTrace ();

}

Text. setText (sb. toString ());

SetContentView (text );

}

}

 

Create a new test.txt file under/res/raw/directory, add the "test read txt file" text, save as UTF-8 format.

Figure 5-1 shows the running result:

Figure 5-1 result of reading txt document

Related Article

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.