Simple IO flow

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[simple IO stream]

< span style="color:#333333;">< span style="color:#333333;"> Hello, I'm the 9th java student of Shenzhen Branch of it Academy. I'm an honest and kind java programmer. Today, I'd like to share with you the simple IO stream of Java.

< span style="color:#333333;">< span style="color:#333333;"> 1. Background introduction

< span style="color:#333333;">< span style="color:#333333;">

< span style="color:#333333;">< span style="color:#333333;"> Stream is a set of sequential bytes with starting point and ending point. It is the general name or abstraction of data transmission.

< span style="color:#333333;">< span style="color:#333333;"> The transmission of data between two devices is called stream. The essence of stream is data transmission. According to the characteristics of data transmission, the stream is abstracted into various types to facilitate more intuitive data operation.

< span style="color:#333333;">< span style="color:#333333;">

< span style="color:#333333;">< span style="color:#333333;"> 2. Knowledge analysis

< span style="color:#333333;">< span style="color:#333333;"> IO overview:

< span style="color:#333333;"> File class

< span style="color:#333333;"> IO stream classification

< span style="color:#333333;"> InputStream&Reader

< span style="color:#333333;"> OutPutStream&Writer

< span style="color:#333333;"> RandomAccessFile class: random reading;

< span style="color:#333333;"> Byte stream input and output

< span style="color:#333333;"> Byte stream

< span style="color:#333333;"> Learn the main classes and methods.

< span style="color:#333333;"> Q: Accept command line input:

< span style="color:#333333;"> Q: Accept keyboard input

< span style="color:#333333;"> Q: How do I know which directories and subdirectories are in the file system

< span style="color:#333333;"> How to know the attributes of the file, readable | writable;

< span style="color:#333333;"> Q: How to read and write files? (byte file, character file, sequential read, random read, object serialization and reconstruction, etc.)

< span style="color:#333333;"> summary:

< span style="color:#333333;"> Input: read

< span style="color:#333333;"> Output: disk, CD, database, node, etc. (very wide)

< span style="color:#333333;"> IO stream: input, output stream.

< span style="color:#333333;"> Byte stream: processes input and output operations in bytes

< span style="color:#333333;"> Character stream: processes input and output operations in character units

< span style="color:#333333;"> File class

< span style="color:#333333;"> The file class represents platform independent files and directories (it shields the difference of the underlying file system and is a platform independent file class)

< span style="color:#333333;"> Generalized classes, including methods, and files, folders, and platform independent.

< span style="color:#333333;"> Functions: create, delete and rename files and directories* You cannot read yourself through a file class. Need IO stream access.

< span style="color:#333333;"> Common methods:

< span style="color:#333333;"> Access file name: file detection directory operation related file operation related obtain general file information

< span style="color:#333333;"> getName() -exists() -mkDir() -createNewFile() -lastModify()

< span style="color:#333333;"> create folder

< span style="color:#333333;">- getPath() -canWrite -list( ) -delete( ) -Length( )

< span style="color:#333333;"> Get the path of the construction parameter () and list all subdirectories

< span style="color:#333333;"> (return whatever the construction parameter is written)

< span style="color:#333333;">- getAbsoluteFile -canRead -ListFile( )

< span style="color:#333333;"> () list all documents

< span style="color:#333333;"> Returns the absolute pathname form of the file object

< span style="color:#333333;">- getAbsolutePath -isFile

< span style="color:#333333;"> ( ) ( )

< span style="color:#333333;"> Get absolute path

< span style="color:#333333;"> (full path)

< span style="color:#333333;">- getParent( ) -isDirectory

< span style="color:#333333;"> ( )

< span style="color:#333333;">- renameTo

< span style="color:#333333;"> (File newName)

< span style="color:#333333;"> According to different construction methods, files with different paths and files can be created.

< span style="color:#333333;"> eg:1:

