一、流的概念

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

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

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

 

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

 

 字节流:FileInputStream用来读取文件

     FileOutputStream用来写入到文件

 字符流:FileReaderBufferedReader用来读取文件

     FileWriteBufferedWrite用来写入到文件

二、操作用法

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());
View Code

 

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         }
View Code

 

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

1  //创建文件夹
2         File f = new File("F:\test");
3         if (f.isDirectory())//判断是不是一个文件夹
4         {
5             System.out.println("创建失败");
6         } else {
7             f.mkdir();
8         }
View Code

 

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         }
View Code

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 }
View Code

读取成功..

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 manrn西城男孩 - 更完美的人";
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 }
View Code

写入成功..

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:ttWestlife.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 }
View Code

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 }
View Code

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+"rn");
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 }
View Code

 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!