欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA IO流编程 实现文件的写入、写出以及拷贝

程序员文章站 2022-05-21 17:36:25
一、流的概念 流:数据在数据源(文件)和程序(内存)之间经历的路径。 输入流:数据从数据源(文件)到程序(内存)的路径。 输出流:数据从程序(内存)到数据源(文件)的路径。 以内存为参照,如果数据向内存流动,则是输入流,反之则是输出流 字节流:FileInputStream用来读取文件 FileOu ......

一、流的概念

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

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

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

 

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

JAVA IO流编程   实现文件的写入、写出以及拷贝

 

 字节流: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 }

JAVA IO流编程   实现文件的写入、写出以及拷贝

读取成功..

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 }

JAVA IO流编程   实现文件的写入、写出以及拷贝

写入成功..

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 }