回答編集履歴
2
プラットフォームごとに改行文字列が異なる
answer
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
|
25
25
|
**改行文字 - System.getProperty("line.separator");**
|
26
26
|
|
27
|
-
|
27
|
+
改行文字列の別の取得方法を追記します。
|
28
28
|
|
29
29
|
```Java
|
30
30
|
static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
@@ -36,4 +36,11 @@
|
|
36
36
|
.map(e -> Arrays.stream(e).collect(Collectors.joining("/")))
|
37
37
|
.collect(Collectors.joining(LINE_SEPARATOR));
|
38
38
|
System.out.println(line2);
|
39
|
-
```
|
39
|
+
```
|
40
|
+
|
41
|
+
|
42
|
+
**プラットフォームごとに改行文字列が異なる**
|
43
|
+
|
44
|
+
念のため、プラットフォームごとに改行文字列が異なるのはご存じですね。[System#lineSeparator](https://docs.oracle.com/javase/jp/8/docs/api/java/lang/System.html#lineSeparator--)
|
45
|
+
|
46
|
+
`System.lineSeparator()`はJava 7以降。その前は直接プロパティ`"line.separator"`の値を取得していました。Java 7以降のSystemクラスのソースコードを読めば内部に`"line.separator"`の値を保持しているのがわかります。
|
1
System.getProperty("line.separator");を追加
answer
CHANGED
@@ -19,4 +19,21 @@
|
|
19
19
|
.map(e -> Arrays.stream(e).collect(Collectors.joining("/")))
|
20
20
|
.collect(Collectors.joining(System.lineSeparator()));
|
21
21
|
System.out.println(line);
|
22
|
+
```
|
23
|
+
ここまではotnさんと同じ方法です。
|
24
|
+
|
25
|
+
**改行文字 - System.getProperty("line.separator");**
|
26
|
+
|
27
|
+
申し訳ないので、改行文字列の別の取得方法を追記します。
|
28
|
+
|
29
|
+
```Java
|
30
|
+
static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
31
|
+
```
|
32
|
+
この`LINE_SEPARATOR`を使うと次のようになります。
|
33
|
+
|
34
|
+
```Java
|
35
|
+
String line2 = Arrays.stream(animals)
|
36
|
+
.map(e -> Arrays.stream(e).collect(Collectors.joining("/")))
|
37
|
+
.collect(Collectors.joining(LINE_SEPARATOR));
|
38
|
+
System.out.println(line2);
|
22
39
|
```
|