Java Basic Learning Notes IO stream

Source: Internet
Author: User

1.IO streams are used to process data transfer between devices
Direction: The hard disk through "read Reader" to complete the memory read, memory through "write writer" to complete the write to the hard disk
2.Java operation of the data is streamed through the way
3.Java the objects used to manipulate the flow are in the IO package
4. Streaming is divided into two types of data: byte stream and character stream
--The origin of the character stream:
The byte stream reads bytes of text, does not operate directly but first checks the specified encoding table. Gets the corresponding text. Then manipulate the text. Simply put: Character stream = byte stream + encoding table
--two top-level parent class for byte stream: 1. InputStream 2.OutputStream
--two top-level parent of a character stream: 1. Reader 2.Writer
--Character Stream:

"To manipulate text data, prioritize character streams

"And to write data from memory to the hard disk, use the output stream in the character stream. Writer

-the key word for the action file is FileWriter, which is used to write to the data stream. Specific format: FileWriter fw=new FileWriter ("Demo.txt", true); This is the target file name, an exception is thrown when creating the object IOException, because the file name may not have the correct path and will be unsafe. If the path file does not exist, it is created automatically and, if present, overrides the

Small details: Implement line wrapping in Java with \ r, or create variables with Line_separator, call the System method

The code here

 Public classIoexceptiondemo {Private Static FinalString Line_separator =System.lineseparator ();  Public Static voidMain (string[] args) {FileWriter fw=NULL; Try{FW=NewFileWriter ("Demo.txt",true); Fw.write ("ABCDE" + line_separator + "hahaha"); } Catch(IOException e) {System.out.println (e.tostring ()); } finally {            if(FW! =NULL)                Try{fw.close (); } Catch(IOException e) {Throw NewRuntimeException ("Shutdown failed"); }        }    }}

This is the point, filewriter handling the unusual way!! is important

Read character data keyword FileReader, specific format filereader fr=new filereader ("Demo.txt"), must ensure that the file is present, and then call the Read method reading

Read reads are available in two ways:

 Public class Test11 {    publicstaticvoidthrows  IOException {                FileReader Fr=new filereader ("Demo.text");                 int ch=fr.read ();                SYSTEM.OUT.PRINTLN (CH);                Fr.close ();    }}

This is the way one, each character reads the characters inside the file

 Public classTest11 { Public Static voidMain (string[] args)throwsIOException {FileReader fr=NewFileReader ("Demo.text"); Char[] buf=New Char[3]; intNum=fr.read (BUF);//Store the read characters in an arraySystem.out.println (num+ "" +buf.tostring ()); intNum1=fr.read (BUF);//Store the read characters in an arraySystem.out.println (num+ "" +buf.tostring ()); intNum2=fr.read (BUF);//Store the read characters in an arraySystem.out.println (num+ "" +buf.tostring ()); }}

This is the way two,

The difference between the two is that mode 2 can read more than one at a time, and mode 1 reads 1 characters.

Mode 2 There is a detail when the array is defined as 3, each time the re-reading is placed in the same number of groups, so to overwrite the previously read data, and then the output will be overwritten, this NOTE!!!!

Let's talk about the replication implementation process:

Principle: Read the data in the C-drive file, and then write to the D-disk file, read-write

5. Flow by flow: input stream, output stream

Java Basic Learning Notes IO stream

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.