回答編集履歴

2

追記

2018/04/30 09:29

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -69,3 +69,189 @@
69
69
  ◇参考情報
70
70
 
71
71
  [try-with-resources 文](https://docs.oracle.com/javase/jp/7/technotes/guides/language/try-with-resources.html)
72
+
73
+
74
+
75
+ ---
76
+
77
+
78
+
79
+ 多分 eclipseの実行メニューの実行構成が以下のような設定になってるのではないでしょうか?
80
+
81
+ ![イメージ説明](8b9e0ea5e9ca6eee71a249a497be38fc.png)
82
+
83
+ この場合sample4/sample.txtは以下のような形の設置になります。
84
+
85
+ ```txt
86
+
87
+ …List4_13
88
+
89
+ \src\sample4\List4_13.java
90
+
91
+ \bin
92
+
93
+ \sample4\sample.txt
94
+
95
+ ```
96
+
97
+
98
+
99
+ 以下は今風のソースコードです。ご参考まで
100
+
101
+ ```java
102
+
103
+ package sample4;
104
+
105
+
106
+
107
+ import java.awt.BorderLayout;
108
+
109
+ import java.awt.event.ActionEvent;
110
+
111
+ import java.awt.event.ActionListener;
112
+
113
+ import java.io.BufferedReader;
114
+
115
+ import java.io.BufferedWriter;
116
+
117
+ import java.io.FileReader;
118
+
119
+ import java.io.FileWriter;
120
+
121
+ import java.util.stream.Collectors;
122
+
123
+
124
+
125
+ import javax.swing.JButton;
126
+
127
+ import javax.swing.JFrame;
128
+
129
+ import javax.swing.JPanel;
130
+
131
+ import javax.swing.JScrollPane;
132
+
133
+ import javax.swing.JTextArea;
134
+
135
+ import javax.swing.SwingUtilities;
136
+
137
+
138
+
139
+ @SuppressWarnings("serial")
140
+
141
+ public class List4_13 extends JFrame implements ActionListener {
142
+
143
+
144
+
145
+ private final JButton btnLoad = new JButton("Load");
146
+
147
+ private final JButton btnSave = new JButton("Save");
148
+
149
+ private final JTextArea textarea = new JTextArea();
150
+
151
+ private final JScrollPane scroll = new JScrollPane(textarea);
152
+
153
+
154
+
155
+ public List4_13() {
156
+
157
+ this.setDefaultCloseOperation(EXIT_ON_CLOSE);
158
+
159
+ this.setSize(400, 300);
160
+
161
+
162
+
163
+ this.add(scroll, BorderLayout.CENTER);
164
+
165
+ btnLoad.addActionListener(this);
166
+
167
+ btnSave.addActionListener(this);
168
+
169
+
170
+
171
+ JPanel p = new JPanel();
172
+
173
+ p.add(btnLoad);
174
+
175
+ p.add(btnSave);
176
+
177
+ this.add(p, BorderLayout.SOUTH);
178
+
179
+ }
180
+
181
+
182
+
183
+ public static void main(String[] args) {
184
+
185
+ SwingUtilities.invokeLater(() -> {
186
+
187
+ List4_13 frame = new List4_13();
188
+
189
+ frame.setVisible(true);
190
+
191
+ });
192
+
193
+ }
194
+
195
+
196
+
197
+ public void actionPerformed(ActionEvent ev) {
198
+
199
+ final String filename = "sample4/sample.txt";
200
+
201
+ if (ev.getSource() == btnLoad) {
202
+
203
+ this.load(filename);
204
+
205
+ return;
206
+
207
+ }
208
+
209
+ if (ev.getSource() == btnSave) {
210
+
211
+ this.save(filename);
212
+
213
+ return;
214
+
215
+ }
216
+
217
+ }
218
+
219
+
220
+
221
+ public void load(String filename) {
222
+
223
+ try (FileReader fr = new FileReader(filename); BufferedReader br = new BufferedReader(fr)) {
224
+
225
+ String str = br.lines().collect(Collectors.joining("\r\n"));
226
+
227
+ textarea.setText(str);
228
+
229
+ } catch (Exception ex) {
230
+
231
+ ex.printStackTrace();
232
+
233
+ }
234
+
235
+ }
236
+
237
+
238
+
239
+ public void save(String filename) {
240
+
241
+ try (FileWriter fw = new FileWriter(filename); BufferedWriter bw = new BufferedWriter(fw)) {
242
+
243
+ bw.write(textarea.getText());
244
+
245
+ bw.flush();
246
+
247
+ } catch (Exception ex) {
248
+
249
+ ex.printStackTrace();
250
+
251
+ }
252
+
253
+ }
254
+
255
+ }
256
+
257
+ ```

1

追記

2018/04/30 09:29

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -1,4 +1,8 @@
1
1
  loadとsaveで読み込んでいるファイルパスが違います。
2
+
3
+ 手順としてはsaveボタンをまずクリックしないと行けないのではないでしょうか?
4
+
5
+ 参考した物にその旨記載がありませんか?
2
6
 
3
7
 
4
8
 
@@ -59,3 +63,9 @@
59
63
  }
60
64
 
61
65
  ```
66
+
67
+ あとコードの記述方法がtry-with-resources 文を使っていない(Java 1.7以前)書き方なので、もし参考にしている文献とうあるなら、あまり参考にされないほうが良いかと。
68
+
69
+ ◇参考情報
70
+
71
+ [try-with-resources 文](https://docs.oracle.com/javase/jp/7/technotes/guides/language/try-with-resources.html)