質問編集履歴
2
書き込んだテキストファイルの内容
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,6 +6,8 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
+
(書き込まれたテキストファイルでカーソルを文字列に沿って動かしていくと改行部分でカーソルが見えない文字を指しているのが分かるので、改行文字は書き込まれている?)
|
10
|
+
|
9
11
|
|
10
12
|
|
11
13
|
例
|
1
誤った投稿を別の質問内容に変えました。すみませんでした。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
javaで
|
1
|
+
javaでテキストエリアから文字列を取得し、改行ありでテキストファイルに書き込む方法
|
test
CHANGED
@@ -1,5 +1,275 @@
|
|
1
|
+
テキストエリアから文字列を取得し、それをテキストファイル(メモ帳)に書き込むプログラムを作成しています。
|
2
|
+
|
3
|
+
splitを利用して改行コードを読み込むと改行されるようにしたいのですが、出力されたテキストファイルをみると文字列がすべて1行にまとまって出力されます。
|
4
|
+
|
5
|
+
どのようにしたら改行ありで書き込めるのか教えていただきたいです。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
例
|
12
|
+
|
1
|
-
ス
|
13
|
+
・読み込んだテキスト
|
14
|
+
|
2
|
-
|
15
|
+
あいうえお
|
16
|
+
|
17
|
+
かきくけこ
|
18
|
+
|
19
|
+
さしすせそ
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
・書き込まれたテキスト
|
24
|
+
|
25
|
+
あいうえおかきくけこさしすせそ
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
```lang-java
|
30
|
+
|
31
|
+
import javax.swing.*;
|
32
|
+
|
3
|
-
|
33
|
+
import java.awt.*;
|
34
|
+
|
4
|
-
|
35
|
+
import java.awt.event.*;
|
36
|
+
|
37
|
+
import java.io.*;
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
class Enshu15{
|
42
|
+
|
43
|
+
public static void main(String[] args){
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
JFrame frame = new JFrame("textediter");
|
48
|
+
|
49
|
+
MyPanel panel = new MyPanel();
|
50
|
+
|
51
|
+
Container cont = frame.getContentPane();
|
52
|
+
|
53
|
+
cont.add(panel);
|
54
|
+
|
55
|
+
frame.setSize(640,480);
|
56
|
+
|
57
|
+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
58
|
+
|
59
|
+
frame.setVisible(true);
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
class MyPanel extends JPanel{
|
66
|
+
|
67
|
+
JTextField input;
|
68
|
+
|
69
|
+
JButton open,save,create;
|
70
|
+
|
71
|
+
JLabel label = new JLabel("please input file");
|
72
|
+
|
73
|
+
JTextArea ta = new JTextArea();
|
74
|
+
|
75
|
+
JScrollPane scroll = new JScrollPane(ta);
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
MyPanel(){
|
80
|
+
|
81
|
+
EventListener el = new EventListener();
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
input = new JTextField("None",10);
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
open = new JButton("ファイルを開く");
|
90
|
+
|
91
|
+
save = new JButton("保存");
|
92
|
+
|
93
|
+
create = new JButton("新規作成");
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
scroll.setPreferredSize(new Dimension(600,350));
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
// panel[] とはしない
|
102
|
+
|
103
|
+
JPanel panelInput = new JPanel();
|
104
|
+
|
105
|
+
JPanel panelButton = new JPanel();
|
106
|
+
|
107
|
+
JPanel panelNorth = new JPanel();
|
108
|
+
|
109
|
+
JPanel panelCenter = new JPanel();
|
110
|
+
|
111
|
+
JPanel panelSouth = new JPanel();
|
112
|
+
|
113
|
+
panelInput.setLayout(new FlowLayout());
|
114
|
+
|
115
|
+
panelInput.add(input);
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
panelButton.setLayout(new FlowLayout());
|
120
|
+
|
121
|
+
panelButton.add(open);
|
122
|
+
|
123
|
+
panelButton.add(save);
|
124
|
+
|
125
|
+
panelButton.add(create);
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
panelNorth.setLayout(new FlowLayout());
|
130
|
+
|
131
|
+
panelNorth.add(panelInput);
|
132
|
+
|
133
|
+
panelNorth.add(panelButton);
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
panelCenter.add(scroll);
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
panelSouth.add(label);
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
this.setLayout(new BorderLayout());
|
148
|
+
|
149
|
+
this.add(panelNorth, BorderLayout.NORTH);
|
150
|
+
|
151
|
+
this.add(panelCenter, BorderLayout.CENTER);
|
152
|
+
|
153
|
+
this.add(panelSouth, BorderLayout.SOUTH);
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
open.addActionListener(el);
|
158
|
+
|
159
|
+
save.addActionListener(el);
|
160
|
+
|
161
|
+
create.addActionListener(el);
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
class EventListener implements ActionListener{
|
166
|
+
|
167
|
+
public void actionPerformed(ActionEvent e){
|
168
|
+
|
169
|
+
if(e.getSource()==open){
|
170
|
+
|
171
|
+
String data = "";
|
172
|
+
|
173
|
+
try{
|
174
|
+
|
175
|
+
String failName = input.getText();
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
BufferedReader br = new BufferedReader(new FileReader(failName+".txt"));
|
180
|
+
|
181
|
+
ta.read(br,null);
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
br.close();
|
186
|
+
|
187
|
+
label.setText("open succeed");
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
}catch(Exception ex){
|
192
|
+
|
193
|
+
label.setText("open error");
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
else if(e.getSource()==save){
|
200
|
+
|
201
|
+
// String data = "";
|
202
|
+
|
203
|
+
try{
|
204
|
+
|
205
|
+
String failName = input.getText();
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(failName+".txt")));
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
String str = ta.getText();
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
// 改行コードのインプットはできるが、アウトプットはOSなどによって違うため、プラットフォームに合わせて変換
|
218
|
+
|
219
|
+
// str.replaceAll("\r\n",System.getProperty("line.separator"));
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
String[] outText = str.split("\r\n");
|
224
|
+
|
225
|
+
// System.out.println(outText[2]);
|
226
|
+
|
227
|
+
for (int i = 0; i < outText.length; i++) {
|
228
|
+
|
229
|
+
pw.println(outText[i]);
|
230
|
+
|
231
|
+
System.out.println(outText[i]);
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
pw.close();
|
238
|
+
|
239
|
+
label.setText("save succeed");
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
}catch(Exception ex){
|
244
|
+
|
245
|
+
label.setText("save error");
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
}
|
250
|
+
|
5
|
-
|
251
|
+
else{
|
252
|
+
|
253
|
+
try{
|
254
|
+
|
255
|
+
ta.setText("");
|
256
|
+
|
257
|
+
label.setText("create succeed");
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
}catch(Exception ex){
|
262
|
+
|
263
|
+
label.setText("create error");
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
```
|