标签:
http://www.cnblogs.com/hellokitty1/p/4456305.html
java I/O系统
I:Input 输入 O:Output 输出
二:流的分类
按照方向:输入流 输出流
按照最小单位:字节流 (byte) 字符流 (char)
三:
所有的I/O系统操作都由以下步骤构成
1)建立流
2)操作流
3)关闭流
四:文件类
java.io包中的File类提供了管理磁盘文件和目录的基本功能
File类有四种构造方法
最常用的的是 public File(URI uri) URI是资源统一标识符
java.io.File的一些方法,主要还是要查看API文档。
1 package com.lovo;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Date;
6
7 /**
8 * File类测试
9 *
10 * @author hellokitty
11 *
12 */
13 public class FileTest {
14
15 public static void main(String[] args) {
16 // 创建File对象
17 File file = new File("E:\\jg\\exercise_bak.txt");
18
19 // 能否读
20 System.out.println("能否读:" + file.canRead());
21
22 // 删除
23 System.out.println("删除成功:" + file.delete());
24
25 // 重新创建文件对象
26 file = new File("E:\\jg\\exercise_bak.txt");
27
28 // 判断文件是否存在
29 System.out.println("是否存在:" + file.exists());
30
31 // 目录或文件名称
32 System.out.println("名称:" + file.getName());
33
34 // 是否目录、文件
35 System.out.println("是否目录:" + file.isDirectory());
36 System.out.println("是否文件:" + file.isFile());
37
38 // 最后一次修改时间
39 System.out.println("最后一次修改时间:" + new Date(file.lastModified()));
40
41 // 文件大小
42 System.out.println("文件大小:" + file.length());
43
44 // 重新创建File对象
45 file = new File("E:\\jg");
46
47 System.out.println("文件目录列表:");
48 // 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录
49 String[] list = file.list();
50 for (String string : list) {
51 System.out.println(string);
52 }
5354
55 // 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件对象
56 File[] files = file.listFiles();
57 for (File item : files) {
58 if (item.isDirectory()) { // 当前File对象为目录,则遍历该目录下所有子目录与文件
59 System.out.println(item.getName() + " 目录下子目录与文件:");
60 String[] it = item.list();
61 for (String i : it) {
62 System.out.println(i);
63 }
64 System.out.println("*******************************");
65 continue;
66 }
67
68 System.out.println(item.getName() + " 文件");
69 }
70
71 // 重新创建File对象
72 file = new File("E:\\jg\\test\\demo\\test.txt");
73 if (!file.exists()) { // 文件不存在
74 // 获取文件路径
75 File dir = file.getParentFile();
76 if (!dir.exists()) { // 目录不存在,则创建路径中所有不存在的目录
77 dir.mkdirs();
78 }
79
80 try {
81 // 创建空文件
82 System.out.println("文件是否创建成功:" + file.createNewFile());
83 } catch (IOException e) {
84 e.printStackTrace();
85 }
86 }
87
88 }
89 }
五:字节流 byte:用于处理二进制文件
InputStream ----》FileInputStream (读取) OutputStream ---》FileOutputStream (写入)
字节流的输入输出流代码测试
1 package com.lovo;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10
11 /**
12 * 字节输入输出流测试
13 *
14 * @author hellokitty
15 *
16 */
17 public class IOTest {
18
19 public static void main(String[] args) {
20 StringBuffer buffer = new StringBuffer(); // 字符串缓冲
21
22 /* 输入流 */
23 InputStream in = null;
24
25 try {
26 // 1. 打开输入流
27 in = new FileInputStream("E:\\jg\\exercise.txt");
28 // 2. 读取
29
30 byte[] b = new byte[1024 * 4];
31 int len = in.read(b); // 返回读取到的字节数,返回-1表示读取到流结尾
32 while(len != -1){
33 buffer.append(new String(b, 0, len)); // 将读取到的字节解析为String追加到缓冲
34 len = in.read(b);
35 }
36 // System.out.println("读到" + len + "字节的数据");
37 System.out.println(buffer.toString());
38 } catch (FileNotFoundException e) {
39 e.printStackTrace();
40 } catch (IOException e) {
41 e.printStackTrace();
42 } finally {
43 // 3. 释放资源,关闭输入流
44 if (in != null){
45 try {
46 in.close();
47 } catch (IOException e) {
48 e.printStackTrace();
49 }
50 }
51 }
52
53 /* 输出流 */
54 OutputStream out = null;
55
56 try {
57 File file = new File("D:\\test\\demo\\test.txt");
58 if (!file.getParentFile().exists()){ // 文件路径不存在,则创建路径中所有不存在的目录
59 file.getParentFile().mkdirs();
60 }
61 // 1. 打开输出流
62 out = new FileOutputStream(file);
63 // 2. 写
64 out.write(buffer.toString().getBytes());
65 } catch (FileNotFoundException e) {
66 e.printStackTrace();
67 } catch (IOException e) {
68 e.printStackTrace();
69 } finally {
70 // 3. 释放输出流资源
71 if (out != null){
72 try {
73 74 out.close();//已经带有刷新的了
75 } catch (IOException e) {
76 e.printStackTrace();
77 }
78 }
79 }
80 }
81 }
六:字符流 char 用于处理文本文件 Reader----》InputStreamReader--->FileReader Write---》OutputStreamWrite---》FileWrite
1 package com.lovo;
2
3 import java.io.FileNotFoundException;
4 import java.io.FileReader;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.io.Reader;
8 import java.io.Writer;
9
10 /**
11 * 字符输入输出流测试
12 *
13 * @author14 *
15 */
16 public class IOTest2 {
17
18 public static void main(String[] args) {
19 StringBuffer buffer = new StringBuffer();
20
21 /* 输入流 */
22 Reader reader = null;
23
24 try {
25 // 1. 打开流
26 reader = new FileReader("E:\\jg\\exercise.txt");
27 // 2. 读取
28 char[] ch = new char[128]; // 缓冲区
29 int len;
30 do {
31 len = reader.read(ch);
32 if (len == -1)
33 break;
34 buffer.append(new String(ch, 0, len));
35 } while (len != -1);
36 System.out.println(buffer.toString());
37 } catch (FileNotFoundException e) {
38 e.printStackTrace();
39 } catch (IOException e) {
40 e.printStackTrace();
41 } finally {
42 // 3. 释放资源
43 if (reader != null) {
44 try {
45 reader.close();
46 } catch (IOException e) {
47 e.printStackTrace();
48 }
49 }
50 }
51
52 /* 输出流 */
53
54 Writer writer = null;
55
56 try {
57 // 1. 打开流
58 writer = new FileWriter("d:\\test.txt");
59 // 2. 写入
60 writer.write(buffer.toString());
61 } catch (IOException e) {
62 e.printStackTrace();
63 } finally {
64 // 3. 释放资源
65 if (writer != null) {
66 try {
67 68 writer.close();
69 } catch (IOException e) {
70 e.printStackTrace();
71 }
72 }
73 }
74 }
75 }
七:缓冲流
1 package com.lovo.day2;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12
13 public class BufferedTest {
14
15 public static void main(String[] args) {
16 long start = System.currentTimeMillis();
17 copyByBuffer("E:\\myeclipse-2015-2014-07-11-offline-installer-windows.exe", "d:\\test.exe");
18 long end = System.currentTimeMillis();
19 System.out.println("缓冲:" + (end - start));
20 System.out.println("***************");
21 start = System.currentTimeMillis();
22 copy("E:\\myeclipse-2015-2014-07-11-offline-installer-windows.exe", "d:\\test2.exe");
23 end = System.currentTimeMillis();
24 System.out.println("不带缓冲:" + (end - start));
25 }
26
27 private static void copy(String source, String destination){
28 InputStream in = null;
29 OutputStream out = null;
30
31 File file = new File(destination);
32 if (!file.getParentFile().exists()){
33 file.getParentFile().mkdirs();
34 }
35
36 try {
37 // 打开流
38 in = new FileInputStream(source);
39 out = new FileOutputStream(file);
40 // 操作:读写
41 byte[] b = new byte[1024];
42 int len;
43
44 while(-1 != (len=in.read(b))){
45 out.write(b, 0, len);
46 }
47 } catch (FileNotFoundException e) {
48 e.printStackTrace();
49 } catch (IOException e) {
50 e.printStackTrace();
51 } finally {
52 // 释放资源
53 if (in != null){
54 try {
55 in.close();
56 } catch (IOException e) {
57 e.printStackTrace();
58 }
59 }
60 if(out != null){
61 try {
62 out.close();
63 } catch (IOException e) {
64 e.printStackTrace();
65 }
66 }
67 }
68 }
69
70 private static void copyByBuffer(String source, String destination){
71 BufferedInputStream in = null;
72 BufferedOutputStream out = null;
73
74 try {
75 // 打开流
76 in = new BufferedInputStream(new FileInputStream(source), 8 * 1024 * 1024);
77 out = new BufferedOutputStream(new FileOutputStream(destination), 8 * 1024 * 1024);
78 // 读写
79 byte[] b = new byte[1024];
80 int len;
81
82 while(-1 != (len = in.read(b))){
83 out.write(b, 0, len);
84 }
85 } catch (FileNotFoundException e) {
86 e.printStackTrace();
87 } catch (IOException e) {
88 e.printStackTrace();
89 } finally {
90 // 释放资源
91 if (in != null){
92 try {
93 in.close();
94 } catch (IOException e) {
95 e.printStackTrace();
96 }
97 }
98 if (out != null){
99 try {
100 out.close();
101 } catch (IOException e) {
102 e.printStackTrace();
103 }
104 }
105 }
106 }
107 }
有缓冲的比没有缓冲要快些。
未完。。。
标签:
原文地址:http://www.cnblogs.com/alloevil/p/5939549.html