質問編集履歴
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -29,3 +29,235 @@
|
|
29
29
|
雑な説明しかできないのですが
|
30
30
|
|
31
31
|
よろしくお願いします
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
```Java
|
36
|
+
|
37
|
+
import java.io.BufferedReader;
|
38
|
+
|
39
|
+
import java.io.File;
|
40
|
+
|
41
|
+
import java.io.FileReader;
|
42
|
+
|
43
|
+
import java.io.FileWriter;
|
44
|
+
|
45
|
+
import java.io.IOException;
|
46
|
+
|
47
|
+
import java.io.PrintWriter;
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
import javax.swing.JFileChooser;
|
52
|
+
|
53
|
+
import javax.swing.filechooser.FileFilter;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
public class TextDevider {
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
/**
|
62
|
+
|
63
|
+
* 1ファイルあたりの行数
|
64
|
+
|
65
|
+
*/
|
66
|
+
|
67
|
+
private final static int MAX_LINE = 500000;
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
/**
|
72
|
+
|
73
|
+
* 分割するファイル
|
74
|
+
|
75
|
+
*/
|
76
|
+
|
77
|
+
private File target = null;
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
public static void main(String[] args) {
|
82
|
+
|
83
|
+
JFileChooser chooser = new JFileChooser();
|
84
|
+
|
85
|
+
chooser.setFileFilter(new FileFilter() {
|
86
|
+
|
87
|
+
public boolean accept(File file) {
|
88
|
+
|
89
|
+
return file.getName().endsWith(".txt");
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
public String getDescription() {
|
96
|
+
|
97
|
+
return "テキストファイル";
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
});
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
106
|
+
|
107
|
+
TextDevider devider = new TextDevider(chooser.getSelectedFile());
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
try {
|
112
|
+
|
113
|
+
int num = devider.execDevide();
|
114
|
+
|
115
|
+
System.out.println(num + "ファイルに分割");
|
116
|
+
|
117
|
+
} catch (IOException e) {
|
118
|
+
|
119
|
+
e.printStackTrace();
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
} else {
|
124
|
+
|
125
|
+
System.out.println("キャンセル");
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
/**
|
134
|
+
|
135
|
+
* TextDeviderクラスのコンストラクタです。分割するテキストファイルを引数として渡します。
|
136
|
+
|
137
|
+
*
|
138
|
+
|
139
|
+
* @param file 分割するテキストファイル
|
140
|
+
|
141
|
+
*/
|
142
|
+
|
143
|
+
public TextDevider(final File file) {
|
144
|
+
|
145
|
+
this.target = file;
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
/**
|
152
|
+
|
153
|
+
* テキストファイルの分割を実行します。分割したファイルは分割するファイルと同じディレクトリに保存されます。
|
154
|
+
|
155
|
+
*
|
156
|
+
|
157
|
+
* @throws IOException
|
158
|
+
|
159
|
+
* @return 分割したファイル数
|
160
|
+
|
161
|
+
*/
|
162
|
+
|
163
|
+
public int execDevide() throws IOException {
|
164
|
+
|
165
|
+
BufferedReader reader = null;
|
166
|
+
|
167
|
+
PrintWriter writer = null;
|
168
|
+
|
169
|
+
boolean isFinished = false;
|
170
|
+
|
171
|
+
int fileCount = 0;
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
try {
|
176
|
+
|
177
|
+
reader = new BufferedReader(new FileReader(this.target));
|
178
|
+
|
179
|
+
fileCount = 0;
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
while (!isFinished) {
|
184
|
+
|
185
|
+
File saveFile = new File(getSaveFilePath(fileCount));
|
186
|
+
|
187
|
+
writer = new PrintWriter(new FileWriter(saveFile));
|
188
|
+
|
189
|
+
String line = null;
|
190
|
+
|
191
|
+
fileCount++;
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
for (int lineCount = 0; lineCount < MAX_LINE
|
196
|
+
|
197
|
+
&& (line = reader.readLine()) != null; lineCount++) {
|
198
|
+
|
199
|
+
writer.println(line);
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
isFinished = (line == null);
|
206
|
+
|
207
|
+
writer.close();
|
208
|
+
|
209
|
+
writer = null;
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
} finally {
|
214
|
+
|
215
|
+
if (writer != null) {
|
216
|
+
|
217
|
+
writer.close();
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
// writer.closeは例外を投げないので、reader.closeは確実に実行される
|
222
|
+
|
223
|
+
if (reader != null) {
|
224
|
+
|
225
|
+
reader.close();
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
return fileCount;
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
/**
|
238
|
+
|
239
|
+
* 分割したファイルのパスを決定します。
|
240
|
+
|
241
|
+
*
|
242
|
+
|
243
|
+
* @param count
|
244
|
+
|
245
|
+
* 分割したファイルの順番を表す整数値
|
246
|
+
|
247
|
+
* @return 分割したファイルのパス
|
248
|
+
|
249
|
+
*/
|
250
|
+
|
251
|
+
private String getSaveFilePath(final int count) {
|
252
|
+
|
253
|
+
return this.target.getPath() + "_" + count;
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
```
|