回答編集履歴

2

見直しキャンペーン中

2023/08/13 12:50

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -1,81 +1,41 @@
1
- 「java テキストファイル 読み込み」等で検索されたのでしょうか?(提示コードからは全く読み取れません)
2
-
1
+ [java テキストファイル 読み込み](https://www.google.co.jp/search?q=java+%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB+%E8%AA%AD%E3%81%BF%E8%BE%BC%E3%81%BF)」等で検索されたのでしょうか?(提示コードからは全く読み取れません)
3
2
  歴史的経緯で方法が複数ありややこしいのですが、Java11以上なら1行で簡単に読めます。
4
3
 
5
-
6
-
7
4
  ```Java
8
-
9
5
  import java.io.File;
10
-
11
6
  import java.io.IOException;
12
-
13
7
  import java.nio.file.Files;
14
8
 
15
-
16
-
17
9
  import javafx.application.Application;
18
-
19
10
  import javafx.scene.Scene;
20
-
21
11
  import javafx.scene.control.Accordion;
22
-
23
12
  import javafx.scene.control.TextArea;
24
-
25
13
  import javafx.scene.control.TitledPane;
26
-
27
14
  import javafx.scene.layout.BorderPane;
28
-
29
15
  import javafx.stage.Stage;
30
16
 
31
-
32
-
33
17
  public class seventeen extends Application {
34
-
35
18
  public static void main(String[] args) { launch(args); }
36
19
 
37
-
38
-
39
20
  public void start(Stage stage) throws IOException {
40
-
41
21
  // ↓ラムダ式 sampleFileFilterでやっているのと同じ
42
-
43
22
  File[] textFiles = new File(".").listFiles((f, n) -> n.toLowerCase().endsWith(".txt"));
44
-
45
-
46
23
 
47
24
  Accordion accordion = new Accordion();
48
25
 
49
-
50
-
51
26
  for (File file : textFiles) {
52
-
53
27
  String text = Files.readString(file.toPath()); // Java11以上ならこれでファイルからStringに一気に読める
54
-
55
28
  TextArea textArea = new TextArea(text); // 読んだ文字列をTextAreaに入れる
56
-
57
29
  TitledPane titledPane = new TitledPane(file.getName(), textArea); // タイトル部分はファイル名 中身はTextArea
58
-
59
30
  accordion.getPanes().add(titledPane);
60
-
61
31
  }
62
32
 
63
-
64
-
65
33
  BorderPane borderPane = new BorderPane();
66
-
67
34
  borderPane.setCenter(accordion);
68
35
 
69
-
70
-
71
36
  stage.setScene(new Scene(borderPane, 300, 300));
72
-
73
37
  stage.setTitle("サンプル");
74
-
75
38
  stage.show();
76
-
77
39
  }
78
-
79
40
  }
80
-
81
41
  ```

1

ずれてた

2021/02/04 16:59

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
  public void start(Stage stage) throws IOException {
40
40
 
41
- // ↓ラムダ式 sampleFileFilterでやっているのと同じ
41
+ // ↓ラムダ式 sampleFileFilterでやっているのと同じ
42
42
 
43
43
  File[] textFiles = new File(".").listFiles((f, n) -> n.toLowerCase().endsWith(".txt"));
44
44