回答編集履歴

2

追記

2020/08/11 07:58

投稿

shiketa
shiketa

スコア3971

test CHANGED
@@ -49,3 +49,97 @@
49
49
  //name=\u3042\u3044\u3046\u3048\u304A
50
50
 
51
51
  ```
52
+
53
+
54
+
55
+
56
+
57
+ ----
58
+
59
+ ----
60
+
61
+ ----
62
+
63
+
64
+
65
+ A-pZさんの回答にある、FileWriterはJava11から使えますね。
66
+
67
+
68
+
69
+ A-pZさんのを参考に、Java11以前向けに書き直してみました。store/loadでいけますね。失礼。
70
+
71
+
72
+
73
+
74
+
75
+ ```java
76
+
77
+ import java.io.*;
78
+
79
+ import java.nio.charset.Charset;
80
+
81
+ import java.util.Properties;
82
+
83
+
84
+
85
+ public class xxHoge {
86
+
87
+ public static void main(String[] args) throws Exception {
88
+
89
+ Properties prop = new Properties();
90
+
91
+ prop.setProperty("name", "あいうえお");
92
+
93
+ prop.put("sampleValue", "日本語どうでしょう");
94
+
95
+ System.out.println(prop);
96
+
97
+
98
+
99
+ final File file = new File("hoge.properties");
100
+
101
+ final Charset charset = Charset.forName("utf-8");
102
+
103
+ System.out.println(file);
104
+
105
+ prop.store(new OutputStreamWriter(new FileOutputStream(file), charset), "");
106
+
107
+
108
+
109
+ final Properties hoge = new Properties();
110
+
111
+ hoge.load(new InputStreamReader(new FileInputStream(file), charset));
112
+
113
+ System.out.println(hoge);
114
+
115
+ System.out.println(prop.equals(hoge));
116
+
117
+ }
118
+
119
+ }
120
+
121
+ //{name=あいうえお, sampleValue=日本語どうでしょう}
122
+
123
+ //hoge.properties
124
+
125
+ //{name=あいうえお, sampleValue=日本語どうでしょう}
126
+
127
+ //true
128
+
129
+ ```
130
+
131
+
132
+
133
+ ```
134
+
135
+ $ cat hoge.properties
136
+
137
+ #
138
+
139
+ #Tue Aug 11 16:48:24 JST 2020
140
+
141
+ name=あいうえお
142
+
143
+ sampleValue=日本語どうでしょう
144
+
145
+ ```

1

typo

2020/08/11 07:58

投稿

shiketa
shiketa

スコア3971

test CHANGED
@@ -7,3 +7,45 @@
7
7
 
8
8
 
9
9
  てことなので、game.cfgファイルは`ISO 8859-1`でエンコーディングされています。
10
+
11
+
12
+
13
+ ----
14
+
15
+
16
+
17
+ どうせソースを添付するなら、せめて簡単に実行できるようなものにしてほしいものです。
18
+
19
+ ```java
20
+
21
+ import java.io.*;
22
+
23
+ import java.util.*;
24
+
25
+ public class xxHoge {
26
+
27
+ public static void main(String[] args) throws Exception {
28
+
29
+ try (FileOutputStream f = new FileOutputStream( "game.cfg");
30
+
31
+ BufferedOutputStream b = new BufferedOutputStream(f)) {
32
+
33
+ Properties prop = new Properties();
34
+
35
+ prop.setProperty("name", "あいうえお");
36
+
37
+ prop.store(b, "");
38
+
39
+ }
40
+
41
+ }
42
+
43
+ }
44
+
45
+ //#
46
+
47
+ //#Sat Aug 08 21:19:09 JST 2020
48
+
49
+ //name=\u3042\u3044\u3046\u3048\u304A
50
+
51
+ ```