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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles