Skip to content

Commit 7b099fe

Browse files
committed
🔖 JAVA IO 示例
1 parent c5315d8 commit 7b099fe

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
7+
/**
8+
* @author Zhang Peng
9+
*/
10+
public class BufferedReaderDemo {
11+
12+
public static void main(String args[]) throws IOException {
13+
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
14+
while (true) {
15+
System.out.print("请输入内容:");
16+
String str = buf.readLine();
17+
if (str.equalsIgnoreCase("exit")) {
18+
System.out.print("退出");
19+
break;
20+
}
21+
System.out.println("输入的内容为:" + str);
22+
}
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.OutputStream;
6+
import java.io.PrintStream;
7+
8+
/**
9+
* @author Zhang Peng
10+
*/
11+
public class PrintStreamDemo {
12+
13+
public static void main(String arg[]) throws Exception {
14+
final String filepath = "d:\\test.txt";
15+
// 如果现在是使用 FileOuputStream 实例化,意味着所有的数据都会输出到文件中
16+
OutputStream os = new FileOutputStream(new File(filepath));
17+
PrintStream ps = new PrintStream(os);
18+
ps.print("Hello ");
19+
ps.println("World!!!");
20+
ps.printf("姓名:%s;年龄:%d", "张三", 18);
21+
ps.close();
22+
}
23+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
import java.util.Scanner;
6+
7+
/**
8+
* @author Zhang Peng
9+
*/
10+
public class ScannerDemo {
11+
12+
public static void main(String args[]) {
13+
Scanner scan = new Scanner(System.in); // 从键盘接收数据
14+
int i = 0;
15+
float f = 0.0f;
16+
System.out.print("输入整数:");
17+
if (scan.hasNextInt()) { // 判断输入的是否是整数
18+
i = scan.nextInt(); // 接收整数
19+
System.out.println("整数数据:" + i);
20+
} else {
21+
System.out.println("输入的不是整数!");
22+
}
23+
24+
System.out.print("输入小数:");
25+
if (scan.hasNextFloat()) { // 判断输入的是否是小数
26+
f = scan.nextFloat(); // 接收小数
27+
System.out.println("小数数据:" + f);
28+
} else {
29+
System.out.println("输入的不是小数!");
30+
}
31+
32+
Date date = null;
33+
String str = null;
34+
System.out.print("输入日期(yyyy-MM-dd):");
35+
if (scan.hasNext("^\\d{4}-\\d{2}-\\d{2}$")) { // 判断
36+
str = scan.next("^\\d{4}-\\d{2}-\\d{2}$"); // 接收
37+
try {
38+
date = new SimpleDateFormat("yyyy-MM-dd").parse(str);
39+
} catch (Exception e) {}
40+
} else {
41+
System.out.println("输入的日期格式错误!");
42+
}
43+
System.out.println(date);
44+
}
45+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.io.OutputStream;
6+
import java.io.PrintStream;
7+
8+
/**
9+
* @author Zhang Peng
10+
*/
11+
public class SystemErrDemo {
12+
13+
public static void main(String args[]) throws IOException {
14+
OutputStream bos = new ByteArrayOutputStream(); // 实例化
15+
PrintStream ps = new PrintStream(bos); // 实例化
16+
System.setErr(ps); // 输出重定向
17+
System.err.print("此处有误");
18+
System.out.println(bos); // 输出内存中的数据
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
/**
7+
* System.in 示例
8+
* @author Zhang Peng
9+
*/
10+
public class SystemInDemo {
11+
12+
public static void main(String args[]) throws IOException {
13+
InputStream input = System.in;
14+
StringBuffer buf = new StringBuffer();
15+
System.out.print("请输入内容:");
16+
int temp = 0;
17+
while ((temp = input.read()) != -1) {
18+
char c = (char) temp;
19+
if (c == '\n') {
20+
break;
21+
}
22+
buf.append(c);
23+
}
24+
System.out.println("输入的内容为:" + buf);
25+
input.close();
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.dunwu.javase.io;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.OutputStream;
5+
import java.io.PrintStream;
6+
7+
/**
8+
* 重定向 System.out 输出流
9+
* @author Zhang Peng
10+
*/
11+
public class SystemOutDemo {
12+
13+
public static void main(String args[]) throws Exception {
14+
OutputStream out = new FileOutputStream("d:\\test.txt");
15+
PrintStream ps = new PrintStream(out);
16+
System.setOut(ps);
17+
System.out.println("人生若只如初见,何事秋风悲画扇");
18+
ps.close();
19+
out.close();
20+
}
21+
}

0 commit comments

Comments
 (0)