teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードの追加

2017/01/31 15:11

投稿

kohekoh
kohekoh

スコア140

title CHANGED
File without changes
body CHANGED
@@ -13,4 +13,120 @@
13
13
  すみません
14
14
  ちょっと焦っていて
15
15
  雑な説明しかできないのですが
16
- よろしくお願いします
16
+ よろしくお願いします
17
+
18
+ ```Java
19
+ import java.io.BufferedReader;
20
+ import java.io.File;
21
+ import java.io.FileReader;
22
+ import java.io.FileWriter;
23
+ import java.io.IOException;
24
+ import java.io.PrintWriter;
25
+
26
+ import javax.swing.JFileChooser;
27
+ import javax.swing.filechooser.FileFilter;
28
+
29
+ public class TextDevider {
30
+
31
+ /**
32
+ * 1ファイルあたりの行数
33
+ */
34
+ private final static int MAX_LINE = 500000;
35
+
36
+ /**
37
+ * 分割するファイル
38
+ */
39
+ private File target = null;
40
+
41
+ public static void main(String[] args) {
42
+ JFileChooser chooser = new JFileChooser();
43
+ chooser.setFileFilter(new FileFilter() {
44
+ public boolean accept(File file) {
45
+ return file.getName().endsWith(".txt");
46
+ }
47
+
48
+ public String getDescription() {
49
+ return "テキストファイル";
50
+ }
51
+ });
52
+
53
+ if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
54
+ TextDevider devider = new TextDevider(chooser.getSelectedFile());
55
+
56
+ try {
57
+ int num = devider.execDevide();
58
+ System.out.println(num + "ファイルに分割");
59
+ } catch (IOException e) {
60
+ e.printStackTrace();
61
+ }
62
+ } else {
63
+ System.out.println("キャンセル");
64
+ }
65
+ }
66
+
67
+ /**
68
+ * TextDeviderクラスのコンストラクタです。分割するテキストファイルを引数として渡します。
69
+ *
70
+ * @param file 分割するテキストファイル
71
+ */
72
+ public TextDevider(final File file) {
73
+ this.target = file;
74
+ }
75
+
76
+ /**
77
+ * テキストファイルの分割を実行します。分割したファイルは分割するファイルと同じディレクトリに保存されます。
78
+ *
79
+ * @throws IOException
80
+ * @return 分割したファイル数
81
+ */
82
+ public int execDevide() throws IOException {
83
+ BufferedReader reader = null;
84
+ PrintWriter writer = null;
85
+ boolean isFinished = false;
86
+ int fileCount = 0;
87
+
88
+ try {
89
+ reader = new BufferedReader(new FileReader(this.target));
90
+ fileCount = 0;
91
+
92
+ while (!isFinished) {
93
+ File saveFile = new File(getSaveFilePath(fileCount));
94
+ writer = new PrintWriter(new FileWriter(saveFile));
95
+ String line = null;
96
+ fileCount++;
97
+
98
+ for (int lineCount = 0; lineCount < MAX_LINE
99
+ && (line = reader.readLine()) != null; lineCount++) {
100
+ writer.println(line);
101
+ }
102
+
103
+ isFinished = (line == null);
104
+ writer.close();
105
+ writer = null;
106
+ }
107
+ } finally {
108
+ if (writer != null) {
109
+ writer.close();
110
+ }
111
+ // writer.closeは例外を投げないので、reader.closeは確実に実行される
112
+ if (reader != null) {
113
+ reader.close();
114
+ }
115
+ }
116
+ return fileCount;
117
+ }
118
+
119
+ /**
120
+ * 分割したファイルのパスを決定します。
121
+ *
122
+ * @param count
123
+ * 分割したファイルの順番を表す整数値
124
+ * @return 分割したファイルのパス
125
+ */
126
+ private String getSaveFilePath(final int count) {
127
+ return this.target.getPath() + "_" + count;
128
+ }
129
+ }
130
+
131
+
132
+ ```