プロパティ
ストリームから読み取るか、ストリームにロードできることを示します。
継承されたハッシュテーブル
1.トラバース
package com.itheima.demo07.Prop;
import java.util.Properties;
import java.util.Set;
public class Demo01Properties {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("filename", "a.txt");
properties.setProperty("length", "209385038");
properties.setProperty("location", "D:\\a.txt");
System.out.println(properties);
/*遍历打印属性集*/
Set<String> strings = properties.stringPropertyNames();
for (String s : strings) {
System.out.println(properties.getProperty(s));
}
}
}
運転結果
{location = D:\ a.txt、filename = a.txt、length = 209385038}
D:\ a.txt
a.txt
209385038
2.コレクション->ハードディスクストア
文字ストリームFileWriter
に
対応して、メモリからデータを書き込みます。バイトストリームに対応してFileOutputStream
、メモリからデータを出力します。
public void store(Writer writer, String comments)
throws IOException
{
store0((writer instanceof BufferedWriter)?(BufferedWriter)writer
: new BufferedWriter(writer),
comments,
false);
}
public void store(OutputStream out, String comments)
throws IOException
{
store0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")),
comments,
true);
}
最初のタイプ:中国語で記述できる文字出力ストリーム
。2番目のタイプ:中国語で記述できないバイト出力ストリーム。
2.1ファイルを書き込むための文字ストリームFileWriter
//使用字符流
package com.itheima.demo07.Prop;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Properties;
public class Demo02Properties {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("filename", "a.txt");
properties.setProperty("length", "209385038");
properties.setProperty("location", "D:\\a.txt");
//创建一个字符流
FileWriter fw = new FileWriter("h.txt");
properties.store(fw, "sava date");
fw.close();
}
}
運転結果
2.2メモリからのバイトストリームFileOutputStream出力
//使用字节流
package com.itheima.demo07.Prop;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Demo03Properties {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("filename", "a.txt");
properties.setProperty("length", "209385038");
properties.setProperty("location", "D:\\a.txt");
//创建一个字节流
FileOutputStream fos = new FileOutputStream("h.txt");
properties.store(fos, "sava date");
fos.close();
}
}
運転結果
3.ハードディスク->一括ロード
文字ストリームに対応してFileRead
、メモリからデータを読み取り
ます。バイトストリームに対応してFileInputStream
、メモリからデータを入力します。
public synchronized void load(Reader reader) throws IOException {
load0(new LineReader(reader));
}
public synchronized void load(InputStream inStream) throws IOException {
load0(new LineReader(inStream));
}
最初のタイプ:文字入力ストリーム、中国語を書くことができます
2番目のタイプ:バイト入力ストリーム、中国語を書くことができません
3.1ファイルを読み取るための文字ストリームFileRead
package com.itheima.demo07.Prop;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class Demo04Properties {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
FileReader fr = new FileReader("h.txt");
System.out.println(properties);
properties.load(fr);
System.out.println(properties);
Set<String> strings = properties.stringPropertyNames();
for (String s : strings) {
System.out.println(properties.getProperty(s));
}
}
}
運転結果
{}
{location = D:\ a.txt、filename = a.txt、length = 209385038}
D:\ a.txt
a.txt
209385038
3.2バイトストリームFileInputStreamメモリへの入力
package com.itheima.demo07.Prop;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class Demo05Properties {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
FileInputStream fis = new FileInputStream("h.txt");
System.out.println(properties);
properties.load(fis);
System.out.println(properties);
Set<String> strings = properties.stringPropertyNames();
for (String s : strings) {
System.out.println(properties.getProperty(s));
}
}
}
運転結果
{}
{location = D:\ a.txt、filename = a.txt、length = 209385038}
D:\ a.txt
a.txt
209385038
ヒント:ファイルで接続されているキーと値のペアは、等号とスペースで接続できます。
ファイルでは、#でコメントできます。
デフォルトのキーと値のペアは文字列として読み取られます。追加 ""