質問編集履歴

2

変更

2017/11/26 10:43

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -223,3 +223,239 @@
223
223
  read_fileのボタンを押すとread file error!!が帰ってくるのですが
224
224
 
225
225
  file.txtは入力した時に作成されるのではなく、あらかじめ用意するものなのでしょうか?そしてこのファイルがどこに保存されるのかを教えていただきたいです。
226
+
227
+
228
+
229
+
230
+
231
+
232
+
233
+ #これでできました
234
+
235
+ ```java
236
+
237
+
238
+
239
+ package com.example.ueday.write;
240
+
241
+
242
+
243
+ import android.app.Activity;
244
+
245
+ import android.os.Bundle;
246
+
247
+ import android.view.View;
248
+
249
+ import android.widget.Button;
250
+
251
+ import android.widget.EditText;
252
+
253
+ import android.widget.TextView;
254
+
255
+ import java.io.BufferedReader;
256
+
257
+ import java.io.BufferedWriter;
258
+
259
+ import java.io.File;
260
+
261
+ import java.io.FileInputStream;
262
+
263
+ import java.io.FileNotFoundException;
264
+
265
+ import java.io.FileOutputStream;
266
+
267
+ import java.io.IOException;
268
+
269
+ import java.io.InputStreamReader;
270
+
271
+ import java.io.OutputStreamWriter;
272
+
273
+
274
+
275
+
276
+
277
+ public class MainActivity extends Activity {
278
+
279
+ private TextView textView;
280
+
281
+ private EditText editText;
282
+
283
+ private String fileName = "file.txt";
284
+
285
+
286
+
287
+
288
+
289
+ @Override
290
+
291
+ protected void onCreate(Bundle savedInstanceState) {
292
+
293
+ super.onCreate(savedInstanceState);
294
+
295
+ setContentView(R.layout.activity_main);
296
+
297
+
298
+
299
+ textView = findViewById(R.id.text_view);
300
+
301
+
302
+
303
+ editText = findViewById(R.id.edit_text);
304
+
305
+
306
+
307
+
308
+
309
+ createFile();
310
+
311
+
312
+
313
+
314
+
315
+ Button buttonSave = findViewById(R.id.button_save);
316
+
317
+ buttonSave.setOnClickListener(new View.OnClickListener() {
318
+
319
+ @Override
320
+
321
+ public void onClick(View v) {
322
+
323
+ String text = editText.getText().toString();
324
+
325
+ saveFile(fileName, text);
326
+
327
+ if (text.length() == 0) {
328
+
329
+ textView.setText(R.string.no_text);
330
+
331
+ } else {
332
+
333
+ textView.setText(R.string.saved);
334
+
335
+ }
336
+
337
+ }
338
+
339
+ });
340
+
341
+
342
+
343
+ Button buttonRead = findViewById(R.id.button_read);
344
+
345
+ buttonRead.setOnClickListener(new View.OnClickListener() {
346
+
347
+ @Override
348
+
349
+ public void onClick(View v) {
350
+
351
+ String str = readFile(fileName);
352
+
353
+ if (str != null) {
354
+
355
+ textView.setText(str);
356
+
357
+ } else {
358
+
359
+ textView.setText(R.string.read_error);
360
+
361
+ }
362
+
363
+ }
364
+
365
+ });
366
+
367
+ }
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+ public void createFile() {
376
+
377
+ File file = new File(getFilesDir() + "/" + fileName);
378
+
379
+ if(!file.exists()){
380
+
381
+ try {
382
+
383
+ file.createNewFile();
384
+
385
+ } catch (IOException e) {
386
+
387
+ }
388
+
389
+ }
390
+
391
+ }
392
+
393
+
394
+
395
+
396
+
397
+ public void saveFile(String file, String str) {
398
+
399
+ try {
400
+
401
+ FileOutputStream fos = openFileOutput(file, MODE_PRIVATE);
402
+
403
+ OutputStreamWriter osw = new OutputStreamWriter(fos);
404
+
405
+ BufferedWriter writer = new BufferedWriter(osw);
406
+
407
+ writer.write(str);
408
+
409
+ writer.close();
410
+
411
+ } catch (IOException e) {
412
+
413
+ e.printStackTrace();
414
+
415
+ }
416
+
417
+ }
418
+
419
+
420
+
421
+ public String readFile(String file) {
422
+
423
+ String text = null;
424
+
425
+ try {
426
+
427
+ FileInputStream fileInputStream = openFileInput(file);
428
+
429
+ BufferedReader reader = new BufferedReader(
430
+
431
+ new InputStreamReader(fileInputStream, "UTF-8"));
432
+
433
+ String lineBuffer;
434
+
435
+ while( (lineBuffer = reader.readLine()) != null ) {
436
+
437
+ text = lineBuffer ;
438
+
439
+ }
440
+
441
+
442
+
443
+ } catch (IOException e) {
444
+
445
+ e.printStackTrace();
446
+
447
+ }
448
+
449
+
450
+
451
+ return text;
452
+
453
+ }
454
+
455
+
456
+
457
+
458
+
459
+ }
460
+
461
+ ```

1

変更

2017/11/26 10:43

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -20,14 +20,22 @@
20
20
 
21
21
  import java.io.BufferedReader;
22
22
 
23
+ import java.io.BufferedWriter;
24
+
25
+ import java.io.File;
26
+
23
27
  import java.io.FileInputStream;
24
28
 
29
+ import java.io.FileNotFoundException;
30
+
25
31
  import java.io.FileOutputStream;
26
32
 
27
33
  import java.io.IOException;
28
34
 
29
35
  import java.io.InputStreamReader;
30
36
 
37
+ import java.io.OutputStreamWriter;
38
+
31
39
 
32
40
 
33
41
 
@@ -42,6 +50,8 @@
42
50
 
43
51
 
44
52
 
53
+
54
+
45
55
  @Override
46
56
 
47
57
  protected void onCreate(Bundle savedInstanceState) {
@@ -60,10 +70,16 @@
60
70
 
61
71
 
62
72
 
73
+
74
+
75
+ createFile();
76
+
77
+
78
+
79
+
80
+
63
81
  Button buttonSave = findViewById(R.id.button_save);
64
82
 
65
-
66
-
67
83
  buttonSave.setOnClickListener(new View.OnClickListener() {
68
84
 
69
85
  @Override
@@ -120,15 +136,43 @@
120
136
 
121
137
 
122
138
 
139
+
140
+
141
+ public void createFile() {
142
+
143
+ File file = new File(getFilesDir() + "/" + fileName);
144
+
145
+ if(!file.exists()){
146
+
147
+ try {
148
+
149
+ file.createNewFile();
150
+
151
+ } catch (IOException e) {
152
+
153
+ }
154
+
155
+ }
156
+
157
+ }
158
+
159
+
160
+
161
+
162
+
123
163
  public void saveFile(String file, String str) {
124
164
 
125
165
  try {
126
166
 
127
- FileOutputStream fileOutputstream = openFileOutput(file, MODE_PRIVATE);
167
+ FileOutputStream fos = openFileOutput(file, MODE_PRIVATE);
128
-
129
-
130
-
168
+
131
- fileOutputstream.write(str.getBytes());
169
+ OutputStreamWriter osw = new OutputStreamWriter(fos);
170
+
171
+ BufferedWriter writer = new BufferedWriter(osw);
172
+
173
+ writer.write(str);
174
+
175
+ writer.close();
132
176
 
133
177
  } catch (IOException e) {
134
178