文件/IO流

Java文件、IO流

字符集

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
字符集
各个国家为自己的语言创造的一套编号规则
计算机底层不能直接存储字符,只能存储二进制

1B=8bit
计算机最小单位B(字节)

英文
8bit为一组,1个字节
2^8=256 1个字节可以表示256种情况

a 97
A 65
0 48
ASCII

中文
一般16bit 2个字节表示一个中文字符
GBK编码
兼容ASCII


Unicode编码(万国码)
变种 -> UTF-8
一个中文一般占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
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
io

File类只能操作文件本身,不能读写

按方向分类
输出流:把程序(内存)中的内容写出到磁盘

输入流:把磁盘/网络的内容读入程序(内存)

按内容分类
字节流:流中的数据最小单位是字节
字符流:最小单位是字符(文本文件)

按方向、内容分类:
字节输入流 字节输出流 字符输入流 字符输出流
InputStream | OutputStream | Reader | Writer
| | |
上面是抽象类,下面是实现类 | |
| | |
FileInputStream | FileOutputStream | FileReader | FileWriter
| | |
BufferedInputStream | BufferedOutputStream| BufferedReader | BufferedWriter(提高性能)
| | |
| | InputStreamReader | OutputStreamWriter(解决乱码)

构造可以传File或者路径


字节流适合复制文件

字符流适合中文


字节缓冲输入流
有一个默认的DEFAULT_BUFFER_SIZE = 8192 8KB的缓冲池

从磁盘读文件,内容先到缓冲池(在内存),程序(内存)再从缓冲池读取

字节缓冲输出流
从程序(内存)向磁盘写数据,先写入内存中的缓冲池(也是8KB),再由操作系统写入磁盘

字符缓冲输入流 √
多了一个换行

字符缓冲输出流



输出流设置追加,要在原始的构造器添加true(FileInputStream(_, true)....)


InputStreamReader字符输入转换流
可以把字节流按照默认或指定的编码转换成字符流
可以解决乱码
如果文件是GBK编码,程序代码是utf-8,需要InputStreamReader包装InputStream,设置编码
OutputStreamReader字符输出转换流
能指定编码把字节输出流转换成字符输出流


对象序列化ObjectOutputStream
把java对象实例直接存到文件
对象反序列化ObjectInputStream
把文件存储的对象实例恢复为java对象实例
目标对象
需要实现Serialize接口,如果成员变量加了transient修饰,这个变量不参与序列化(安全)
private static final long serialVersionUID序列化版本号



PrintStream打印流(字节)
PrintWriter打印流(字符)
极其强大
  • 文件=文件+文件夹

英文数字占一个字节
中文占

文件路径
1正斜杠/
2反斜杠\(即需要转义)
3分隔符api File.separator

.length()字节数

new file文件夹,得到的大小只是文件夹本身的大小,不包含里面的文件

不能删除非空文件夹

.lastModified()最后修改时间

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
/**
* Created by KebabShell
* on 2020/4/9 下午 04:44
*/
public class FileTest {
@Test
public void test0(){
File file = new File("E:\\影音\\me.jpg");
System.out.println(file.length());
}
@Test
public void test1(){
File file = new File("src/cn/kebabshell/test/file/字符集.txt");
System.out.println(file.exists());
System.out.println(file.length());
}
@Test
public void test2(){
File file = new File("src/cn/kebabshell/test/file");
System.out.println(file.length());
System.out.println(file.exists());
}
@Test
public void test3(){
File file = new File("src/cn/kebabshell/test/file/字符集.txt");
File file1 = new File("E:\\影音\\me.jpg");
File file2 = new File("src/cn/kebabshell/test/file");
System.out.println(file.getAbsolutePath());
System.out.println(file.getPath());
System.out.println(file1.getPath());
// D:\IDEA\AllPojects\JavaSourceLearn\src\cn\kebabshell\test\file
// src\cn\kebabshell\test\file获取文件定义时使用的路径
// E:\影音\me.jpg
System.out.println(file.getName());//带后缀
System.out.println(file.length());//拿字节数

System.out.println(file.isFile());
System.out.println(file2.isDirectory());
}
@Test
public void test4() throws IOException {
File file = new File("src/cn/kebabshell/test/file/test.txt");
System.out.println(file.createNewFile());
}
@Test
public void test5(){
File file = new File("src/cn/kebabshell/test/file/test");
System.out.println(file.mkdirs());
}
@Test
public void test6(){
//遍历
File dir = new File("src/cn/kebabshell/test/file");

//取到的只是字符串
String[] list = dir.list();
for (String fileName :
list) {
System.out.println(fileName);
}//只是一级目录

//取到的是File对象
File[] listFiles = dir.listFiles();
for (File f :
listFiles) {
System.out.print(f.length() + "----");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//最后修改时间
System.out.println(format.format(f.lastModified()));
}

//递归搜索
File gotFile = searchFile(dir, "test.txt");
if (gotFile != null){
System.out.println(gotFile.length());
}

}
File searchFile(File dir, String name){
if (dir.exists() && dir.isDirectory()){
File[] listFiles = dir.listFiles();
if (listFiles != null && listFiles.length != 0){
for (File file : listFiles) {
if (file.isFile()){
if (file.getName().equals(name)){
return file;
}
}else {
searchFile(file, name);
}
}
}
}
return null;
}
}
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
/**
* Created by KebabShell
* on 2020/4/11 下午 09:31
*/
public class CopyDirTest {
@Test
public void test0() {

copyDir(new File("src/cn/kebabshell/test/file/cptest"),
new File("src/cn/kebabshell/test/file/cptest_bak"));
}


public void copyDir(File srcDir, File destDir) {
//判断是否合法
if (srcDir.exists() && srcDir.isDirectory()) {
//创建目标文件夹
destDir.mkdirs();
//拿到一级文件
File[] files = srcDir.listFiles();
//判断合法性,有些操作系统用户没有权限,会返回null或者其他
if (files != null && files.length > 0) {
//遍历数组
for (File file : files) {
//如果是文件,复制
if (file.isFile()) {
copyFile(file, new File(destDir, file.getName()));
} else {
//如果是目录
copyDir(file, new File(destDir, file.getName()));
}
}
}
}
}

public void copyFile(File srcFile, File destDir) {
//复制,使用缓冲流,用一个一个字节
try (InputStream fis = new FileInputStream(srcFile);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream fos = new FileOutputStream(destDir);
BufferedOutputStream bos = new BufferedOutputStream(fos)) {

byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}

System.out.println("success");


} catch (Exception e) {
e.printStackTrace();
}
}
}