Skip to content

Commit e4d6bf6

Browse files
committed
io
1 parent d61c57c commit e4d6bf6

File tree

2 files changed

+277
-0
lines changed

2 files changed

+277
-0
lines changed

IO/Javaio之FIle.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
##### 0.File类
2+
File类表示一个文件或者一个文件夹。所以,下文中说道的文件,请自行转换。
3+
4+
##### 1.相关方法
5+
6+
创建File对象的方法。
7+
8+
* File(File parent, String child)
9+
* File(String pathname)
10+
* File(String child, File parent)
11+
* File(String pathname, int prefixLength)
12+
* File(String parent, String child)
13+
* File(URI uri)
14+
15+
上面的几种方法会返回一个File对象,但是,我们所指定的文件(文件夹)不一定存在。因此 我们需要通过下面方法判断一下。
16+
```
17+
file.exists()
18+
```
19+
有些情况下,文件可能是隐藏文件,我们可以通过这样得知。
20+
```
21+
file.isHidden()
22+
```
23+
或许,你获取的是个文件夹也说不定。
24+
```
25+
file.isDirectory()
26+
```
27+
当是文件夹的时候,我们可以通过list()方法,列出他下面所有的文件名。举个例子。
28+
```
29+
File file = new File(".");
30+
String[] list ;
31+
if (file.isDirectory()){
32+
list = file.list();
33+
System.err.println("is dirctory");
34+
for (String s : list) {
35+
System.err.println(s);
36+
}
37+
}else {
38+
System.out.println("is file");
39+
System.out.println(file.getName());
40+
}
41+
```
42+
当然,我们可以通过listFiles的方法,直接返回File对象数组。
43+
44+
如果是文件的话,我们还可以判断访问权限,诸如,读写、执行。
45+
```
46+
file.canRead();
47+
file.canWrite();
48+
file.canExecute();
49+
```
50+
当然,我们也可以通过对应的set方法去改变权限。
51+
值得注意的是,执行指的是在linux下面。
52+
53+
还有一个get方法可以获得文件的绝对路径、文件大小、空闲大小、父目录等,这里就不啰嗦了。
54+
55+
##### 2.创建,删除等操作
56+
我们可以通过mkDir、mkDirs去创建文件夹。他们的区别如下。
57+
* mkDir只能在指定目录下创建一级文件,也就是说如果是./a/b的情况,目录./a需要存在。
58+
* mkDirs则是创建多级,如上,a不存在则先创建a,在创建b。
59+
60+
那么,怎么创建文件呢。createNewFile()方法就是用来创建文件的。
61+
62+
说了创建,那么我们就得说下删除了。删除有如下两个方法。
63+
* deleteOnExit 如果存在,就啥un出
64+
* delete 删除
65+
66+
我们在删除文件夹的时候要注意了,因为文件夹下面是有文件的,所以我们调用delete,并不会其效果,我们需要现将他下面的文件删除才行,注意了。
67+
68+
##### 3.重命名
69+
我们可以通过rename方法将文件重命名。
70+
71+
##### 4.RandomAccessFile
72+
RandomAccessFile是一种特使的File,只能是已存在的文件。
73+
获取方法。
74+
```
75+
RandomAccessFile randomAccessFile = new RandomAccessFile("./1.txt","rw");
76+
```
77+
第一个参数表示文件路径,第二个参数表示权限,rw为读写。当只有r权限的时候,如果我们想通过write方法写入内容,就会报EOF描述符异常。
78+
* seek 移动到文件某处
79+
* readxxx 读方法
80+
* writexx 写方法。
81+
82+
##### 4.总结
83+
File是io中相对基础的一个类,但是也很重要。
84+
85+
86+
87+
88+
89+
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
下文中所出现的代码可能不规范,譬如文件检查,读者自行体会用法就好。
2+
### 0.什么是字节流
3+
简单来说就是面向字节的IO流,也就是提供的read或者write方法,是对byte或者byte[] 操作的。在Java中,有这么几类。
4+
5+
**InputStream**
6+
7+
|| 功能 | 构造器的参数 |
8+
| ------------- |:-------------:| -----:|
9+
| ByteArrayInputStream | 将内存中的缓冲区当做InputStream使用 | byte[] |
10+
| StringBufferInputStream | 将String转化为InputStream | String |
11+
|FIleInputStream |从文件中读取信息 | File,FileDescriptor(文件描述符),String(文件)|
12+
|PipedInputStream |读取PipedOutputStream输入的数据,实现“管道化” | PipedOutputStream,需要和PipedOutputStream建立链接 |
13+
| SequenceInputStream| 将两个或多个流合并成一个(按顺序)|1.两个流 2.充满流的容器 |
14+
|FilterInputStream |包装其他流,提供更多有用的功能 | |
15+
16+
**OutputStream**
17+
|类名 |功能 |参数 |
18+
|-|:-:|-:|
19+
|ByteOutputStream |将输入写入到byte[] |大小size |
20+
|FileOutputStream|输入到文件 | File,FileDescriptor(文件描述符),String(文件)|
21+
| PipedOutputStream| 输入数据,当做PipedInputStream的输入数据,实现“管道化” |PipedInputStream,需要和PipedInputStream建立链接关系 |
22+
|FilterOutputStream | 包装其他流,提供更多有用的功能| |
23+
24+
### 1.通用
25+
通过read和write的方法来进行数据的读写。
26+
### 2.ByteArray相关
27+
举个例子,现在将一个byte[]数组的内容读出来并输入到控制台。
28+
```
29+
String s = new String("这是测试数据");
30+
byte[] in = new byte[0];
31+
try {
32+
in = s.getBytes("utf-8");
33+
} catch (UnsupportedEncodingException e) {
34+
e.printStackTrace();
35+
}
36+
ByteArrayInputStream bais = new ByteArrayInputStream(in);
37+
byte[] bytes1 = new byte[1024];
38+
try {
39+
StringBuffer stringBuffer = new StringBuffer();
40+
while (bais.read(bytes1) != -1){
41+
stringBuffer.append(new String(bytes1).trim());
42+
System.err.println(stringBuffer);
43+
}
44+
bais.close();
45+
} catch (IOException e) {
46+
e.printStackTrace();
47+
}
48+
```
49+
看下控制台的输出。
50+
51+
![这里写图片描述](http://img.blog.csdn.net/20160806115524375)
52+
由于StringBufferInputStream和ByteArrayInputStream的用法一致,就不在多说。
53+
### 2.File相关
54+
举个例子,从一个文件中读取内容,写入到另一个文件中。代码如下。
55+
```
56+
File file = new File("./1.txt");
57+
File file1 = new File("./2.txt");
58+
if (!file1.exists()){
59+
try {
60+
file1.createNewFile();
61+
} catch (IOException e) {
62+
e.printStackTrace();
63+
}
64+
}
65+
try {
66+
FileInputStream fis = new FileInputStream(file);
67+
StringBuffer sb = new StringBuffer();
68+
FileOutputStream fos = new FileOutputStream(file1);
69+
byte[] b = new byte[1024];
70+
while ( fis.read(b) != -1){
71+
sb.append(new String(b).trim());
72+
}
73+
fos.write(b);
74+
fos.flush();
75+
fos.close();
76+
System.err.println(sb);
77+
fis.close();
78+
} catch (FileNotFoundException e) {
79+
e.printStackTrace();
80+
} catch (IOException e) {
81+
e.printStackTrace();
82+
} finally {
83+
84+
}
85+
```
86+
### 3.Piped相关
87+
在此之前,我们需要了解一下管道。[点我了解管道](http://blog.chinaunix.net/uid-27034868-id-3394243.html)
88+
接下来,我们以两个线程为例,来展示下他的用法。
89+
```
90+
static class ReadThread implements Runnable{
91+
92+
private PipedInputStream pis ;
93+
94+
public ReadThread(PipedInputStream pis){
95+
this.pis = pis ;
96+
}
97+
98+
@Override
99+
public void run() {
100+
byte[] b = new byte[1024];
101+
try {
102+
if (null != pis){
103+
System.err.println(getDate() + " 等待另一头输入数据");
104+
while (pis.read(b) != -1){
105+
System.err.println(getDate()+ " 读取成功,数据为:"+new String(b).trim());
106+
}
107+
pis.close();
108+
}else {
109+
System.err.println("pis is null");
110+
}
111+
} catch (IOException e) {
112+
e.printStackTrace();
113+
}
114+
}
115+
}
116+
```
117+
读线程一直等待输入线程像管道中写入数据。若没有数据,就一致处在阻塞状态。
118+
接下来我们看写入线程。
119+
```
120+
static class WriteThread implements Runnable{
121+
122+
private PipedOutputStream pos;
123+
124+
public WriteThread(PipedOutputStream pos){
125+
this.pos = pos ;
126+
}
127+
128+
@Override
129+
public void run() {
130+
byte[] b = new byte[1024];
131+
try {
132+
if (null != pos){
133+
Thread.sleep(5000);
134+
pos.write("这是输入的数据".getBytes("utf-8"));
135+
System.err.println(getDate()+" 输入数据成功");
136+
pos.close();
137+
}
138+
}catch (IOException e){
139+
e.printStackTrace();
140+
} catch (InterruptedException e) {
141+
e.printStackTrace();
142+
}
143+
}
144+
}
145+
```
146+
接下来我们就看下控制台的输出。稍后我会将这个类push到https://github.com/Guolei1130/ATips/tree/master/Java 下面。
147+
![这里写图片描述](http://img.blog.csdn.net/20160806124516444)
148+
149+
### 4.SequenceInputStream
150+
```
151+
FileInputStream fis_1 = new FileInputStream(new File("./1.txt"));
152+
FileInputStream fis_2 = new FileInputStream(new File("./2.txt"));
153+
154+
FileOutputStream fos = new FileOutputStream(new File("./3.txt"));
155+
156+
//将两个或者多个合并.
157+
SequenceInputStream sis = new SequenceInputStream(fis_1,fis_2);
158+
159+
byte[] b = new byte[1024];
160+
while (sis.read(b) != -1){
161+
fos.write(b);
162+
}
163+
sis.close();
164+
fos.close();
165+
fis_1.close();
166+
fis_2.close();
167+
```
168+
将两个流合并成一个之后,(这里是有顺序的),输出的流就会按照输入流的顺序输出数据。
169+
### 5.Filter相关
170+
这个很重要,因为我们在平常使用过程中往往需要结合这些来用,一般的流只提供了read,write方法,而装饰之后的流则有更多的方法,如DataInputStrem,提供了readInt等,比较方便。还有就是BufferedInputStream使用缓存区,来防止每次都进行实际操作。
171+
**FilterInputStream**
172+
||功能 | 构造器参数| 比普通流增加了那些功能|
173+
|--|:--:|:-:|--:|
174+
|DataInputStream|读取基本数据类型 |InputStream|增加基础数据类型的读写 |
175+
|BufferedInputStream|使用缓存区|- |增加缓存区|
176+
|LineNumberInputStream |跟踪输入流中的行号 |- | 增加行号|
177+
|PushedbackInputStream |能弹出字节的缓冲区 |- | |
178+
179+
**FilterOutputStream**
180+
||功能 | 构造器参数| 比普通流增加了那些功能|
181+
|--|:--:|:-:|--:|
182+
|DataOutputStream|写入基本数据类型 |OutputStream|增加基础数据类型的读写 |
183+
|PrintStream |格式化参数 | -| |
184+
|BufferedOutputStream|使用缓存区|- |增加缓存区|
185+
186+
### 6.总结
187+
这一块虽然简单点,但是还是很重要的。
188+

0 commit comments

Comments
 (0)