< span style="color:#333333;"> File myfile = new file("xx","xxx.test)

< span style="color:#333333;"> File myfile = new file("xxx.test")

< span style="color:#333333;"> Eg: 2:

< span style="color:#333333;"> File mydic = new = new file ("XXX") / / create a folder in the current path

< span style="color:#333333;"> Mydic = new file (mydic, "XXX. Test") / / call this folder directly and let XXX Test is placed under XXX folder;

< span style="color:#333333;">————————————

< span style="color:#333333;"> Console I/O

< span style="color:#333333;"> System. Out standard output is a printstream. There are many overloaded methods in it

< span style="color:#333333;"> system. In standard input is an InputStream~

< span style="color:#333333;"> System. Err standard error is a printstream~

< span style="color:#333333;"> In Java util

< span style="color:#333333;"> Scanner provides a standard (formatted) input function.

< span style="color:#333333;">------------------------------------

< span style="color:#333333;"> Advanced I/O Streams

< span style="color:#333333;"> Def: input / output stream:

< span style="color:#333333;"> Input / output stream: it refers to data stream, and data stream is a set of bytes with sequence, starting point and ending point.

< span style="color:#333333;"> Java IO through Java A series of classes and interfaces of IO package;

< span style="color:#333333;"> Java developers provide standard input and output stream classes to shield the differences between underlying hardware or programs;

< span style="color:#333333;">- Java cannot directly operate I / O devices, but establishes channels between programs and devices through streams as intermediate media;

< span style="color:#333333;"> The flow closes the differences between the underlying I / O devices.

< span style="color:#333333;">- To establish a stream is actually to establish a data transmission channel to link the starting point and the ending point.

< span style="color:#333333;">- Stream is an abstract expression of output transmission, which is independent of specific devices. Once the program has established the flow, it can ignore the device at the beginning or end.

< span style="color:#333333;">- Input stream: load from file, standard input, input of other peripherals into memory;

< span style="color:#333333;">- Output stream: save the data in memory to a file or transfer it to the output device;

< span style="color:#333333;"> File learning:

< span style="color:#333333;"> File: I:

< span style="color:#333333;"> 1: Before I / O operator needs to get this file to basic information

< span style="color:#333333;"> Path File name read or write permission (Create time) (file size)

< span style="color:#333333;"> 2: The file itself does not involve the specific contents inside the file, but processes the file as a whole, such as

< span style="color:#333333;"> Operations provided by the above common methods;

< span style="color:#333333;"> Pass: can operate on file name and path (is a generalized class)

< span style="color:#333333;"> 3: Files and directories are abstracted into files;

< span style="color:#333333;"> If "file" is a directory, it has a list method.

< span style="color:#333333;"> If "file" is a file, there is no list method;

< span style="color:#333333;"> File: II:

< span style="color:#333333;"> The file class has four constructors:

< span style="color:#333333;"> The object of file can be: directory, file, existing specific file, and file that does not exist (it will be created automatically after the construction is completed)

< span style="color:#333333;">——————————————————————————————————————————

< span style="color:#333333;"> I / O Basics

< span style="color:#333333;"> Byte : in. out

< span style="color:#333333;"> Character : Reader writer

< span style="color:#333333;"> InputStream:

< span style="color:#333333;"> Except for filterinputstream, all the others are read according to bytes (function stream), which is very inefficient and can only read one byte at a time

< span style="color:#333333;"> FileInputStream read file - the starting point is the file; Read local file

< span style="color:#333333;"> Objectinputstream distributed programming transfers objects (between nodes) on two virtual machines

< span style="color:#333333;"> Pipeinputstream transmits data between multiple threads;

< span style="color:#333333;"> It is used to read data from the pipeline during pipeline input / output

< span style="color:#333333;"> The sequenceinputsteam stream is from multiple nodes, and the nodes are read in turn

< span style="color:#333333;"> (used by converting to a single InputStream input stream object)

< span style="color:#333333;"> FileterInputStream DataInputStream

< span style="color:#333333;"> Format the data, which can be used to read the basic data types in Java

< span style="color:#333333;"> Pushbackinputstream for reading data from local files

< span style="color:#333333;"> (first read a part to determine the attribute, and then press the data back,

< span style="color:#333333;"> Pushback ~ ~ then read the entire file)

< span style="color:#333333;"> Buffered~~ BufferedInputStream

< span style="color:#333333;"> Multiple data can be buffered during reading (cached streams are added)

< span style="color:#333333;"> LineNumberInputStream

< span style="color:#333333;"> (read line by line)

< span style="color:#333333;"> Stringbufferinputstream reads from memory and starts from memory

< span style="color:#333333;"> Bytearrayinputstream is a stream of byte arrays in memory

< span style="color:#333333;"> Audioinputstream audio input / output

< span style="color:#333333;"> OutputStream

< span style="color:#333333;"> fileOutputStream DataOutPutStream

< span style="color:#333333;"> Write data to local file bufferdoutputstream

< span style="color:#333333;"> PrintStream

< span style="color:#333333;"> Provides many ways to output formatted data to the screen

< span style="color:#333333;"> ObjectOutPutStream

< span style="color:#333333;"> PipedOutPutStream

< span style="color:#333333;"> Output data to a pipeline (in pairs)

< span style="color:#333333;"> ByteArrayOutPutStream

< span style="color:#333333;"> Dataoutputstream provides support for basic data types of Java

< span style="color:#333333;"> Reader

< span style="color:#333333;"> BufferdReader LineNumberReader

< span style="color:#333333;"> CharArrayReader

< span style="color:#333333;"> StringReader

< span style="color:#333333;"> InputStreamReader FilerReader

< span style="color:#333333;"> Byte to character conversion Bridge

< span style="color:#333333;"> PipedReader

< span style="color:#333333;"> FilterReader pushbackReader

< span style="color:#333333;"> Writer:

< span style="color:#333333;"> BufferedWriter

< span style="color:#333333;"> CharArrayWriter

< span style="color:#333333;"> StringWriter

< span style="color:#333333;"> OutputStreamWriter fileWriter

< span style="color:#333333;"> PrintWriter

< span style="color:#333333;"> PipeWriter

< span style="color:#333333;"> FilterWriter

< span style="color:#333333;"> Judge the end point of reading and writing; Judge the reading mode. You can select the corresponding function implementation; file. jia buff

< span style="color:#333333;">____

< span style="color:#333333;"> InputStream methods -- abstract methods

< span style="color:#333333;"> Int read(). Abstract methods; Read one at a time

< span style="color:#333333;"> Int read (byte buffer) -- specific method; Read several at a time, in the method byte []

< span style="color:#333333;"> Int read (byte buffer, int offset, int length) the specific method reads several times at a time, where to start reading and how long to read

< span style="color:#333333;"> Other methods include:

< span style="color:#333333;"> Void close( )

< span style="color:#333333;"> How many bytes does int avaliable() have, and then facilitate subsequent memory development

< span style="color:#333333;"> Long skip (long n) how many byte reads are skipped

< span style="color:#333333;"> Boolean makSupported ( )

< span style="color:#333333;"> Void mark (int readlimit)

< span style="color:#333333;"> Void reset() returns to the last tag and reads it repeatedly

< span style="color:#333333;"> OutputStream Methods __ Abstract method

< span style="color:#333333;"> Void write (int c) abstract method

< span style="color:#333333;"> Specific method of void write (byte buffer)

< span style="color:#333333;"> Specific method of void write (byte buffer, int length)

< span style="color:#333333;"> Void close() concrete method

< span style="color:#333333;"> The last stream of the void flush() method is pushed by itself;

< span style="color:#333333;"> The Reader Methods

< span style="color:#333333;"> Int read() Abstract

< span style="color:#333333;"> Int read (char cbuf)

< span style="color:#333333;"> Other data declared by InputStream is not byte, but char

< span style="color:#333333;"> The Writer Methods

< span style="color:#333333;"> Void write(int c)

< span style="color:#333333;"> Void write(char cbuf)

< span style="color:#333333;"> Void write(char cbuf,int length )

< span style="color:#333333;"> Void write(String string )

< span style="color:#333333;"> Void write(String string,int length)

< span style="color:#333333;"> Void close( )

< span style="color:#333333;"> Void flush( )

< span style="color:#333333;">< span style="color:#333333;"> 3. Frequently asked questions

< span style="color:#333333;">< span style="color:#333333;"> Character stream and byte stream conversion characteristics, when to convert, specific embodiment?

< span style="color:#333333;">< span style="color:#333333;"> 4. Solutions

< span style="color:#333333;">< span style="color:#333333;"> Characteristics of conversion flow:

< span style="color:#333333;">< span style="color:#333333;"> It is a bridge between character stream and byte stream

< span style="color:#333333;">< span style="color:#333333;"> The read byte data can be converted into characters through specified encoding

< span style="color:#333333;">< span style="color:#333333;"> The read character data can be converted into bytes through specified encoding

< span style="color:#333333;">< span style="color:#333333;"> When to use a transformation stream?

< span style="color:#333333;">< span style="color:#333333;"> When there is conversion between bytes and characters;

< span style="color:#333333;">< span style="color:#333333;"> When the data of stream operation needs to be encoded or decoded.

< span style="color:#333333;">< span style="color:#333333;"> Specific objects:

< span style="color:#333333;">< span style="color:#333333;"> Inputstreamreader: byte to character Bridge

< span style="color:#333333;">< span style="color:#333333;"> Outputstreamwriter: character to byte Bridge

< span style="color:#333333;">< span style="color:#333333;"> These two stream objects are members of the character system. They have the function of conversion. They are also character streams, so they need to pass in byte stream objects during construction.

< span style="color:#333333;">< span style="color:#333333;">

< span style="color:#333333;">< span style="color:#333333;"> 5. Coding practice

< span style="color:#333333;">< span style="color:#333333;">

< span style="color:#333333;">< span style="color:#333333;">。。。

< span style="color:#333333;">< span style="color:#333333;">

< span style="color:#333333;">< span style="color:#333333;"> 6. Expand thinking

< span style="color:#333333;">< span style="color:#333333;">

< span style="color:#333333;">< span style="color:#333333;"> When to use byte stream and character stream?

< span style="color:#333333;">< span style="color:#333333;">

< span style="color:#333333;">< span style="color:#333333;"> Whenever dealing with plain text data, character streams are preferred. In addition, byte streams are used.

< span style="color:#333333;">< span style="color:#333333;">

< span style="color:#333333;">< span style="color:#333333;"> 7. References

< span style="color:#333333;">< span style="color:#333333;"> Baidu CSDN

< span style="color:#333333;">< span style="color:#333333;">

< span style="color:#333333;">< span style="color:#333333;"> 8. More discussion

< span style="color:#333333;">< span style="color:#333333;"> Q1:

< span style="color:#333333;"> < span style = "color: #333333;" > of InputStream What is the basic media flow:

< span style="color:#333333;">< span style="color:#333333;"> A1:

< span style="color:#333333;">< span style="color:#333333;"> Bytearrayinputstream, stringbufferinputstream and FileInputStream are three basic media streams,

< span style="color:#333333;"> They read data from byte arrays, stringbuffers, and local files, respectively.

< span style="color:#333333;"> Pipedinputstream reads data from a pipe shared with other threads.

< span style="color:#333333;"> Objectinputstream and all subclasses of filterinputstream are decorating streams (the protagonist of decorator pattern).

< span style="color:#333333;"> Q2:

< span style="color:#333333;"> Basic of OutputStream < span style = "color: #333333;" > What is a media stream:

< span style="color:#333333;">< span style="color:#333333;"> A2:

< span style="color:#333333;"> Utputstream is the parent class of all output byte streams. It is an abstract class.

< span style="color:#333333;"> Bytearrayoutputstream and fileoutputstream are two basic media streams,

< span style="color:#333333;"> They write data to byte arrays, and local files, respectively. Pipedoutputstream writes data to a pipeline shared with other threads,

< span style="color:#333333;"> Objectoutputstream and all subclasses of filteroutputstream are decorative streams.

< span style="color:#333333;"> Q3:

< span style="color:#333333;"> What is serialization of objects?

< span style="color:#333333;"> A3:

< span style="color:#333333;"> Convert the object into a series of bytes and record the state of the bytes so that the object can be reconstructed on another machine.

< span style="color:#333333;">< span style="color:#333333;">

< span style="color:#333333;">< span style="color:#333333;"> Ppt link: stamp here

< span style="color:#333333;">< span style="color:#333333;">

< span style="color:#333333;">< span style="color:#333333;"> Tencent Video: poke here

For more information, you can join the IT communication group 565734203 to discuss and communicate with you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>