Skip to content

Commit 662d678

Browse files
committed
🔖 JAVA IO 示例
1 parent 726db60 commit 662d678

File tree

8 files changed

+193
-226
lines changed

8 files changed

+193
-226
lines changed

codes/basics/src/main/java/io/github/dunwu/javase/io/ByteArrayStreamDemo.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
/**
88
* 内存操作流
9-
*
109
* @author Zhang Peng
1110
* @date 2018/4/26
1211
*/
@@ -32,4 +31,4 @@ public static void main(String args[]) {
3231
}
3332
System.out.println(newStr);
3433
}
35-
};
34+
}

codes/basics/src/main/java/io/github/dunwu/javase/io/DataInputStreamDemo.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

codes/basics/src/main/java/io/github/dunwu/javase/io/DataOutputStreamDemo.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.io.*;
4+
5+
/**
6+
* 数据输入流示例
7+
* @author Zhang Peng
8+
*/
9+
public class DataStreamDemo {
10+
11+
public static final String FILEPATH = "d:\\order.txt";
12+
13+
private static void output(String filepath) throws IOException {
14+
// 1.使用 File 类绑定一个文件
15+
File f = new File(filepath);
16+
17+
// 2.把 File 对象绑定到流对象上
18+
DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));
19+
20+
// 3.进行读或写操作
21+
String names[] = {"衬衣", "手套", "围巾"};
22+
float prices[] = {98.3f, 30.3f, 50.5f};
23+
int nums[] = {3, 2, 1};
24+
for (int i = 0; i < names.length; i++) {
25+
dos.writeChars(names[i]);
26+
dos.writeChar('\t');
27+
dos.writeFloat(prices[i]);
28+
dos.writeChar('\t');
29+
dos.writeInt(nums[i]);
30+
dos.writeChar('\n');
31+
}
32+
33+
// 4.关闭流
34+
dos.close();
35+
}
36+
37+
private static void input(String filepath) throws IOException {
38+
// 1.使用 File 类绑定一个文件
39+
File f = new File(filepath);
40+
41+
// 2.把 File 对象绑定到流对象上
42+
DataInputStream dis = new DataInputStream(new FileInputStream(f));
43+
44+
// 3.进行读或写操作
45+
String name = null; // 接收名称
46+
float price = 0.0f; // 接收价格
47+
int num = 0; // 接收数量
48+
char temp[] = null; // 接收商品名称
49+
int len = 0; // 保存读取数据的个数
50+
char c = 0; // '\u0000'
51+
try {
52+
while (true) {
53+
temp = new char[200]; // 开辟空间
54+
len = 0;
55+
while ((c = dis.readChar()) != '\t') { // 接收内容
56+
temp[len] = c;
57+
len++; // 读取长度加1
58+
}
59+
name = new String(temp, 0, len); // 将字符数组变为String
60+
price = dis.readFloat(); // 读取价格
61+
dis.readChar(); // 读取\t
62+
num = dis.readInt(); // 读取int
63+
dis.readChar(); // 读取\n
64+
System.out.printf("名称:%s;价格:%5.2f;数量:%d\n", name, price, num);
65+
}
66+
} catch (EOFException e) {
67+
System.out.println("结束");
68+
} catch (IOException e) {
69+
e.printStackTrace();
70+
}
71+
72+
// 4.关闭流
73+
dis.close();
74+
}
75+
76+
public static void main(String args[]) throws IOException {
77+
output(FILEPATH);
78+
input(FILEPATH);
79+
}
80+
}

codes/basics/src/main/java/io/github/dunwu/javase/io/FileInputStreamDemo.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

codes/basics/src/main/java/io/github/dunwu/javase/io/FileOutputStreamDemo.java

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.io.*;
4+
5+
/**
6+
* 文件输入输出流
7+
* @author Zhang Peng
8+
*/
9+
public class FileStreamDemo {
10+
11+
private static final String FILEPATH = "d:\\test.txt";
12+
13+
public static void output(String filepath) throws IOException {
14+
// 第1步、使用File类找到一个文件
15+
File f = new File(filepath);
16+
17+
// 第2步、通过子类实例化父类对象
18+
OutputStream out = new FileOutputStream(f);
19+
// 实例化时,默认为覆盖原文件内容方式;如果添加true参数,则变为对原文件追加内容的方式。
20+
// OutputStream out = new FileOutputStream(f, true);
21+
22+
// 第3步、进行写操作
23+
String str = "Hello World\r\n";
24+
byte[] bytes = str.getBytes();
25+
out.write(bytes);
26+
27+
// 第4步、关闭输出流
28+
out.close();
29+
}
30+
31+
public static void input(String filepath) throws IOException {
32+
// 第1步、使用File类找到一个文件
33+
File f = new File(filepath);
34+
35+
// 第2步、通过子类实例化父类对象
36+
InputStream input = new FileInputStream(f);
37+
38+
// 第3步、进行读操作
39+
// 有三种读取方式,体会其差异
40+
byte[] bytes = new byte[(int) f.length()];
41+
int len = input.read(bytes); // 读取内容
42+
System.out.println("读入数据的长度:" + len);
43+
44+
// 第4步、关闭输入流
45+
input.close();
46+
System.out.println("内容为:\n" + new String(bytes));
47+
}
48+
49+
public static void main(String args[]) throws Exception {
50+
output(FILEPATH);
51+
input(FILEPATH);
52+
}
53+
}

0 commit comments

Comments
 (0)