Home >Java >javaTutorial >Detailed introduction to File file operations and IO stream content in Java
This article brings you a detailed introduction to File file operations and IO stream content in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
What does a stream do: To permanently save data.
IO streams are used to handle data transmission between devices (uploading and downloading files)
Java operates on data through streams. .
Java objects used to operate streams are in the IO package.
The learning of java IO system is essentially the reading and writing of files.
File is used in java to abstract a file. Whether it is an ordinary file or a directory, it can deal with a File object.
File is just an abstract representation of a file or directory on the disk. Internally, it actually relies on a platform-independent local file system class.
File cannot perform any read or write operations on the contents of the file it represents (that's what streams do).
Member method:
(1)创建功能: 【1】public boolean createNewFile() 【2】public boolean mkdir() 【3】public boolean mkdirs() (2)删除功能:public boolean delete() (3)重命名功能:public boolean renameTo(File dest) (4)判断功能: 【1】public boolean isDirectory() 【2】public boolean isFile() 【3】public boolean exists() 【4】public boolean canRead() 【5】public boolean canWrite() 【6】public boolean isHidden() (5)获取功能: 【1】public String getAbsolutePath() 【2】public String getPath() 【3】public String getName() 【4】public long length() 【5】public long lastModified() (6)高级获取功能: 【1】public String[] list() 【2】public File[] listFiles()
Divided into different categories according to different data flow directions Input stream and output stream.
According to the different types of data processed, it is divided into byte stream and character stream:
(1) Character stream: As the name implies, this stream only Can process characters, but it processes characters very quickly.
(2) Byte stream: It can process all files stored in bit units, which means that it can process all files, but it is not as fast as character stream in processing characters.
Abstract base class of character stream:
(1) Reader: Abstract class of character input stream, all The implementation of character input stream is its subclass
(2)Writer: the abstract class of character output stream, the implementation of all character output classes is its subclass
Abstract base class of byte stream:
(1) InputStream: abstract class of byte input stream, the parent class of all byte input streams
(2) OutputStream: The abstract class of byte output stream is the super class of all classes that output byte streams
A subclass derived from character stream Reader:
(1)CharArrayReader (2)BufferedReader/LineNumberReader (3)FilterReader (4)InputStreamReader/FileReader
Character stream Writer-derived subclass:
(1)BufferedWriter (2)CharArrayWriter (3)FilterWriter (4)OutputStreamWriter/FileWriter (5)PrintWriter (6)StringWriter
Byte stream InputStream-derived Subclass :
(1)FileInputStream (2)ObjectInputStream (3)FilterInputStream/BufferedInputStream/DataInputStream/PushbackInputStream (4)SequenceInputStream (5)ByteArrayInputStream (6)StringBufferInputStream)
Byte stream OutputStream derived class:
(1)ByteArrayOutputStream (2)FileOutputStream (3)FilterOutputStream/BufferedOutputStream/DataOutputStream (4)ObjectOutputStream
Byte stream reads and writes one array at a time, which is obviously much faster than reading and writing one byte at a time. This is the buffer effect of adding an array.
When reading the file byte input stream, it is read directly from the same byte stream.
Because the byte stream is read from the hardware (storage medium), it is slower.
When the CPU needs to use data, it will be limited by the slow speed of hardware IO when reading data through read() and read(byte[]).
The read and write speed between CPU and memory is more than 10 times faster than hardware IO, so buffer optimization can be established in the memory.
Ideas for optimizing reading and writing:
(1) Establish a buffer in the memory
(2) First put the buffer in the storage medium The bytes are read into the buffer
(3) When the CPU needs data, it is read directly from the buffer
(4) The buffer must be large enough, and after being read, the fill() function is triggered to automatically read from the buffer The bytes are read from the file byte contents of the media and stored into a buffer array.
Conversion stream is a bridge from byte stream to character stream, which can convert byte stream into character stream.
The principle is to add encoding and decoding operations on the basis of byte stream.
Character stream = byte stream encoding table
FileReader and FileWriter are actually byte streams at the bottom, but they are configured with an additional encoding decoder, the bottom layer uses bytes to read data and converts it into characters through the decoder, or converts the characters into bytes through the encoder and then outputs it.
The above is the detailed content of Detailed introduction to File file operations and IO stream content in Java. For more information, please follow other related articles on the PHP Chinese website!