Reading notes--java IO

Source: Internet
Author: User

Typical use of IO streams

1. Cache input Files

BufferedReader in=Newnew  filereader (file name)); String s; StringBuilder SB=new  StringBuilder ();  while ((S=in.readline ()) =null) {sb.append (s+ "\ n");} In.close ();

A string SB is used to store the contents of a file.

2. Input from memory

Work with the contents of the files saved in the SB in the previous step to output the contents of the memory to the console.

StringReader in=New  StringReader (SB);   while ((int c=in.read ())!=-1) {System.out.print ((char) c); }

  

3. Formatted memory input

4, the basic file output

The FileWriter object can write data to a file. First, create a filewriter that is connected to the specified file. In fact, we usually wrap it in bufferedwriter to buffer the output. PrintWriter can format the output of the data for easy reading.

5. Storing and recovering data

If we use DataOutputStream output data, Java guarantees that we can use DataInputStream to read the data accurately.

6. Read and write random access files

Using Randomaccessfile, it is similar to combining DataInputStream and dataoutputstream (because they implement the same interface: Datainput and DataOutput). In addition, the use of seek () can be moved around the file, arbitrarily modify a value in the file.

7. Pipeline Flow

The values of PipedInputStream, PipedOutputStream, Pipedreader and PipedWriter are reflected in the understanding of multithreading. Because pipeline flows are used for communication between tasks.

Utility for file read and write

A common task is to read the file into memory modification and save it to external memory.

Reading notes--java IO

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.