java流文件复制编程_JAVA IO流编程 实现文件的写入、写出以及拷贝

本文详细介绍了JAVA中的IO流概念,包括输入流、输出流、字节流和字符流。通过实例展示了如何使用FileInputStream、FileOutputStream、FileReader、BufferedReader等进行文件读写,并提供了文件复制、创建、删除等操作。同时,文章还讲解了缓冲流BufferedReader和BufferedWriter在提高效率方面的应用。
摘要由CSDN通过智能技术生成

一、流的概念

流:数据在数据源(文件)和程序(内存)之间经历的路径。

输入流:数据从数据源(文件)到程序(内存)的路径。

输出流:数据从程序(内存)到数据源(文件)的路径。

以内存为参照,如果数据向内存流动,则是输入流,反之则是输出流

d6f623e0e6df06bbc6613e11aae1b5aa.png

字节流:fileinputstream用来读取文件

fileoutputstream用来写入到文件

字符流:filereader\bufferedreader用来读取文件

filewrite\bufferedwrite用来写入到文件

二、操作用法

1.获取文件对象,针对该对象进行一些基本操作

1 //创建一个文件对象

2 file f = new file("f:\\test\\sheet.xls");

3 //得到文件的路径

4 system.out.println("文件路径"+f.getabsolutepath());

5 //得到文件的大小,字节数

6 system.out.println("文件大小"+f.length());

7 //可读属性

8 system.out.println("可读"+f.canread());

2.创建文件(判断该文件是否存在,若存在则弹出提示,若不存在则进行创建)

1 //创建文件

2 file f = new file("f:\\test\\test.txt");

3 //判断该文件是否存在

4 if(!f.exists())

5 {

6 //可以创建

7 try {

8 f.createnewfile();

9 } catch (ioexception e) {

10 e.printstacktrace();

11 }

12 }

13 else

14 {

15 system.out.println("改文件已存在,创建失败!");

16 }

3.创建文件夹(条件同上)

1 //创建文件夹

2 file f = new file("f:\\test");

3 if (f.isdirectory())//判断是不是一个文件夹

4 {

5 system.out.println("创建失败");

6 } else {

7 f.mkdir();

8 }

tips:这里写明一下isfile()、exists()和isdirectory()的区别

isfile():判断是否文件,也许可能是文件或者目录。

exists():判断是否存在,可能不存在。

isdirectory(): 判断该对象是否是一个文件夹。

4.列出某文件夹下面的所有文件(此时对象还是file,file没有文件和文件夹之分,对电脑来讲,文件夹只是一种特殊的文件)

1 file f = new file("f:\\testt");

2 if (f.isdirectory()) {

3 file filelists[] = f.listfiles();

4 for (int i = 0; i < filelists.length; i++)

5 {

6 system.out.println("文件名是:"+filelists[i].getname());

7 }

8 }

5.fileinputstream的使用

1 /**

2 * 演示fileinputstream类的使用

3 */

4 package com.test2;

5

6 import java.io.*;

7

8 public class demo11_2 {

9 public static void main(string[] args) {

10

11 //得到一个文件对象

12 file f = new file("f:\\tt\\test.txt");

13 fileinputstream fis = null;

14 //因为file没用读写的能力,所以需要使用fileinputstream

15 try {

16 fis = new fileinputstream(f);

17

18 //定义一个字节数组(相当于一个缓存,如果你的对象"f"是一个很大的文件,内存不够用,所以只能一点一点地读取)

19 byte[] bytes = new byte[1024];

20 //实际读取到的字节数

21 int n = 0;

22 //循环读取

23 //如果read()返回-1,则说明读取完毕

24 while ((n = fis.read(bytes)) != -1) {

25 //将字节转换成string

26 //此时实例化s时,要注意指定编码格式,电脑上文档默认的是gbk,而我这边默认的是utf-8,

27 //所以如果不指定格式的话,最后输出的中文会出现乱码

28 string s = new string(bytes, 0, n,"gbk");

29 system.out.println(s);

30 }

31 } catch (exception e) {

32 e.printstacktrace();

33 } finally {

34 //关闭文件流(关键)

35 try {

36 if (fis != null) {

37 fis.close();

38 }

39 } catch (ioexception e) {

40 e.printstacktrace();

41 }

42 }

43 }

44 }

b79c1f5d795f79902bd1985ec43ccfcd.png

读取成功..

6.fileoutputstream的使用

1 /**

2 * 演示fileoutputstream的使用

3 */

4 package com.test2;

5

6 import java.io.*;

7

