io

对程序语言设计者来说,设计一个令人满意的I/O(输入输出)系统,是件极艰巨的任务。————摘自《Think in Java》

File

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
public class FileTest1 {
public static void main(String[] args) throws IOException {
File file = new File("/Users/bob/Downloads/1.txt");
System.out.println(file.createNewFile());
}
}
public class FileTest2 {
public static void main(String[] args) throws IOException {
File file = new File("/Users/bob/Downloads/abc");
File file1 = new File(file, "1.txt");
System.out.println(file1.createNewFile());
File file2 = new File("/Users/bob/Downloads/abc", "2.txt");
System.out.println(file2.createNewFile());
}
}
public class FileTest3 {
public static void main(String[] args) throws IOException {
File file = new File("/Users/bob/Downloads/abc/xyz");
//file.mkdir();
//建立路径下的多个目录
file.mkdirs();
System.out.println(file.isDirectory());
System.out.println(file.isFile());
}
}
public class FileTest4 {
public static void main(String[] args) throws IOException {
File file = new File("/Users/bob/Downloads");
// String[] names = file.list();
//
// for (String name : names) {
// System.out.println(name);
// }
//获取子目录和文件列表
File[] files = file.listFiles();
for (File file1 : files) {
// System.out.println(file1.getName());
System.out.println(file1.getParent());
}
}
}
public class FileTest5 {
public static void main(String[] args) throws IOException {
File file = new File("/Users/bob/Downloads/abc/xyz");
// file.mkdirs();
file.delete();
}
}
public class FileTest6 {
public static void main(String[] args) throws IOException {
File file = new File("/Users/bob/Downloads/abc/xyz");
//文件和目录过滤
String[] names = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.endsWith(".java")) {
return true;
}
return false;
}
});
for (String name : names) {
System.out.println(name);
}
}
}
public class FileTest7 {
public static void main(String[] args) throws IOException {
//系统文件分隔符
File file = new File(File.separator);
File file1 = new File(file, "1.txt");
System.out.println(file1.getAbsolutePath());
file1.createNewFile();
}
}

java程序通过流来完成输入/输出。流是生产或者消费信息的抽象。流通过java的输入/输出系统与物理设备链接。尽管与它们链接的物理设备不尽相同,所有流的行为具有同样的方式。这样,相同的输入/输出类和方法适用于所有类型的外部设备。这意味着一个输入流能够抽象多种不同类型的输入:从磁盘文件,从键盘或从网络套接字。同样,一个输出流可以输出到控制台,磁盘文件或相连的网络。流是处理输入/输出的一个洁净的方法,例如它不需要代码理解键盘和网络的不同。java中流的实现是子啊java.io包定义的类层次结构内部。

输入/输出时,数据在通信通道中流动。所谓“数据流(stream)”指的是所有数据通信通道之中,数据的起点和终点。信息的通道就是一个数据流。只要是数据从一个地方”流“到另外一个地方,这种数据流动的通道都可以称为数据流。

输入/输出是相对于程序来说的。程序在使用数据时所扮演的角色有两个:一个是源,一个是目的。若程序是数据流的源,即数据的提供者,这个数据流对程序来说就是一个”输出数据流“(数据从程序流出)。若程序是数据流的终点,这个数据流对程序而言就是一个”输入数据流“(数据从程序外流向程序)

流的分类

在java.io包中提供了60多个类(流)。

输入流/输出流

从功能上分为两大类:输入流和输出流。

从流结构上可分为字节流(以字节为处理单位或称面向字节)和字符流(以字符为处理单位或称面向字符)。字节流(byte stream)为处理字节的输入和输出提供了方便的方法。例如使用字节流读取或写入二进制数据。字符流(character stream)为字符的输入和输出处理提供了方便。它们采用了统一的编码标准,因而可以国际化。当然在某些场合,字符流比字节流更有效。

需要声明:在最底层,所有的输入/输出都是字节形式的。

字节流的输入流和输出流基础是InputStream和OutputStream这两个抽象类,字节流的输入输出操作由这两个类的子类实现。字符流是java1.1版以后新增加的以字符为单位进行输入输出处理的流,字符流输入输出的基础是抽象类Reader和Writer。

读数据的逻辑为:
open a stream

while more information

read information

close the stream

写数据的逻辑为:
open a stream

while more information

write information

close the stream

节点流/过滤流

节点流:从特定的地方读写的流类,例如:磁盘或一块内存区域。
过滤流:使用节点流作为输入或输出。过滤流是使用一个已经存在的输入流或输出流连接创建的。

类层次

io-1

io-2

io-3

io-4

io-5

API和示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class InputStreamTest {
public static void main(String[] args) throws IOException {
FileInputStream inputStream = new FileInputStream("/Users/bob/Downloads/abc/xyz/1.txt");
byte[] bytes = new byte[200];
int length = 0;
while (-1 != (length = inputStream.read(bytes, 0, 200))) {
String str = new String(bytes, 0, length);
System.out.println(str);
}
inputStream.close();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class OutputStreamTest {
public static void main(String[] args) throws IOException {
FileOutputStream outputStream = new FileOutputStream("/Users/bob/Downloads/abc/xyz/2.txt");
String str = "hello world!";
byte[] bytes = str.getBytes();
outputStream.write(bytes);
outputStream.close();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class BufferedOutputStreamTest {
public static void main(String[] args) throws IOException {
OutputStream outputStream = new FileOutputStream("/Users/bob/Downloads/abc/xyz/2.txt");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
bufferedOutputStream.write("http://www.baidu.com".getBytes());
bufferedOutputStream.close();
outputStream.close();
}
}