6.12.8 PrintWriter
文本输出打印流,方便java打印操作。
优势:
自动创建文件
自动刷新
自动缓冲
自动换行 pringln()
缺点:
只能覆盖写入,不能追加写入。
构造方法:
new PrintWriter(File f);
方法:
prinf()
println()
printf()
format()
close()
public class TestWriter {
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter("D:\\6.txt");
pw.println("welcome to qf");
pw.println("哈哈哈");
pw.close();
}
}
练习:控制台接收字符串,学生名字,当接收end,停止接收,每接收一个写入【PrintWriter】
import java.awt.print.Printable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
//练习:控制台接收字符串,学生名字,当接收end,
//停止接收,每接收一个写入【PrintWriter】
public class printll {
public static void main(String[] args) throws IOException {
Scanner sc =new Scanner(System.in);
boolean boo = true;
PrintWriter pw = new PrintWriter("E:\\Test\\3\\1.txt");
while(boo) {
System.out.println("请输入学生名字:");
String name = sc.next();
if(name.equals("end")) {
boo = false;
break;
}
pw.println(name);
}
pw.close();
}
}
printf : 格式化输出
public class TestWriter3 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("D:\\三体去空行.txt");
BufferedReader br = new BufferedReader(fr);
PrintWriter pw = new PrintWriter("D:\\c.txt");
int count = 1;
String line = br.readLine();
while(line != null) {
pw.printf("%04d", count++); //以格式为4位数不足补0的格式
pw.println(line);
line = br.readLine();
}
br.close();
pw.close();
}
}
6.12.9 InputStreamReader
将字节流向字符流转化的桥梁。字节流需要BufferedReader,需要此类做转化。
public class InputStreamReader extends Reader{
}
构造方法:
Reader r = new InputStreamReader(InputStream i);
借助BufferedReader:
public class TestInputStreamReader {
public static void main(String[] args) throws IOException {
System.out.println("输入内容:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
System.out.println("您写了:"+line);
br.close();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/f6279a331cad1004674b75eba04de6e3.png)
6.12.10 操作配置文件Properties
java.util.Properties 键值对存储,Map下。专门用于操作配置文件,参数配置是键值对类型。
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载
构造方法:
new Properties()
方法:
load(Reader r); //载入配置文件
load(InputStream r); //载入配置文件
getProperty(String key); //直接根据键取值
public class TestProperties {
public static void main(String[] args) throws IOException, IOException {
Properties p = new Properties();
//将配置文件载入
p.load(new FileReader(new File("D:\\info.ini")));
System.out.println(p.getProperty("ip"));
}
}
6.13 对象序列化和反序列化
对象序列化就是将创建的JAVA对象写入到文件中,长久保存下来。
对象反序列化就是将对象从文件中读取出来。
作用:
【对象持久化】
对象可以长久存储到文件。
实现:
1. 什么样的对象可以序列化?
必须实现: java.io.Serializable
类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。
2. 序列化 ObjectOutputStream
ObjectOutputStream(OutputStream out)
writeObject(Object obj)
3. 反序列化 ObjectInputStream
Object readObject()
4. 单独某个属性不参与序列化和反序列化
private transient int age;
5. EOFException
示例:
public class Student implements Serializable{
private int no;
private String name;
}
public class TestStudent {
public static void main(String[] args) throws IOException {
String username = "ys";
Integer age = 20;
Student s = new Student(1, "yushuang");
//学生集合
ArrayList<Student> stu = new ArrayList<Student>();
//序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\oos.txt"));
oos.writeObject(username);
oos.writeObject(age);
oos.writeObject(s);
oos.close();
}
}
public class TestStudent2 {
public static void main(String[] args) throws IOException, IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\oos.txt"));
Object o1 = ois.readObject();
Object o2 = ois.readObject();
Student o3 =(Student) ois.readObject();
System.out.println(o1);
System.out.println(o2);
System.out.println(o3.getNo() + "," + o3.getName());
}
}