-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
83 lines (65 loc) · 1.86 KB
/
Main.java
File metadata and controls
83 lines (65 loc) · 1.86 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("ファイルのパスを入力してください");
String path = br.readLine();
br.close();
if(path.length() == 0) {
System.out.println("ファイルのパスが入力されていません");
System.exit(-1);
}
File file = new File(path);
if(!file.exists()) {
System.out.println("ファイルが存在しません");
System.exit(-1);
}
List<String> lines = new ArrayList<>();
List<String> result = new ArrayList<>();
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
String str;
while((str = br.readLine()) != null) {
lines.add(str);
}
br.close();
for(int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
if(line.matches("[0-9]{1,}")){
result.add(line);
} else {
result.add(line.replaceAll("[A-Za-z!\":#\\$%&'\\[\\]\\(\\)=~\\|\\-\\^`\\{\\}\\*\\+<>\\?_@;,\\.\\/'\\\\ ,]",""));
}
}
for(String data : lines){
System.out.println(data);
}
for(String data : result){
if(data.length() != 0) {
System.out.println(data);
}
}
file = new File(System.getProperty("user.home") + "/Desktop/replaced.txt");
if(file.exists()) {
file.delete();
}
file.createNewFile();
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
for(String data : result){
if(data.length() != 0) {
pw.println(data);
}
}
pw.flush();
pw.close();
}
}