-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReader4Java.java
More file actions
50 lines (41 loc) · 1.55 KB
/
Reader4Java.java
File metadata and controls
50 lines (41 loc) · 1.55 KB
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
package reader4java;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
public class Reader4Java {
public static void main(String[] args) throws FileNotFoundException, IOException {
String sa = new File("").getAbsolutePath();
BufferedReader reader = new BufferedReader(new FileReader(sa + "\\src\\onegin.txt"));
/* import java.io.File;
import java.net.URL;
URL resource = new MyTest().getClass().getResource("/");
System.out.println(resource.getFile()); */
StringBuilder sb = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
String[] words = sb.toString().split("\\s+");
System.out.println("Total words:" + words.length);
waitEnter();
long ts = System.nanoTime();
String buff = "";
//2 StringBuffer buff = new StringBuffer();
//3 StringBuilder buff = new StringBuilder();
for (String word : words) {
buff += word + " ";
//2&3 buff.append(word).append(" ");
}
long te =System.nanoTime();
System.out.println("Complete, lenght:" + buff.length() + " elapsed time:" + (te - ts)/1e6 + "ms");
}
private static void waitEnter() {
Scanner scan = new Scanner(System.in);
System.out.print("Press Enter key.");
scan.nextLine();
}
}