イメージ
2 3 45 5 3 44 5 55
実現したいことが何なのか良く分からないですが、こんな感じでしょうか?
java
1import java.util.Scanner;
2import java.io.PrintWriter;
3
4public class Hello {
5 public static void main(String[] args) {
6 String data;
7 String line = "";
8 java.util.Scanner sc = new Scanner(System.in);
9 System.out.print("filename>> ");
10 String filename = sc.nextLine();
11 try(PrintWriter pw = new PrintWriter(filename)){
12 for(int i=0;i<10;i++) {
13 System.out.print("contents>> ");
14 data = sc.nextLine();
15 if (data.isEmpty()) {
16 continue;
17 }
18 if (line.isEmpty()) {
19 line = data;
20 } else {
21 line += " " + data;
22 }
23 }
24 pw.println(line);
25 }catch(Exception e) {
26 System.out.println(e);
27 }
28 }
29}
30
■ 実行結果
C:\home\edu\teratail\questions\377344>javac Hello.java
C:\home\edu\teratail\questions\377344>java Hello
filename>> test.txt
contents>> 2
contents>> 3
contents>> 45
contents>> 5
contents>> 3
contents>> 44
contents>> 5
contents>> 55
contents>>
contents>>
C:\home\edu\teratail\questions\377344>type test.txt
2 3 45 5 3 44 5 55