Home > Java > javaTutorial > body text

Detailed introduction to File file operations and IO stream content in Java

不言
Release: 2018-10-12 15:56:09
forward
2829 people have browsed it

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.

Preface

  1. What does a stream do: To permanently save data.

  2. IO streams are used to handle data transmission between devices (uploading and downloading files)

  3. Java operates on data through streams. .

  4. Java objects used to operate streams are in the IO package.

  5. The learning of java IO system is essentially the reading and writing of files.

File class

  1. 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.

  2. 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.

  3. File cannot perform any read or write operations on the contents of the file it represents (that's what streams do).

  4. 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()
    Copy after login

Classification of flows

  1. Divided into different categories according to different data flow directions Input stream and output stream.

  2. 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.

  1. 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

  1. 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

  1. A subclass derived from character stream Reader:

    (1)CharArrayReader
    (2)BufferedReader/LineNumberReader
    (3)FilterReader
    (4)InputStreamReader/FileReader
    Copy after login
  2. Character stream Writer-derived subclass:

    (1)BufferedWriter
    (2)CharArrayWriter
    (3)FilterWriter
    (4)OutputStreamWriter/FileWriter
    (5)PrintWriter
    (6)StringWriter
    Copy after login
  3. Byte stream InputStream-derived Subclass :

    (1)FileInputStream
    (2)ObjectInputStream
    (3)FilterInputStream/BufferedInputStream/DataInputStream/PushbackInputStream
    (4)SequenceInputStream
    (5)ByteArrayInputStream
    (6)StringBufferInputStream)
    Copy after login
  4. Byte stream OutputStream derived class:

    (1)ByteArrayOutputStream
    (2)FileOutputStream
    (3)FilterOutputStream/BufferedOutputStream/DataOutputStream
    (4)ObjectOutputStream
    Copy after login

Optimize reading and writing: Byte buffer stream

  1. 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.

  2. When reading the file byte input stream, it is read directly from the same byte stream.

  3. Because the byte stream is read from the hardware (storage medium), it is slower.

  4. 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[]).

  5. 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.

  6. 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

  1. Conversion stream is a bridge from byte stream to character stream, which can convert byte stream into character stream.

  2. The principle is to add encoding and decoding operations on the basis of byte stream.

  3. Character stream = byte stream encoding table

  4. 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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template