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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package _8javaio;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class Main {
public static byte[] getFileDataAsBytes(File f)
{
byte[] data = new byte[(int)f.length()];
try(InputStream is =new FileInputStream(f))
{
int n;
while((n=is.read(data))!=-1)
{
System.out.println("read"+n+"bytes");
}

}catch(IOException e)
{
e.printStackTrace();
}
return data;
}
static void printFiles(File[] files) {
System.out.println("==========");
if (files != null) {
for (File f : files) {
System.out.println(f);
}
}
System.out.println("==========");
}

public static void main(String[] args) throws Exception{
//File对象
File f = new File("..");
System.out.println(f.getPath()); //构造方法传入路径
System.out.println(f.getAbsolutePath()); //返回绝对路径
System.out.println(f.getCanonicalPath());//返回规范绝对路径
System.out.println(File.separator); //返回当前平台系统路径分隔符

// File f1 = new File("C:\\Windows");
// File f2 = new File("C:\\Windows\\notepad.exe");
// File f3 = new File("C:\\Windows\\nothing");
// System.out.println(f1.isFile()); //false 文件
// System.out.println(f1.isDirectory()); //true 目录
// System.out.println(f2.isFile()); //true
// System.out.println(f2.isDirectory()); //false
// System.out.println(f3.isFile()); //false
// System.out.println(f3.isDirectory()); //false


//文件权限和大小
// System.out.println(f2.canRead()); //是否可读
// System.out.println(f2.canWrite()); //是否可写
// System.out.println(f2.canExecute());//是否可执行
// System.out.println(f2.length()); //文件字节大小

//创建删除文件
File file = new File("test/test.txt"); //与src同一级
if (file.createNewFile()) {
// 文件创建成功:
System.out.println("create success");
System.out.println(file.getAbsolutePath());
// TODO:
if (file.delete()) {
// 删除文件成功:
System.out.println("delete success");
}
}

//创建临时文件
// File f4 = File.createTempFile("tmp-", ".txt"); // 提供临时文件的前缀和后缀
// f4.deleteOnExit(); // JVM退出时自动删除
// System.out.println(f4.isFile());
// System.out.println(f4.getAbsolutePath());

//遍历文件和目录
// File f5 = new File("C:Windows");
//
// File[] fs1 = f5.listFiles(); // 列出所有文件和子目录
// printFiles(fs1);
// File[] fs2 = f5.listFiles(new FilenameFilter() { // 仅列出.exe文件
// public boolean accept(File dir, String name) {
// return name.endsWith(".exe"); // 返回true表示接受该文件
// }
// });
// printFiles(fs2);

//创建和删除目录
new File("hello").mkdir();
new File("hello/le").mkdirs();
new File("hello/le").delete();
new File("hello").delete();


//Path
Path p1 = Paths.get(".", "project", "study"); // 构造一个Path对象
System.out.println(p1);
Path p2 = p1.toAbsolutePath(); // 转换为绝对路径
System.out.println(p2);
Path p3 = p2.normalize(); // 转换为规范路径
System.out.println(p3);
File f6 = p3.toFile(); // 转换为File对象
System.out.println(f6);
for (Path p : Paths.get("..").toAbsolutePath()) { // 可以直接遍历Path
System.out.println(" " + p);
}


/**InputStream*/
//字节读文件
// 创建一个FileInputStream对象:
File file2 = new File("test/readme.txt");
file2.createNewFile();
try (InputStream input = new FileInputStream("test/readme.txt")) {
int n;
while ((n = input.read()) != -1) {
System.out.println(n);
}
} // 编译器在此自动为我们写入finally并调用close()


//缓冲读文件
try (InputStream input = new FileInputStream("test/readme.txt")) {
// 定义1000个字节大小的缓冲区:
byte[] buffer = new byte[1000];
int n;
while ((n = input.read(buffer)) != -1) { // 读取到缓冲区
System.out.println("read " + n + " bytes.");
}
}
//模拟InputStream
String s;
byte[] data = { 72, 101, 108, 108, 111, 33 };
try (InputStream input = new ByteArrayInputStream(data)) {
int n;
StringBuilder sb = new StringBuilder();
while ((n = input.read()) != -1) {
sb.append((char) n);
System.out.println((char)n);
}
s = sb.toString();
}
System.out.println(s);


/**OutputStream*/
//一次写入一个字节
OutputStream output = new FileOutputStream("test/readme.txt");
output.write(72); // H
output.write(101); // e
output.write(108); // l
output.write(108); // l
output.write(111); // o
output.close();

//一次写入若干字节
try (OutputStream output1 = new FileOutputStream("test/readme.txt")) {
output1.write("Hello!".getBytes("UTF-8")); // Hello
} // 编译器在此自动为我们写入finally并调用close()

//模拟OutputStream
byte[] data3;
try (ByteArrayOutputStream output3 = new ByteArrayOutputStream()) {
output3.write("Hello ".getBytes("UTF-8"));
output3.write("world!".getBytes("UTF-8"));
data3 = output3.toByteArray();
}
System.out.println(new String(data3, "UTF-8"));


//Filter模式
InputStream file7 = new FileInputStream("test/test.gz");
InputStream buffered = new BufferedInputStream(file7);
InputStream gzip = new GZIPInputStream(buffered);
System.out.println(gzip);
file7.close();

/**操作Zip*/
//读取zip
try (ZipInputStream zip = new ZipInputStream(new FileInputStream("test/test.zip"))) {
ZipEntry entry = null;
while ((entry = zip.getNextEntry()) != null) {
String name = entry.getName();
System.out.println("name:"+name);
if (!entry.isDirectory()) {
int n;
while ((n = zip.read()) != -1) {
System.out.println(n);
}
}
}
}
//写入zip
try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream("test/log.zip"))) {
File ff = new File("log");
File[] files = ff.listFiles();
for (File file1 : files) {
zip.putNextEntry(new ZipEntry(file1.getName()));
zip.write(getFileDataAsBytes(file1));
zip.closeEntry();
}
}

