It is very standard to save data to file and read data from file.

Source: Internet
Author: User

Closed stream: reader (read) writer (write)

 

Byte stream: inputstream (read data) outputstream (write data)

1. byte stream

Inputstream (read), outputstream (write)

2. Streams

Reader and writer)

 

Conclusion: as long as the plain text data is processed, the use of the byte stream should be given priority, in addition to the byte stream.

 

Write content to the file

Try {

Fileoutputstream Fos = openfileoutput (internal_filename, mode_append );

FOS. Write ("Michael \ n". getbytes ());

// The getbytes () method of string is used to convert string => byte []

FOS. Write ("89 \ n". getbytes ());

FOS. Close ();

Toast. maketext (this, "Write successful! ", Toast. length_long). Show ();

} Catch (exception e ){

E. printstacktrace ();

Show. settext ("Write failed! ");

}

 

The saved location of info.txt is data/package name/files/info.txt.

The data/package name/Files directory is used by default to read specific files, as shown below:

Try {

Fileinputstream FCM = openfileinput (internal_filename );

Bufferedreader BR = new bufferedreader (New inputstreamreader (FCM, "UTF-8"); // read Row Operations

String content = "";

String line;

While (line = Br. Readline ())! = NULL ){

Content + = line + "\ n ";

}

BR. Close ();

FCM. Close ();

Show. settext (content );

 

} Catch (exception e ){

E. printstacktrace ();

Toast. maketext (this, "failed to read data! ", Toast. length_long). Show ();

}

 

Write a user class as follows:

Public class user {

Private string name;

Private long date;

Private int count;

Public user (string name, long date, int count ){

Super ();

This. Name = Name;

This. Date = date;

This. Count = count;

}

Public user (string name, int count ){

Super ();

This. Name = Name;

This. Count = count;

This. Date = system. currenttimemillis ();

}

Public String getname (){

Return name;

}

Public void setname (string name ){

This. Name = Name;

}

Public long getdate (){

Return date;

}

Public void setdate (long date ){

This. Date = date;

}

Public int getcount (){

Return count;

}

Public void setcount (INT count ){

This. Count = count;

}

@ Override

Public String tostring (){

Return "User [name =" + name + ", date =" + date + ", Count =" + count

+ "]";

}

 

Public String writeinfo (){

Return name + "," + date + "," + count;

}

 

}

 

Click the Add User button once to add a piece of data to the file, as shown below:

Internalstoragetool. appendinfo (this, new user ("BB", 3 ));

The following method is added:

// Append user information

Public static Boolean appendinfo (context, user ){

Try {

Fileoutputstream Fos = context. openfileoutput (Const. in_filename, context. mode_append );

// The append permission is available here. Context. mode_append

FOS. Write (user. writeinfo () + "\ n"). getbytes ());

// Convert to byte write

FOS. Close ();

Return true;

} Catch (exception e ){

E. printstacktrace ();

Return false;

}

}

 

Click a button to read data from all users.

// Read all user information

Public static list <user> readallinfo (context ){

Try {

Fileinputstream FCM = context. openfileinput (Const. in_filename );

Bufferedreader BR = new bufferedreader (New inputstreamreader (FCM, "UTF-8 "));

List <user> Users = new arraylist <user> ();

String line;

While (line = Br. Readline ())! = NULL ){

Users. Add (PARSE (line ));

}

FCM. Close ();

BR. Close ();

Return users;

} Catch (exception e ){

E. printstacktrace ();

Return NULL;

}

}

 

The parse () method is as follows:

Private Static user parse (string info ){

String [] infoes = info. Split (",");

Long date = long. parselong (infoes [1]);

Int COUNT = integer. parseint (infoes [2]);

Return new user (infoes [0], date, count );

}

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.