Java中IO流 字节流实例详解

2025-05-29 0 55

JavaIO流 字节流实例详解

IO流(输入流、输出流),又分为字节流、字符流。

  流是磁盘或其它外围设备中存储的数据的源点或终点。

  输入流:程序从输入流读取数据源。数据源包括外界(键盘、文件、网络…),即是将数据源读入到程序的通信通道。

  输出流:程序向输出流写入数据。将程序中的数据输出到外界(显示器、打印机、文件、网络…)的通信通道。

字节流

  1.InputStream、OutputStream

  InputStream抽象了应用程序读取数据的方式

  OutputStream抽象了应用程序写出数据的方式

  2.读到文件结尾,称为EOF = end,读到-1就读到结尾

  3.输入流基本方法

  int b = in.read();读取一个字节,无符号填充到int的低八位.-1是EOF

  int.read(byte[] buf)读取数据填充到字节数组buf

  int.read(byte[] buf, int start, int size)读取数据填充到字节数组buf,从buf的start位置开始存储size长度的数据

  4.输出流基本方法

  out.write(int b);写出一个byte到流,b的低八位

  out.write(byte[] buf);将buf字节数组都写入到流

  out.write(byte[] buf, int start, int size);字节数组buf从start位置开始写size长度的字节到流

  5.FileInputStream是InputStream的子类,具体实现了在文件上读取数据

  6.FileOutputStream是OutputStream的子类,实现了向文件中写出字节数据的方法

  FileInputStream的demo:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

69

70

71
package com.test.io;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class IOUtils {

/**

* 读取指定文件内容,按照十六进制输出到控制台

* 并且每输出10个byte换行

* @param fileName

* @throws IOException

*/

public static void printHex(String fileName) throws IOException {

//把文件作为字节流进行读操作

FileInputStream in = new FileInputStream(fileName);

int b;

int i = 1;

while ((b = in.read()) != -1) {

if (b <= 0xf) {

System.out.print("0");

}

System.out.print(Integer.toHexString(b) + " ");

if (i % 10 == 0) {

System.out.println("");

}

i++;

}

in.close();

}

public static void printHexByByteArray(String fileName) throws IOException {

FileInputStream in = new FileInputStream(fileName);

byte[] buf = new byte[20*1024];

//如果字节数组够大,可以一次性读完

//从in中批量读取字节,放入到buf这个字节数组中,从第0个位置开始放,最多放buf.length个,返回的是读到的字节的个数

/* int bytes = in.read(buf, 0, buf.length);

int j = 1;

for(int i = 0;i < bytes; i++) {

if (buf[i] <= 0xf) {

System.out.print("0");

}

System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");

if (j % 10 == 0) {

System.out.println("");

}

j++;

} */

//如果字节数组不够大,不能一次性读完

int bytes = 0;

int j = 1;

while ((bytes = in.read(buf, 0, buf.length)) != -1) {

for (int i = 0; i <bytes; i++) {

if (buf[i] <= 0xf) {

System.out.print("0");

}

System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");

if (j % 10 == 0) {

System.out.println("");

}

j++;

}

}

}

}

  FileOutputStream的demo:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25
package com.test.io;

import java.io.FileOutputStream;

import java.io.IOException;

public class FileOutputDemo {

public static void main(String[] args) throws IOException {

//如果该文件不存在,则直接创建,如果存在,删除后创建。(如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。)

FileOutputStream out = new FileOutputStream("F:\\\\javaio\\\\out.dat");

out.write('A');//写入了‘A'的低八位(一次只写入一个字节)

int a = 10;

out.write(a >>> 24);

out.write(a >>> 16);

out.write(a >>> 8);

out.write(a);

byte[] b = "10".getBytes();

out.write(b);

out.close();

IOUtils.printHex("F:\\\\javaio\\\\out.dat");

}

}

  7.DataOutputStream和DataInputStream,对流功能的扩展,可以更加方便的读取int,long,字符等类型数据。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
package com.test.io;

import java.io.DataOutputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class DataOutputDemo {

public static void main(String[] args) throws IOException {

String file = "F:\\\\javaio\\\\b.txt";

DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));

dos.writeInt(10);

dos.writeInt(-10);

dos.writeLong(10l);

dos.writeDouble(10.5);

dos.writeUTF("你好");

dos.writeChars("中国");

dos.close();

IOUtils.printHex(file);

}

}

  运行结果:

?

1

2

3

4
00 00 00 0a ff ff ff f6 00 00

00 00 00 00 00 0a 40 25 00 00

