- 1 Properties 类的存储与读取
package exrcise;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class Demo1 {
public static void main(String[] args) throws IOException {
File file = new File("/home/alex/test/test.properties");
Properties pro = new Properties();
pro.setProperty("a", "1");
pro.setProperty("b", "2");
pro.setProperty("c", "3");
//写入文件
BufferedWriter out = new BufferedWriter(new FileWriter(file));
pro.store(out, "");
//从文件读取
BufferedReader in = new BufferedReader(new FileReader(file));
pro.load(in);
//遍历properties
Set<String> set = pro.stringPropertyNames();
for (String string : set) {
System.out.println(string + "..." + pro.getProperty(string));
}
}
}
- 2对象的序列化与反序列化
transient关键字
当一个类的对象需要被序列化时,某些属性不需要被序列化,这时不需要序列化的属性可以使用关键字transient修饰。只要被transient修饰了,序列化时这个属性就不会琲序列化了。
同时静态修饰也不会被序列化,因为序列化是把对象数据进行持久化存储,而静态的属于类加载时的数据,不会被序列化。
package exrcise;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Demo2 {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
// 写入对象
writeObj();
// 读取对象
Person person = readObj();
System.out.println(person.toString());
}
private static Person readObj() throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("/home/alex/test/test.object")));
Person person = (Person)ois.readObject();
ois.close();
return person;
}
public static void writeObj() throws FileNotFoundException, IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("/home/alex/test/test.object")));
oos.writeObject(new Person("alex", 33));
oos.close();
}
}
class Person implements Serializable{
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
- 打印流
打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式.
打印流根据流的分类:
字节打印流 PrintStream
字符打印流 PrintWriter
方法:
void print(String str):输出任意类型的数据,
void println(Stringstr): 输出任意类型的数据,自动写入换行操作
package exrcise;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Demo3 {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter("/home/alex/test/printStream.txt"));
// PrintWriter pw = new PrintWriter(System.out);
for(int i=0;i<3;i++) {
//打印到输出流
pw.println("Hello World!");
}
pw.close();
}
}