About IO streams in Java: Inheritance relationships for streams, processing flows, transformation flows

Source: Internet
Author: User
1, the inheritance of the stream, as well as the byte stream and character stream.

2, Node stream fileoutputstream and FileInputStream and processing flow bufferedinputstream and Bufferedoutputstream. and the corresponding fileoutputwriter,fileinputreader,bufferedinputreader,bufferedoutputwriter.

3. Conversion Flow InputStreamReader and OutputStreamWriter

One: The inheritance relationship of the stream

BYTE stream

Character Stream

The use of character streams and byte streams: Byte streams are typically used to process images, videos, and Ppt,word types of files. Character streams are typically used to work with plain text types of files, such as TXT files, which can be used to process plain text files, but character streams cannot be used to process non-text types of files such as image video.

Two: Processing flow Bufferedreader,bufferedwriter,bufferedinputstream

Bufferedoutputsstream, a layer of node flow is to be wrapped. That is, the processing flow is based on the node flow, with buffered flow, also known as the buffer stream, the buffer stream processing file input and output speed is the fastest. So the general buffer flow is used more.

Here are two simple examples of File replication:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

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

publicclassMycopyTest {

publicstatic voidmain(String[] args) {

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

// D:/1.jpg必须的存在不然会报错

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

// 如果D:/2.jpg存在则覆盖,如果不存在则新建

streamCopy(src, dest);

}

privatestatic voidreadCopy(File src,File dest)


{

FileReader fr=null;

FileWriter fw=null;

BufferedReader br=null;

BufferedWriter bw=null;

try{

fr=newFileReader(src);

fw=newFileWriter(dest);

br=newBufferedReader(fr);

bw=newBufferedWriter(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();

}

}

privatestatic voidstreamCopy(File src, File dest) {

FileInputStream fis = null;

FileOutputStream fos = null;

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

try{

fis = newFileInputStream(src);

fos = newFileOutputStream(dest);

bis = newBufferedInputStream(fis);

bos = newBufferedOutputStream(fos);

intlen;

byte[] b = newbyte[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 flow InputStreamReader and OutputStreamWriter

The function of the conversion stream, the text file in the hard disk in the form of byte stream storage, through the InputStreamReader read after the conversion to a character stream to the program processing, the program handles the character stream through OutputStreamWriter conversion to a byte stream save.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

publicclassInputStreamWriterTest {

publicstatic voidmain(String[] args) {

File src = newFile("D:/Files/狗屁.txt");

File dest = newFile("D:/Files/斯密斯.txt");

BufferedWriter bw = null;

BufferedReader br = null;

FileInputStream fis = null;

FileOutputStream fos = null;

try{

fis = newFileInputStream(src);

fos = newFileOutputStream(dest);

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

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

bw = newBufferedWriter(ow);

br = newBufferedReader(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:

Use of the output stream outputstring () of the Java IO stream

A comprehensive introduction to IO flow in Java

Related videos:

latest Java full video tutorial-free online video tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.