Reading and Writing files in Android

Source: Internet
Author: User

As we all know, Android has its own security model. For details, see the android development documentation. When the application (.apk) is installed, a userid is assigned,

When the application wants to access other resources, such as files, it needs to match the userid.
By default, files, databases, and sharedpreferences created by any application must be private (in/data/your_project/files /),

The default data path of the program is/data/your_project/files /;

Other programs cannot be accessed. Unless mode_world_readable or mode_world_writeable is specified during creation, the other programs can access it correctly only in this way.
Android provides a security system for reading and writing files (Android requires that the user ID of the process be checked when a process opens a private file );

Therefore, we cannot directly use Java APIs to open private files of programs. For example:

1. filereader file = new filereader ("android.txt"); the above Code cannot be successfully executed.
Private Data is particularly emphasized here! The implication is that if a file or data is not private to the program, and you do not need to go through the android permission check when accessing it, you can still directly access it using Java I/O APIs. The so-called non-private data is only files or data stored on sdcard,

You can use Java I/O APIs to directly open files on sdcard.

1. filereader file = new filereader ("/sdcard/android.txt ");
To open your own private files and data, you must use the openfileoutput and openfileinput methods provided by activity.

To create a private file of a program, you must use the file operation method provided by activity due to permission requirements.
1. fileoutputstream OS = This. openfileoutput ("android.txt", mode_private );

2. outputstreamwriter outwriter = new outputstreamwriter (OS );

To read private files of a program, you must use the file operation method provided by activity due to permission requirements. 1. fileinputstream OS = This. openfileinput ("android.txt ");

2. inputstreamreader inreader = new inputstreamreader (OS );
Speed up file read/write
The principle is to read some data in the file to the buffer.

The advantage is that if the read content is already in the buffer, the buffered data is read.

If no, the buffer reads data from the file first and then the data is read from the buffer.
The same is true for write operations.

This reduces the number of file operations to improve performance.

The disadvantage is that extra memory is used as the buffer.
The following method is used for fileinputstream and fileoutputstream.
Bufferedinputstream Buf = new bufferedinputstream (New fileinputstream ("file. Java "));

Bufferedoutputstream Buf = new bufferedoutputstream (New fileoutputstream ("file. Java "));
Use the following method for filereader and filereader:
Bufferedreader Buf = new bufferedreader (New filereader ("file. Java "));

Bufferedwriter Buf = new bufferedwriter (New filewriter ("file. Java "));
For write operations, it is best to add flush ().

Instance 1:

Void write2file ()

{

String content = "Hello:" + system. currenttimemillis ();

Fileoutputstream OS = NULL;

String filename = "hubin.txt ";

Int mode = context. mode_world_writeable | context. mode_world_readable;

Try {
OS = openfileoutput (filename, mode );

OS. Write (content. getbytes ());
/* Outputstreamwriter outwriter = new outputstreamwriter (OS );

Outwriter. Write (content );*/

Log. I (TAG, "write:" + content );

} Catch (filenotfoundexception E)

{

Log. E (TAG, "createfile:", e );

}

Catch (ioexception E)

{

Log. E (TAG, "Write File", e );

}

Finally

{

If (OS! = NULL)

{

Try {
OS. Flush ();

OS. Close ();

} Catch (ioexception E)

{

Log. E (TAG, "Close file", e );

}

}

}



}

Void readfromfile ()

{

String content;

Fileinputstream is = NULL;

String filename = "hubin.txt ";

Try {
Is = openfileinput (filename );

Byte buffer [] = new byte [is. Available ()];

Is. Read (buffer );

Content = new string (buffer );

Log. I (TAG, "read:" + content );

// Inputstreamreader inreader = new inputstreamreader (is );

} Catch (filenotfoundexception E)

{

Log. E (TAG, "createfile:", e );

}

Catch (ioexception E)

{

Log. E (TAG, "Write File", e );

}

Finally

{

If (is! = NULL)

{

Try {

Is. Close ();

} Catch (ioexception E)

{

Log. E (TAG, "Close file", e );

}

}

}

}
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.