Skip to content

Commit 9e6fc7b

Browse files
committed
🔖 JAVA IO 示例
1 parent 662d678 commit 9e6fc7b

File tree

5 files changed

+328
-0
lines changed

5 files changed

+328
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.io.*;
4+
5+
/**
6+
* 将 InputStream 转为 Reader
7+
* @author Zhang Peng
8+
*/
9+
public class InputStreamReaderDemo {
10+
11+
public static void main(String args[]) throws IOException {
12+
File f = new File("d:" + File.separator + "test.txt");
13+
Reader reader = new InputStreamReader(new FileInputStream(f));
14+
char c[] = new char[1024];
15+
int len = reader.read(c);
16+
reader.close();
17+
System.out.println(new String(c, 0, len));
18+
}
19+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.io.*;
4+
5+
/**
6+
* 对象输入输出流,一般用于对象序列化
7+
* @author Zhang Peng
8+
*/
9+
public class ObjectStream {
10+
11+
public static class Person implements Serializable {
12+
13+
private String name;
14+
private int age;
15+
16+
public Person(String name, int age) {
17+
this.name = name;
18+
this.age = age;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return "姓名:" + this.name + ";年龄:" + this.age;
24+
}
25+
}
26+
27+
public static void writeObject(String filepath, Object obj[]) throws Exception {
28+
// 1.使用 File 类绑定一个文件
29+
File f = new File(filepath);
30+
31+
// 2.把 File 对象绑定到流对象上
32+
OutputStream out = new FileOutputStream(f);
33+
ObjectOutputStream oos = new ObjectOutputStream(out);
34+
35+
// 3.进行读或写操作
36+
oos.writeObject(obj);
37+
38+
// 4.关闭流
39+
oos.close();
40+
}
41+
42+
public static Object[] readObject(String filepath) throws Exception {
43+
// 1.使用 File 类绑定一个文件
44+
File f = new File(filepath);
45+
46+
// 2.把 File 对象绑定到流对象上
47+
InputStream input = new FileInputStream(f);
48+
ObjectInputStream ois = new ObjectInputStream(input);
49+
50+
// 3.进行读或写操作
51+
Object[] objects = (Object[]) ois.readObject();
52+
53+
// 4.关闭流
54+
ois.close();
55+
return objects;
56+
}
57+
58+
public static void main(String args[]) throws Exception {
59+
final String filepath = "d:\\object.txt";
60+
Person per[] = {new Person("张三", 30), new Person("李四", 31), new Person("王五", 32)};
61+
writeObject(filepath, per);
62+
Object o[] = readObject(filepath);
63+
for (int i = 0; i < o.length; i++) {
64+
Person p = (Person) o[i];
65+
System.out.println(p);
66+
}
67+
}
68+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.io.*;
4+
5+
/**
6+
* 将 OutputStream 转为 Writer
7+
* @author Zhang Peng
8+
*/
9+
public class OutputStreamWriterDemo {
10+
11+
public static void main(String args[]) throws IOException {
12+
File f = new File("d:" + File.separator + "test.txt");
13+
Writer out = new OutputStreamWriter(new FileOutputStream(f));
14+
out.write("hello world!!");
15+
out.close();
16+
}
17+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.io.*;
4+
5+
/**
6+
* Reader 和 Writer 示例
7+
* @author Zhang Peng
8+
*/
9+
public class ReaderAndWriterDemo {
10+
11+
public static void output(String filepath) throws IOException {
12+
// 1.使用 File 类绑定一个文件
13+
File f = new File(filepath);
14+
15+
// 2.把 File 对象绑定到流对象上
16+
Writer out = new FileWriter(f);
17+
// Writer out = new FileWriter(f, true); // 追加内容方式
18+
19+
// 3.进行读或写操作
20+
String str = "Hello World!!!\r\n";
21+
out.write(str);
22+
23+
// 4.关闭流
24+
// 字符流操作时使用了缓冲区,并在关闭字符流时会强制将缓冲区内容输出
25+
// 如果不关闭流,则缓冲区的内容是无法输出的
26+
// 如果想在不关闭流时,将缓冲区内容输出,可以使用 flush 强制清空缓冲区
27+
out.flush();
28+
out.close();
29+
}
30+
31+
public static char[] input(String filepath) throws IOException {
32+
// 1.使用 File 类绑定一个文件
33+
File f = new File(filepath);
34+
35+
// 2.把 File 对象绑定到流对象上
36+
Reader input = new FileReader(f);
37+
38+
// 3.进行读或写操作
39+
int temp = 0; // 接收每一个内容
40+
int len = 0; // 读取内容
41+
char[] c = new char[1024];
42+
while ((temp = input.read()) != -1) {
43+
// 如果不是-1就表示还有内容,可以继续读取
44+
c[len] = (char) temp;
45+
len++;
46+
}
47+
System.out.println("文件字符数为:" + len);
48+
49+
// 4.关闭流
50+
input.close();
51+
52+
return c;
53+
}
54+
55+
public static void main(String[] args) throws IOException {
56+
String filepath = "d:\\test.txt";
57+
58+
output(filepath);
59+
System.out.println("内容为:" + new String(input(filepath)));
60+
}
61+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.io.*;
4+
import java.util.zip.ZipEntry;
5+
import java.util.zip.ZipFile;
6+
import java.util.zip.ZipInputStream;
7+
import java.util.zip.ZipOutputStream;
8+
9+
/**
10+
* @author Zhang Peng
11+
*/
12+
public class ZipStreamDemo {
13+
14+
public static final String ZIP_FILE_PATH = "d:\\zipdemo.zip";
15+
16+
public static void demo01(String zipfilepath) throws IOException {
17+
File file = new File(zipfilepath);
18+
ZipFile zipFile = new ZipFile(file);
19+
ZipEntry entry = zipFile.getEntry("mldn.txt");
20+
System.out.println("压缩文件的名称:" + zipFile.getName());
21+
22+
File outputFile = new File("d:" + File.separator + "mldn_unzip.txt");
23+
OutputStream out = new FileOutputStream(outputFile); // 实例化输出流
24+
InputStream input = zipFile.getInputStream(entry); // 得到一个压缩实体的输入流
25+
int temp = 0;
26+
while ((temp = input.read()) != -1) {
27+
out.write(temp);
28+
}
29+
input.close(); // 关闭输入流
30+
out.close(); // 关闭输出流
31+
}
32+
33+
/**
34+
* 压缩一个文件
35+
*/
36+
public static void output1(String filepath, String zipfilepath) throws Exception {
37+
// 1.使用 File 类绑定一个文件
38+
// 定义要压缩的文件
39+
File file = new File(filepath);
40+
// 定义压缩文件名称
41+
File zipFile = new File(zipfilepath);
42+
43+
// 2.把 File 对象绑定到流对象上
44+
InputStream input = new FileInputStream(file);
45+
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
46+
47+
// 3.进行读或写操作
48+
zipOut.putNextEntry(new ZipEntry(file.getName()));
49+
zipOut.setComment("This is a zip file.");
50+
int temp = 0;
51+
while ((temp = input.read()) != -1) { // 读取内容
52+
zipOut.write(temp); // 压缩输出
53+
}
54+
55+
// 4.关闭流
56+
input.close();
57+
zipOut.close();
58+
}
59+
60+
/**
61+
* 读取实体为一个文件的压缩包
62+
*/
63+
public static void input1(String zipfilepath, String filepath) throws Exception {
64+
// 1.使用 File 类绑定一个文件
65+
File zipFile = new File(zipfilepath);
66+
67+
// 2.把 File 对象绑定到流对象上
68+
ZipInputStream input = new ZipInputStream(new FileInputStream(zipFile));
69+
70+
// 3.进行读或写操作
71+
ZipEntry entry = input.getNextEntry(); // 得到一个压缩实体
72+
System.out.println("压缩实体名称:" + entry.getName());
73+
74+
// 4.关闭流
75+
input.close();
76+
}
77+
78+
/**
79+
* 压缩一个目录
80+
*/
81+
public static void output2(String dirpath, String zipfilepath) throws Exception {
82+
// 1.使用 File 类绑定一个文件
83+
// 定义要压缩的文件夹
84+
File file = new File(dirpath);
85+
// 定义压缩文件名称
86+
File zipFile = new File(zipfilepath);
87+
88+
// 2.把 File 对象绑定到流对象上
89+
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
90+
zipOut.setComment("This is zip folder.");
91+
92+
// 3.进行读或写操作
93+
int temp = 0;
94+
if (file.isDirectory()) { // 判断是否是文件夹
95+
File lists[] = file.listFiles(); // 列出全部文件
96+
for (int i = 0; i < lists.length; i++) {
97+
InputStream input = new FileInputStream(lists[i]);
98+
// 设置ZipEntry对象
99+
zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + lists[i].getName()));
100+
while ((temp = input.read()) != -1) {
101+
zipOut.write(temp);
102+
}
103+
input.close();
104+
}
105+
}
106+
107+
// 4.关闭流
108+
zipOut.close();
109+
}
110+
111+
/**
112+
* 解压实体为一个目录的压缩包
113+
*/
114+
public static void input2(String zipfilepath, String dirpath) throws Exception {
115+
// 1.使用 File 类绑定一个文件
116+
File file = new File(zipfilepath);
117+
ZipFile zipFile = new ZipFile(file);
118+
119+
// 2.把 File 对象绑定到流对象上
120+
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
121+
122+
// 3.进行读或写操作
123+
ZipEntry entry = null;
124+
while ((entry = zis.getNextEntry()) != null) { // 得到一个压缩实体
125+
System.out.println("解压缩" + entry.getName() + "文件。");
126+
// 定义输出的文件路径
127+
File outFile = new File(dirpath + File.separator + entry.getName());
128+
if (!outFile.getParentFile().exists()) { // 如果输出文件夹不存在
129+
outFile.getParentFile().mkdirs(); // 创建文件夹
130+
}
131+
if (!outFile.exists()) { // 判断输出文件是否存在
132+
outFile.createNewFile(); // 创建文件
133+
}
134+
InputStream input = zipFile.getInputStream(entry); // 得到每一个实体的输入流
135+
OutputStream out = new FileOutputStream(outFile); // 实例化文件输出流
136+
int temp = 0;
137+
while ((temp = input.read()) != -1) {
138+
out.write(temp);
139+
}
140+
input.close(); // 关闭输入流
141+
out.close(); // 关闭输出流
142+
}
143+
144+
// 4.关闭流
145+
zis.close();
146+
}
147+
148+
public static void main(String[] args) throws Exception {
149+
final String filepath = "d:\\demo.txt";
150+
final String zipfilepath = "d:\\demo.zip";
151+
152+
final String dirpath = "d:\\demo2";
153+
final String dirpath2 = "d:\\new";
154+
final String zipfilepath2 = "d:\\demo2.zip";
155+
156+
// demo01(ZIP_FILE_PATH);
157+
output1(filepath, zipfilepath);
158+
input1(zipfilepath, filepath);
159+
160+
output2(dirpath, zipfilepath2);
161+
input2(zipfilepath2, dirpath2);
162+
}
163+
}

0 commit comments

Comments
 (0)