-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextReader.java
More file actions
52 lines (38 loc) · 1.57 KB
/
TextReader.java
File metadata and controls
52 lines (38 loc) · 1.57 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
package textreader;
import java.util.ArrayList;
import java.io.*;
public class TextReader {
public static void main(String[] args) throws FileNotFoundException, IOException {
//файл має мати назву text.txt
BufferedReader reader = new BufferedReader(new FileReader("src\\textreader\\text.txt"));
String line = "";
ArrayList<String> lines = new ArrayList<String>();
while ((line = reader.readLine()) != null) {
if (line.compareTo("\n") < 0)
continue;
lines.add(line);
}
String [] linesAsArray = lines.toArray(new String[lines.size()]);
for(int p = 0; p < lines.size(); p++) {
for (int j = 0; j < lines.size() - 1; j++) {
String temp = "";
if (linesAsArray[j].compareToIgnoreCase(linesAsArray[j + 1]) > 0) {
temp = linesAsArray[j];
linesAsArray[j] = linesAsArray[j + 1];
linesAsArray[j + 1] = temp;
}
}
}
File crtFile = new File("src\\textreader\\SortedText.txt");
String parentFile = crtFile.getPath();
try(FileWriter writer = new FileWriter(parentFile, false)){
for(int i = 0;i <lines.size(); i++){
writer.write(linesAsArray[i]+"\n");
}
System.out.println("Done!");
}
catch(IOException ex){
System.out.println(ex.getMessage());
}
}
}