Java Learning Note--io Stream

Source: Internet
Author: User

The 12th Chapter outlines:
I/O input/output input/output
First, create the file, with the help of the files class to implement
File.createnewfile (): Create File
File.exists (): Determines if the file exists and returns true if it exists
Delete (): Deletes the file and returns true if the deletion succeeds
Deleteonexit (): Delete Files only when program exits
Second, create a folder
Exists (): Determines if the folder exists and returns true if it exists
Delete (): Deletes the folder and returns true if the deletion succeeds
mkdir (): Create Folder
Mkdirs (): Create a nested folder

Three
The input or output reference is memory. Our program is running in memory
Input: D:\\720.txt---> Memory
Output: Memory---->f:\\721.txt

Byte stream and character stream, both can realize the input and output of text, the difference is the reading and writing unit (speed) is not the same
BYTE stream: 1byte=8bit 2^8-1 can represent 255 characters at a time of reading and writing
Character stream: 1 characters per read/write = 2 bytes Common Chinese characters 5,000 more 2^16

Both the node stream and the filter stream are encapsulated in byte streams and character streams, with the benefit of faster read and write speeds.
1. Byte stream: FileInputStream and FileOutputStream
FileInputStream
Read (): One byte at a time, the return value is int, if read to the head, the return value is-1 want to read the data in the text, to use the loop
Available (): Returns the number of readable bytes, the return value is int
Close (): Closes the input stream, freeing the resource

FileOutputStream
Write (): writes data to the specified file
Close (): Turn off the output stream

Enter the contents of the 720.txt text into the 721.txt text

2. Character Stream: FileReader FileWriter

FileReader
Read (): The reading parameter can be a char array that holds the data read into memory
Close (): Close Resource

FileWriter
Write (): The Write parameter can be a char array, or it can be a string
Close (): Close Resource

From the direction of flow
Input stream FileInputStream FileReader
Output stream FileOutputStream FileWriter
From read-write units
byte stream FileInputStream FileOutputStream
Character Stream FileReader FileWriter

Add: Filter Stream: Package a node stream, provide a buffer, read and write faster
BufferedReader
ReadLine (): reads one line at a time
BufferedWriter
Write (): Writing
NewLine (): Line break

Addition: Serialization (Serializable) and deserialization of read-write objects is the deserialization of a write object that is serialized
An object that wants to be serialized must implement an interface: Serializable
ObjectInputStream
WriteObject (obj): Writes the object obj as a whole into the specified text as a binary stream

ObjectOutputStream
ReadObject (): Reads binary objects in the text into memory

Q: What do we do when we want to save multiple objects? You can call WriteObject () to write more than once, but you'll be in trouble when you read it.


File to create and delete files, folders
InputStream implementation of data input
OutputStream implementing the output of the data
FileReader--------------
FileWriter--------------

BufferedReader output stream with buffer
BufferedWriter input stream with buffer
ObjectInputStream Input Object
ObjectOutputStream Output Object

Java Learning Note--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.