8 public class demo11_3 {

9 public static void main(string[] args) {

10 file f = new file("f:\\tt\\test.txt");

11 //字节输出流

12 fileoutputstream fos = null;

13

14 try {

15 fos = new fileoutputstream(f);

16

17 string s = "westlife - better man\r\n西城男孩 - 更完美的人";

18

19 fos.write(s.getbytes());

20 } catch (exception e) {

21 e.printstacktrace();

22 } finally {

23 try {

24 if (fos != null) {

25 fos.close();

26 }

27 } catch (ioexception e) {

28 e.printstacktrace();

29 }

30 }

31

32 }

33 }

00f0a0648ce7dd1184a4940daddff758.png

写入成功..

7.字节流的操作(通过写入写出来实现图片的拷贝,操作byte)

1 /**

2 * 图片拷贝

3 */

4 package com.test2;

5

6 import java.io.*;

7

8 public class demo11_4 {

9 public static void main(string[] args) {

10 //先把图片读入到内存,再写到某个文件

11 //因为是二进制文件,因此只能用字节流完成

12 file f = new file("f:\\tt\\westlife.jpg");

13 //输入流

14 fileinputstream fis = null;

15 //输出流

16 fileoutputstream fos = null;

17

18 try {

19 fis = new fileinputstream(f);

20 //或者省略上面实例化file,直接在这里fis = new fileinputstream("f:\tt\westlife.jpg");也可以

21

22 fos = new fileoutputstream("d:\\练习\\westlife.jpg");

23 byte[] bytes = new byte[1024];

24 int n = 0;//记录实际读取到的字节数

25 //循环读取

26 while ((n = fis.read(bytes)) != -1) {

27 //输出到指定文件

28 fos.write(bytes);

29 }

30 } catch (exception e) {

31 e.printstacktrace();

32 } finally {

33 //关闭打开的文件流

34 if (fos != null) {

35 try {

36 fos.close();

37 } catch (ioexception e) {

38 e.printstacktrace();

39 }

40 }

41

42 if (fis != null) {

43 try {

44 fis.close();

45 } catch (ioexception e) {

46 e.printstacktrace();

47 }

48 }

49 }

50 }

51 }

8.字符流的操作(操作char)

1 /**

2 * 字符流操作

3 */

4 package com.test2;

5

6 import java.io.*;

7

8 public class demo11_5 {

9 public static void main(string[] args) {

10 //文件取出字符流对象(输入流)

11 filereader fr = null;

12 //写入到文件(输出流)

13 filewriter fw = null;

14

15 //创建一个fr对象

16 try {

17 //创建输入对象

18 fr = new filereader("f:\\tt\\test.txt");

19 //创建输出对象

20 fw = new filewriter("d:\\练习\\test2.txt");

21

22

23 //读入到内存

24 int n = 0;//记录实际读取到的字符数

25 char c[] = new char[1024];

26 while ((n = fr.read(c)) != -1) {

27 //输入

28 // string s = new string(c,0,n);

29 // system.out.println(s);

30 //输出

31 //方法一:fw.write(c);

32 方法二://指定输出的起始位置

33 fw.write(c, 0, n);

34 }

35 } catch (exception e) {

36 e.printstacktrace();

37 } finally {

38 //关闭文件流

39 if (fr != null) {

40 try {

41 fr.close();

42 } catch (ioexception e) {

43 e.printstacktrace();

44 }

45 }

46

47 if (fw != null) {

48 try {

49 fw.close();

50 } catch (ioexception e) {

51 e.printstacktrace();

52 }

53 }

54 }

55

56 }

57 }

9.缓冲字符流(提高了效率,直接操作string)

1 /**

2 * 缓冲字符流操作

3 */

4 package com.test2;

5

6 import java.io.*;

7

8 public class demo11_6 {

9 public static void main(string[] args) {

10 bufferedreader br = null;

11 bufferedwriter bw = null;

12

13 //先创建filereader对象

14 filereader fr = null;

15

16 //创建filewriter对象

17 filewriter fw = null;

18 try {

19 fr = new filereader("f:\\tt\\test.txt");

20 br = new bufferedreader(fr);

21

22

23 fw = new filewriter("d:\\练习\\test3.txt");

24 bw = new bufferedwriter(fw);

25

26 //循环读取文件

27 string s = "";

28 while ((s = br.readline()) != null) {

29 //读取到内存

30 //system.out.println(s);

31

32 //输出到磁盘

33 bw.write(s+"\r\n");

34 }

35 } catch (exception e) {

36 e.printstacktrace();

37 } finally {

38 //注:如果文件流不关闭的话会影响后续对该文件的操作,比如可能读不到该文件的数据

39 if (br != null) {

40 try {

41 {

42 br.close();

43 }

44 } catch (ioexception e) {

45 e.printstacktrace();

46 }

47 }

48

49 if (bw != null) {

50 try {

51 {

52 bw.close();

53 }

54 } catch (ioexception e) {

55 e.printstacktrace();

56 }

57 }

58 }

59 }

60 }

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值