Skip to content

Commit a76f7a4

Browse files
committed
[add] add io examples
1 parent 724ffb2 commit a76f7a4

File tree

6 files changed

+284
-0
lines changed

6 files changed

+284
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.brianway.learning.java.io;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
/**
8+
* Created by brian on 16/11/28.
9+
*/
10+
public class BufferedInputFile {
11+
public static String read(String filename) throws IOException {
12+
BufferedReader in = new BufferedReader(
13+
new FileReader(filename));
14+
String s;
15+
StringBuilder sb = new StringBuilder();
16+
while ((s = in.readLine()) != null) {
17+
sb.append(s).append("\n");
18+
}
19+
in.close();
20+
return sb.toString();
21+
}
22+
23+
public static void main(String[] args) throws IOException {
24+
String file = BufferedInputFile.class.getResource("/").getPath()
25+
+ "/infile.txt";
26+
System.out.print(read(file));
27+
}
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.brianway.learning.java.io;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.io.PrintWriter;
7+
8+
/**
9+
* Created by brian on 16/11/28.
10+
*
11+
* 从标准输入中读取,并将 System.out 转换成 PrintWriter
12+
*/
13+
public class Echo {
14+
public static void main(String[] args) throws IOException {
15+
BufferedReader stdin = new BufferedReader(
16+
new InputStreamReader(System.in));
17+
PrintWriter out = new PrintWriter(System.out, true);// 开启自动清空
18+
19+
String s;
20+
while ((s = stdin.readLine()) != null && s.length() != 0) {
21+
//System.out.println(s);
22+
out.println(s);
23+
}
24+
}
25+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.brianway.learning.java.io;
2+
3+
import java.io.BufferedReader;
4+
import java.io.BufferedWriter;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
import java.io.PrintWriter;
8+
import java.io.StringReader;
9+
10+
/**
11+
* Created by brian on 16/11/28.
12+
*/
13+
public class FileOutput {
14+
15+
/**
16+
* 基本的文件输出
17+
*/
18+
public static void basic(String infile, String outfile) throws IOException {
19+
BufferedReader in = new BufferedReader(
20+
new StringReader(
21+
BufferedInputFile.read(infile)));
22+
PrintWriter out = new PrintWriter(
23+
new BufferedWriter(new FileWriter(outfile)));
24+
int lineCount = 1;
25+
String s;
26+
while ((s = in.readLine()) != null) {
27+
out.println(lineCount++ + ": " + s);
28+
}
29+
out.close();
30+
System.out.println(BufferedInputFile.read(outfile));
31+
}
32+
33+
/**
34+
* 文本文件输出的快捷方式
35+
*/
36+
public static void shortcut(String infile, String outfile)throws IOException{
37+
BufferedReader in = new BufferedReader(
38+
new StringReader(
39+
BufferedInputFile.read(infile)));
40+
PrintWriter out = new PrintWriter(outfile);
41+
int lineCount = 1;
42+
String s;
43+
while ((s = in.readLine()) != null) {
44+
out.println(lineCount++ + ": " + s);
45+
}
46+
out.close();
47+
System.out.println(BufferedInputFile.read(outfile));
48+
}
49+
50+
public static void main(String[] args) throws IOException {
51+
String parent = BufferedInputFile.class.getResource("/").getPath();
52+
String infile = parent + "/infile.txt";
53+
54+
String outfile = parent + "/BasicOut";
55+
basic(infile, outfile);
56+
57+
outfile = parent+"ShortcutOut";
58+
shortcut(infile,outfile);
59+
}
60+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.brianway.learning.java.io;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.DataInputStream;
5+
import java.io.IOException;
6+
import java.io.StringReader;
7+
8+
/**
9+
* Created by brian on 16/11/28.
10+
* 内存输入
11+
*/
12+
13+
public class MemoryInput {
14+
15+
/**
16+
* 从内存输入
17+
*/
18+
public static void useStringReader(String filename) {
19+
try {
20+
StringReader in = new StringReader(
21+
BufferedInputFile.read(filename));
22+
int c;
23+
while ((c = in.read()) != -1)
24+
System.out.print((char) c);
25+
} catch (IOException e) {
26+
e.printStackTrace();
27+
}
28+
29+
}
30+
31+
/**
32+
* 格式化的内存输入
33+
*/
34+
public static void readFormattedInput(String filename) {
35+
try {
36+
DataInputStream in = new DataInputStream(
37+
new ByteArrayInputStream(
38+
BufferedInputFile.read(filename)
39+
.getBytes()));
40+
while (in.available() != 0) {
41+
System.out.print((char) in.readByte());
42+
}
43+
} catch (IOException e) {
44+
e.printStackTrace();
45+
}
46+
}
47+
48+
public static void main(String[] args)
49+
throws IOException {
50+
String filename = BufferedInputFile.class.getResource("/").getPath()
51+
+ "/infile.txt";
52+
useStringReader(filename);
53+
readFormattedInput(filename);
54+
}
55+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.brianway.learning.java.io;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.io.PrintWriter;
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.TreeSet;
11+
12+
/**
13+
* Static functions for reading and writing text files as
14+
* a single string, and treating a file as an ArrayList.
15+
*/
16+
public class TextFile extends ArrayList<String> {
17+
// Read a file as a single string:
18+
public static String read(String fileName) {
19+
StringBuilder sb = new StringBuilder();
20+
try {
21+
BufferedReader in = new BufferedReader(
22+
new FileReader(
23+
new File(fileName).getAbsoluteFile()));
24+
try {
25+
String s;
26+
while ((s = in.readLine()) != null) {
27+
sb.append(s);
28+
sb.append("\n");
29+
}
30+
} finally {
31+
try {
32+
in.close();
33+
} catch (IOException e) {
34+
e.printStackTrace();
35+
}
36+
37+
}
38+
} catch (IOException e) {
39+
throw new RuntimeException(e);
40+
}
41+
return sb.toString();
42+
}
43+
44+
// Write a single file in one method call:
45+
public static void write(String fileName, String text) {
46+
try {
47+
PrintWriter out = new PrintWriter(
48+
new File(fileName).getAbsoluteFile());
49+
try {
50+
out.print(text);
51+
} finally {
52+
out.close();
53+
}
54+
} catch (IOException e) {
55+
throw new RuntimeException(e);
56+
}
57+
}
58+
59+
// Read a file, split by any regular expression:
60+
public TextFile(String fileName, String splitter) {
61+
super(Arrays.asList(read(fileName).split(splitter)));
62+
// Regular expression split() often leaves an empty
63+
// String at the first position:
64+
if (get(0).equals("")) remove(0);
65+
}
66+
67+
// Normally read by lines:
68+
public TextFile(String fileName) {
69+
this(fileName, "\n");
70+
}
71+
72+
public void write(String fileName) {
73+
try {
74+
PrintWriter out = new PrintWriter(
75+
new File(fileName).getAbsoluteFile());
76+
try {
77+
for (String item : this)
78+
out.println(item);
79+
} finally {
80+
out.close();
81+
}
82+
} catch (IOException e) {
83+
throw new RuntimeException(e);
84+
}
85+
}
86+
87+
// Simple test:
88+
public static void main(String[] args) {
89+
String parent = BufferedInputFile.class.getResource("/").getPath();
90+
String inFileName = parent + "/infile.txt";
91+
92+
String file = read(inFileName);
93+
write(parent + "/text.txt", file);
94+
TextFile text = new TextFile(parent + "/text.txt");
95+
text.write(parent + "test2.txt");
96+
// Break into unique sorted list of words:
97+
TreeSet<String> words = new TreeSet<String>(
98+
new TextFile(inFileName, "\\W+"));
99+
// Display the capitalized words:
100+
System.out.println(words.headSet("a"));
101+
102+
}
103+
}
104+

java-io/src/main/resources/infile.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-----------------------------------------
2+
public static String read(String filename) throws IOException {
3+
BufferedReader in = new BufferedReader(
4+
new FileReader(filename));
5+
String s;
6+
StringBuilder sb = new StringBuilder();
7+
while ((s = in.readLine()) != null) {
8+
sb.append(s).append("\n");
9+
}
10+
in.close();
11+
return sb.toString();
12+
}

0 commit comments

Comments
 (0)