JAVA之IO--字节流(三)

使用字节流进行文件操作

1、读取文件

一般步骤:1)建立连接 File—>2)选择流 InputStream —>3)操作 byte[]+read—>4)关闭

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
@Test
public void readFile() {
// 1.建立连接 File
File pom = new File("pom.xml");
// 2.选择流
InputStream in = null;
try {
in = new FileInputStream(pom);
// 3.操作
byte[] bytes = new byte[1024]; // 每次读取1024个字节
int len = 0;// 接收读取的大小
while ((len = in.read(bytes, 0, 1024)) != -1) {
System.out.println(new String(bytes, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件不存在!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取文件失败!");
} finally {
//4.释放资源
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

2、写出文件

一般步骤:1)建立连接 File—>2)选择流 OutputStream —>3)write+flush—>4)关闭

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
@Test
public void writeFile() {
// 1.建立联系 File
File file = new File("test.txt");
// 2.选择流
OutputStream out = null;
try {
out = new FileOutputStream(file, true);// 表示写文件是追加还是覆盖
// 3.操作
String msg = "hello world!\n";
byte[] data = msg.getBytes();
out.write(data, 0, data.length);
out.flush();// 强制刷新出去
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件找不到!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件写出失败!");
}finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭失败!");
}
}
}

3、文件的拷贝

就是将文件的读取和文件的写出就是文件的拷贝。

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
@Test
public void copyFile() {
// 1.建立联系
File inFile = new File("pom.xml");
File outFile = new File("test.txt");
//只能拷贝文件
if(inFile.isFile){
return;
}
// 2.选择流
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(inFile);
out = new FileOutputStream(outFile);
// 3.操作
byte[] bytes = new byte[1024];
int len = 0;
while ((len = in.read(bytes, 0, 1024)) != -1) {
out.write(bytes, 0, len);
}
out.flush();// 强制刷新
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

4、文件夹的拷贝

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
@Test
public void copyFile2() {
File inFile = new File("src");
File outFile= new File("outSrc");
try {
copy(inFile,outFile);
} catch (IOException e) {
e.printStackTrace();
}
}
private void copy(File inFile, File outFile) throws IOException {
File nowOut = new File(outFile,inFile.getName()); //需要拷贝的文件名称
if(inFile.isDirectory()) {
nowOut.mkdirs(); //如果是目录就创建目录
for (File subFile : inFile.listFiles()) {
copy(subFile,nowOut); //递归调用
}
}else {
//如果是文件就进行拷贝
InputStream in = new FileInputStream(inFile);
OutputStream out = new FileOutputStream(nowOut);
byte[] bytes = new byte[1024];
int len = 0;
while((len=in.read(bytes, 0, 1024))!=-1) {
out.write(bytes, 0, len);
}
out.flush();
out.close();
in.close();
}
}