Home  >  Article  >  Java  >  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

php是最好的语言
php是最好的语言Original
2018-08-04 14:29:241891browse

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:
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