-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
118 lines (93 loc) · 4.17 KB
/
Main.java
File metadata and controls
118 lines (93 loc) · 4.17 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import jdk.internal.util.xml.impl.Input;
import java.io.*;
import java.lang.reflect.Array;
import java.nio.charset.Charset;
import java.sql.Time;
import java.util.*;
public class Main {
private static BufferedReader reader;
public static void main(String[] args) {
String path = "src/file/firstFile.txt";
solutionOne(path);
solutionTwo();
solutionThree();
}
private static void solutionOne(String path) {
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void solutionTwo() {
ArrayList<FileInputStream> list = new ArrayList<>();
try {
list.add(new FileInputStream("src/file/one.txt"));
list.add(new FileInputStream("src/file/two.txt"));
list.add(new FileInputStream("src/file/three.txt"));
list.add(new FileInputStream("src/file/four.txt"));
list.add(new FileInputStream("src/file/five.txt"));
Enumeration<FileInputStream> enumeration = Collections.enumeration(list);
SequenceInputStream in = new SequenceInputStream(Collections.enumeration(list));
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream("src/file/six.txt", true));
long time = System.currentTimeMillis();
int tmp = -1;
// Способ первый ()
while (enumeration.hasMoreElements()) {
BufferedReader reader = new BufferedReader(new InputStreamReader(enumeration.nextElement()));
while ((tmp = reader.read()) != -1) {
writer.write(tmp);
}
}
//Второй способ
while ( (tmp = in.read()) != -1){
writer.write(tmp);
}
//первый способ быстрей
System.out.println(System.currentTimeMillis() - time);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void solutionThree() {
try {
BufferedReader reader = new BufferedReader(new FileReader("src/file/firstFile.txt"));
Scanner scanner = new Scanner(System.in);
int count = 0;
int tmp = -1;
StringBuilder stringBuilder = new StringBuilder();
long time = System.currentTimeMillis();
endProgram:
while ((tmp = reader.read()) != -1) {
count++;
stringBuilder.append((char)tmp);
if (count == 1799) {
count = 0;
String str = "";
System.out.println(stringBuilder);
stringBuilder.setLength(0);
do {
long timeEnd = System.currentTimeMillis()-time;
System.out.println(""); // Для читабельности аналог <br>
System.out.println(""); // Для читабельности аналог <br>
System.out.println("Обработка первой страницы заняла " + timeEnd);
// Выполняется за 4 милисекунды (book.txt - 23.3 mb)
// Надеюсь я правильно понял задание
System.out.println("------------------------- Страница закончилась -------------------------");
System.out.println("-------------- Чтобы продолжить нажмите Y, END для выхода --------------");
str = scanner.next();
if (str.equals("end")) {
break endProgram;
}
} while (!str.equalsIgnoreCase("y"));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}