00 00 00 00 00 06 e4 bd a0 e5

a5 bd 4e 2d 56 fd

  其中,00 06两个字节是“你好”这两个中文的字节个数。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25
package com.test.io;

import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.IOException;

public class DataInputDemo {

public static void main(String[] args) throws IOException {

String file = "F:\\\\javaio\\\\b.txt";

DataInputStream dis = new DataInputStream(new FileInputStream(file));

int i = dis.readInt();

System.out.println(i);

i = dis.readInt();

System.out.println(i);

long l = dis.readLong();

System.out.println(l);

double d = dis.readDouble();

System.out.println(d);

String s = dis.readUTF();

System.out.println(s);

dis.close();

}

}

  运行结果:

?

1

2

3

4

5
10

-10

10

10.5

你好

  8.BufferedInputStream&BufferedOutputStream,这两个流类为IO提供了带缓冲区的操作,一般打开文件进行写入或读取操作时,都会加上缓冲,这种流模式提高了IO的性能。

  文件的拷贝:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87
package com.test.io;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class IOUtils {/**

* 拷贝文件,字节批量读取

* @param srcFile 源文件

* @param destFile 目标文件

* @throws IOException

*/

public static void copyFile(File srcFile, File destFile) throws IOException {

if (!srcFile.exists()) {

throw new IllegalArgumentException("文件" + srcFile + "不存在");

}

if (!srcFile.isFile()) {

throw new IllegalArgumentException(srcFile + "不是文件");

}

FileInputStream in = new FileInputStream(srcFile);

FileOutputStream out = new FileOutputStream(destFile);

byte[] buf = new byte[10*1024];

int b;

while ((b = in.read(buf, 0, buf.length)) != -1) {

out.write(buf,0,b);

out.flush();//最好加上,刷新此输出流并强制写出所有缓冲的输出字节。

}

in.close();

out.close();

}

/**

* 拷贝文件,利用带缓冲的字节流

* @param srcFile

* @param destFile

* @throws IOException

*/

public static void copyFileByBuffer(File srcFile, File destFile) throws IOException {

if (!srcFile.exists()) {

throw new IllegalArgumentException("文件" + srcFile + "不存在");

}

if (!srcFile.isFile()) {

throw new IllegalArgumentException(srcFile + "不是文件");

}

FileInputStream in = new FileInputStream(srcFile);

FileOutputStream out = new FileOutputStream(destFile);

BufferedInputStream bis = new BufferedInputStream(in);

BufferedOutputStream bos = new BufferedOutputStream(out);

int c;

while ((c = bis.read()) != -1) {

bos.write(c);

bos.flush();

}

bis.close();

bos.close();

}

/**

* 拷贝文件,通过单字节读取

* @param srcFile

* @param destFile

* @throws IOException

*/

public static void copyFileByByte(File srcFile, File destFile) throws IOException {

if (!srcFile.exists()) {

throw new IllegalArgumentException("文件" + srcFile + "不存在");

}

if (!srcFile.isFile()) {

throw new IllegalArgumentException(srcFile + "不是文件");

}

FileInputStream in = new FileInputStream(srcFile);

FileOutputStream out = new FileOutputStream(destFile);

int c;

while ((c = in.read()) != -1) {

out.write(c);

out.flush();

}

in.close();

out.close();

}

}

  测试文件拷贝:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
package com.test.io;

import java.io.File;

import java.io.IOException;

public class IOUtilsTest {

public static void main(String[] args) {

//IOUtils.printHex("D:\\\\javaProgram\\\\Hello.java");

try {

long start = System.currentTimeMillis();

//IOUtils.copyFile(new File("F:\\\\javaio\\\\1.mp3"), new File("F:\\\\javaio\\\\2.mp3"));//211ms

//IOUtils.copyFileByBuffer(new File("F:\\\\javaio\\\\1.mp3"), new File("F:\\\\javaio\\\\3.mp3"));//18583ms

IOUtils.copyFileByByte(new File("F:\\\\javaio\\\\1.mp3"), new File("F:\\\\javaio\\\\4.mp3"));//37822ms

long end = System.currentTimeMillis();

System.out.println(end - start);

} catch (IOException e) {

e.printStackTrace();

}

}

}

  根据以上测试看出,文件拷贝,最快的方式是通过字节的批量读取。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://www.cnblogs.com/tianxintian22/p/6821267.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java中IO流 字节流实例详解 https://www.kuaiidc.com/116590.html

相关文章

发表评论
暂无评论