Home Java javaTutorial About IO streams in Java: stream inheritance relationships, processing streams, and conversion streams

About IO streams in Java: stream inheritance relationships, processing streams, and conversion streams

Aug 04, 2018 pm 02:29 PM
java io流

1. The inheritance relationship of streams, as well as byte streams and character streams.

2. Node streams FileOutputStream and FileInputStream and processing streams BufferedInputStream and BufferedOutputStream. And the corresponding FileOutputWriter, FileInputReader, BufferedInputReader, BufferedOutputWriter.

3. Convert streams InputStreamReader and OutputStreamWriter

1: Stream inheritance relationship

Byte stream

Character stream

Usage scope of character stream and byte stream: Byte stream is generally used to process images, videos, and PPT, Word type files . Character streams are generally used to process plain text files, such as TXT files, etc. Byte streams can be used to process plain text files, but character streams cannot be used to process non-text files such as images and videos.

2: Processing streams BufferedReader, BufferedWriter, BufferedInputStream

BufferedOutputsStream must include the upper layer of node streams. In other words, the processing stream is based on the node stream. The stream with Buffered is also called a buffered stream. The buffered stream processes the input and output of files the fastest. Therefore, buffered streams are generally used more frequently.

The following are two simple examples of file copying:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

twenty one

twenty two

twenty three

twenty four

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

public class MycopyTest {

public static void main(String[] args) {

File src = new File("D:/1.jpg" );

// D:/1.jpg must exist otherwise an error will be reported

File dest = new File("D:/2.jpg" );

// If D:/2.jpg exists, overwrite it, if it does not exist, create a new one

streamCopy(src, dest);

}

private static void readCopy(File src,File dest)


{

FileReader fr=null;

FileWriter fw=null;

BufferedReader br=null;

BufferedWriter bw=null;

try {

      fr=new FileReader(src);

      fw=new FileWriter(dest);

      br=new BufferedReader(fr);

      bw=new BufferedWriter(fw);

      String str;

     while((str=br.readLine())!=null)

        {

        bw.write(str);

        bw.newLine();

      }

    }catch (IOException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

    }

    try {

      bw.close();

      br.close();

    } catch (IOException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

    }

 

  }

  private static void streamCopy(File src, File dest) {

    FileInputStream fis = null;

    FileOutputStream fos = null;

    BufferedInputStream bis = null;

    BufferedOutputStream bos = null;

    try {

      fis = new FileInputStream(src);

      fos = new FileOutputStream(dest);

      bis = new BufferedInputStream(fis);

      bos = new BufferedOutputStream(fos);

      int len;

      byte[] b = new byte[1024];

      while ((len = bis.read(b)) != -1) {

        bos.write(b, 0, len);

        // bos.write(b,0,len);是把读到数组的大小字节写入

        // bos.write(b);最后一次如果数组未写满的话就会多读。

      }

    } catch (IOException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

    }

    try {

      bos.close();

      bis.close();

    } catch (Exception e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

    }

  }

}

Three: Conversion stream InputStreamReader and OutputStreamWriter

The role of conversion stream, when the text file is stored in the form of byte stream on the hard disk, After reading through InputStreamReader, it is converted into a character stream for processing by the program. The character stream processed by the program is converted into a byte stream through OutputStreamWriter and saved.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

twenty one

twenty two

twenty three

twenty four

25

26

27

28

29

30

31

32

public class InputStreamWriterTest {

public static void main(String[] args) {

File src = new File("D:/Files/Shit.txt");

File dest = new File("D:/Files/Smith.txt" );

BufferedWriter bw = null;

BufferedReader br = null;

FileInputStream fis = null;

FileOutputStream fos = null;

try {

fis = new FileInputStream(src);

fos = new FileOutputStream(dest);

     InputStreamReader ir = new InputStreamReader(fis, "GBK");

     OutputStreamWriter ow = new OutputStreamWriter(fos, "GBK");

      bw = new BufferedWriter(ow);

       br = new BufferedReader(ir);

       String str;

       while ((str = br.readLine()) != null) {

        bw.write(str);

        bw.newLine();

         bw.flush();

       }

} catch (IOException e) {

       e.printStackTrace();

}

try {

        bw.close();

       br.close();

} catch (IOException e) {

       e.printStackTrace();

}

}

}

Related articles:

Java IO stream output stream OutputString() use

Java's comprehensive introduction to IO stream

Related videos:

The latest Java complete video tutorial-free online video tutorial

The above is the detailed content of About IO streams in Java: stream inheritance relationships, processing streams, and conversion streams. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

See all articles