/**序列化*/
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try (ObjectOutputStream output1 = new ObjectOutputStream(buffer)) {
// 写入int:
output1.writeInt(12345);
// 写入String:
output1.writeUTF("Hello");
// 写入Object:
output1.writeObject(Double.valueOf(123.456));
}
System.out.println(Arrays.toString(buffer.toByteArray()));


/**反序列化*/
ByteArrayInputStream input1 = new ByteArrayInputStream(buffer.toByteArray());
try (ObjectInputStream input2 = new ObjectInputStream(input1)) {
int n1 = input2.readInt();
String s1 = input2.readUTF();
Double d1 = (Double) input2.readObject();
System.out.println(n1+s1+d1);
}


/**Reader 字符流*/
//FileReader
try (Reader reader = new FileReader("test/test.txt")) {
char[] buffer1 = new char[1000];
int n;
while ((n = reader.read(buffer1)) != -1) {
System.out.println("read " + n + " chars.");
}
}
//CharArrayReader 模拟Reader
try (Reader reader = new CharArrayReader("Hello".toCharArray())) {
System.out.println(reader);
}
//StringReader
try (Reader reader = new StringReader("Hello")) {
System.out.println(reader);
}
//InputStreamReader
try (Reader reader = new InputStreamReader(new FileInputStream("test/test.txt"), "UTF-8")) {
// TODO:
System.out.println(reader);
}

/**Wirter 字符流*/
//FileWriter
try (Writer writer = new FileWriter("test/test.txt")) {
writer.write('H'); // 写入单个字符
writer.write("Hello".toCharArray()); // 写入char[]
writer.write("Hello"); // 写入String
writer.write("你好");
}

//CharArrayWriter
try (CharArrayWriter writer = new CharArrayWriter()) {
writer.write(65);
writer.write(66);
writer.write(67);
char[] data4 = writer.toCharArray(); // { 'A', 'B', 'C' }
System.out.println(data4);
}

//StringWriter
//OutputStreamWriter
try (Writer writer = new OutputStreamWriter(new FileOutputStream("test/test.txt"), "UTF-8")) {
// TODO:
System.out.println(writer);
}

/**PrintStream和PrintWriter*/
System.out.print(12345); // 输出12345
System.out.print(new Object()); // 输出类似java.lang.Object@3c7a835a
System.out.println("Hello"); // 输出Hello并换行

StringWriter buffer4 = new StringWriter();
try (PrintWriter pw = new PrintWriter(buffer4)) {
pw.println("Hello");
pw.println(12345);
pw.println(true);
}
System.out.println(buffer4.toString());

}
}