Storage of data (2) using file storage

Source: Internet
Author: User
Tags readline

I. Overview of File saving

1. Seat of the file preservation

/data/data/<packagename>/files

2. Method of file operation

Openfileinput ()

Openfileoutput ()

DeleteFile ()

FileList () ....


Two. Create and write files in default table

1. Get an output stream FileOutputStream object

Openfileoutput ("MyFile.txt", context.mode_private);

Streams in Java have to specify the output path, but in Android, Openfileoutput is not, and this is a handy place for Android. The default path will be used, as above.

2. Write data to the stream

Write (Data.getbytes ());

Because in 1 we get the FileOutputStream byte stream, which can only be written in bytes. So we're going to turn the data (string) into a byte class,

Of course, we can also use OutputStreamWriter to turn it into a character stream and then manipulate it.

3.close Flow

Fos.close ()


Three. read files in default location

1. Create an input stream

FileInputStream fis; input byte throttling

InputStreamReader ISR; character streams

BufferedReader BR; Buffered stream that can be read over a section

FIS = openfileinput ("MyFile.txt");

ISR =new InputStreamReader (FIS);

br = new BufferedReader (ISR);

2. Reading data

We can use ReadLine here ()

String str =null;

STR =br.readline ();

3. Close the Stream

Fis.close ();

Isr.close ();

Br.close ();




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.