Java中IO流文件读取、写入和复制的实例

2025-05-29 0 35

//构造文件File类
File f=new File(fileName);

//判断是否为目录
f.isDirectory();

//获取目录下的文件
String[] fileName=f.list();

//获取目录下的文件
File[] files=f.listFiles();

1、Java怎么读取文件

?

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
package com.yyb.file;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

/*

* 读取文件:

* 1、找到指定的文件

* 2、根据文件创建文件的输入流

* 3、创建字节数组

* 4、读取内容,放到字节数组里面

* 5、关闭输入流

*/

public class FileRead {

public static void main(String[] args) {

// 构建指定文件

File file = new File("E:" + File.separator + "hello.txt");

InputStream in = null;

try {

// 根据文件创建文件的输入流

in = new FileInputStream(file);

// 创建字节数组

byte[] data = new byte[1024];

// 读取内容,放到字节数组里面

in.read(data);

System.out.println(new String(data));

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

// 关闭输入流

in.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

2、Java怎么写入文件

?

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
package com.yyb.file;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;

/*

* 写入文件:

* 1、找到指定的文件

* 2、根据文件创建文件的输出流

* 3、把内容转换成字节数组

* 4、向文件写入内容

* 5、关闭输入流

*/

public class FileWriter {

public static void main(String[] args) {

// 构建指定文件

File file = new File("E:" + File.separator + "hello.txt");

OutputStream out = null;

try {

// 根据文件创建文件的输出流

out = new FileOutputStream(file);

String message = "我是好人。";

// 把内容转换成字节数组

byte[] data = message.getBytes();

// 向文件写入内容

out.write(data);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

// 关闭输出流

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

3、Java怎么复制文件

?

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
<span style="font-size:18px;">package com.yyb.file;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

/*

* 实现思路:

* 1、构建源文件与目标文件

* 2、源文件创建输入流,目标文件创建输出流

* 3、创建字节数组

* 4、使用循环,源文件读取一部分内容,目标文件写入一部分内容,直到写完所有内容

* 5、关闭源文件输入流,目标文件输出流

*/

public class FileCopy {

public static void main(String[] args) {

// 构建源文件

File file = new File("E:" + File.separator + "HelloWorld.txt");

// 构建目标文件

File fileCopy = new File("D:" + File.separator + "HelloWorld");

InputStream in = null;

OutputStream out = null;

try {

// 目标文件不存在就创建

if (!(fileCopy.exists())) {

fileCopy.createNewFile();

}

// 源文件创建输入流

in = new FileInputStream(file);

// 目标文件创建输出流

out = new FileOutputStream(fileCopy, true);

// 创建字节数组

byte[] temp = new byte[1024];

int length = 0;

// 源文件读取一部分内容

while ((length = in.read(temp)) != -1) {

// 目标文件写入一部分内容

out.write(temp, 0, length);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

// 关闭文件输入输出流

in.close();

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}</span><span style="font-size: 24px;">

</span>

以上这篇JavaIO流文件读取写入复制的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/qq_31360175/article/details/49407005

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 Java中IO流文件读取、写入和复制的实例 https://www.kuaiidc.com/114385.html

相关文章

发表评论
暂无评论