打印流
package IO流.特殊操作流;
import java.io.*;
public class 打印流 {
public static void main(String[] args) throws IOException {
PrintWriter pw=new PrintWriter(new FileWriter("idea_text\ps.txt"));
pw.println("hello");
pw.println("world");
}
}
复制java文件打印流改进版
package IO流.特殊操作流;
import java.io.*;
public class 复制java文件打印流改进版 {
public static void main(String[] args) throws IOException {
//根据数据源创建字符输入流对象
BufferedReader br = new BufferedReader(new FileReader("1"));
//根据目的地创建字符输出流对象
PrintWriter pw=new PrintWriter(new FileWriter("2"));
//读写数据,复制文件
String line;
while ((line = br.readLine()) != null) {
pw.println(line);
}
//释放资源
pw.close();
br.close();
}
}
Properties作为Map集合的使用
package IO流.特殊操作流.Properties;
import java.util.Properties;
import java.util.Set;
public class Properties作为Map集合的使用 {
public static void main(String[] args) {
//创建集合对象--Properties不是泛型
// Properties properties = new Properties();
Properties prop = new Properties();
//存储元素
prop.put("001", "z");
prop.put("002", "x");
prop.put("003", "c");
//遍历结合
Set
Properties特有方法
package IO流.特殊操作流.Properties;
import java.util.Properties;
import java.util.Set;
public class Properties特有方法 {
public static void main(String[] args) {
Properties prop=new Properties();
prop.setProperty("001", "z");
prop.setProperty("002", "x");
prop.setProperty("003", "c");
System.out.println(prop.getProperty("001"));
Set names = prop.stringPropertyNames();
for (String key : names) {
String value = prop.getProperty(key);
System.out.println(key + "=" + value);
}
}
}
Properties和IO流结合的方法
package IO流.特殊操作流.Properties;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Properties和IO流结合的方法 {
public static void main(String[] args) throws IOException {
//把集合中的数据保存文件中
myStore();
//把文件中的数据加载到集合
myload();
}
private static void myload() throws IOException {
Properties prop = new Properties();
//void load(Reader reader)
FileReader fr= new FileReader("idea_text\fw.txt");
prop.load(fr);
fr.close();
System.out.println(prop);
}
private static void myStore() throws IOException {
Properties prop = new Properties();
prop.setProperty("001","z");
prop.setProperty("002","x");
prop.setProperty("003","c");
//void store(Writer writer,String comments)
FileWriter fw = new FileWriter("idea_text\fw.txt");
prop.store(fw,null);
fw.close();
}
}
对象序列化流
package IO流.特殊操作流.对象序列化流;
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
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;
}
}
package IO流.特殊操作流.对象序列化流;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class 对象序列化流 {
public static void main(String[] args) throws IOException {
//创建一个写入指定的OutputStream的ObjectOutputStream
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("idea_text\oos.txt"));
//创建对象
Student s=new Student("zxz",30);
//void writeObject(Object obj):将指定的对象写入ObjectOutputStream
oos.writeObject(s);
//释放资源
oos.close();
}
}
对象反序列化流
package IO流.特殊操作流.对象序列化流;
import java.io.*;
public class 对象反序列化流 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//ObjectInputStream(InputStream in):创建从指定的IutStream读取的ObjectInputStream
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("idea_text\oos.txt"));
//Object readObject0:从ObjectInputStream读取一个对象
Object obj=ois.readObject();
Student s=(Student) obj;
System.out.println(s.getName()+","+s.getAge());
ois.close();
}
}
对象序列化流的问题
package IO流.特殊操作流.对象序列化流.对象序列化流的问题;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID =42L;
private String name;
// private int age;
private transient int age; //age=null;
public Student() {
}
public Student(String name, int age) {
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 "Student{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
package IO流.特殊操作流.对象序列化流.对象序列化流的问题;
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// write();
read();
}
//反序列化
private static void read() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("idea_text\oos.txt"));
Object obj = ois.readObject();
Student s=(Student) obj;
System.out.println(s.getName()+","+s.getAge());
ois.close();
}
//序列化
private static void write() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("idea_text\oos.txt"));
Student s= new Student("程序员",20);
oos.writeObject(s);
oos.close();
}
}