转换流
InputStreamReader
* InputStreamReader:使用指定的charset编码表,读取字节并将其解码为字符。(解码)
* 构造方法:
* InputStreamReader(InputStream in) 创建一个默认字符集
* InputStreamReader(InputStream in,String charsetName)
* 使用步骤:
* 1,创建对象;
* 2,read方法读取;
* 3,释放资源。
* 注意事项:构造方法中指定的编码名称要与文件的编码相同,防止发生乱码。
public static void main(String[] args) throws IOException {
InputStreamReader isr=new InputStreamReader(new FileInputStream("src\\IO\\ReverseStream\\utf-8.txt"),"utf-8");
int len=0;
while((len=isr.read())!=-1){
System.out.print((char)len);
}
isr.close();
}
OutputStreamWriter
* java.io.OutputStreamWriter extends Writer
* 构造方法:
* 1,OutputStreamWriter(OutputStream out) 创建使用默认字符编码的OutputStreamWriter
* 2,OutputStreamWriter(OutputStream out,String charsetName) 创建使用指定字符集的OutputStreamWriter
* 参数:
* 1,OutputStream out:转换后字节文件;
* 2,String charsetName:指定编码表名称,不区分大小写。
* 使用步骤:
* 1,创建OutputStreamWriter对象,指定传递字节输出流及编码表名称;
* 2,使用write方法,字符转换到字节存储到缓冲区;
* 3,flush方法将缓冲区字节刷新到文件中;
* 4,释放资源。
public static void main(String[] args) throws IOException {
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("src\\IO\\ReverseStream\\gbk.txt",true),"gbk");
osw.write("你好");
osw.close();
}
Prac
* 将Gbk编码,转换为Utf-8编码的文本文件,
* 步骤:
* 1,指定GBK编码的转换流,读取文件
* 2,使用UTF-8编码的转换流,写入文件
public static void main(String[] args) throws IOException {
InputStreamReader isr=new InputStreamReader(new FileInputStream("src\\IO\\ReverseStream\\gbk.txt"),"gbk");
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("src\\IO\\ReverseStream\\utf-8.txt",true),"utf-8");
int len=0;
while((len=isr.read())!=-1){
System.out.print((char)len);
osw.write((char)len);
}
isr.close();
osw.close();
}
序列化流
ObjectOutputStream
* java.io.ObjectOutputStream extends OutputStream 对象的序列化流
* 作用:把对象以流的方式写入到文件中保存
* 构造方法:
* ObjectOutputStream(OutputStream out) 创建写入指定OutputStream的ObjectOutputStream
* 特有成员方法:
* void writeObject(Object obj) 指定对象写入ObjectOutputStream
* 步骤:
* 1,创建对象;
* 2,使用writeObject方法,把对象写入文件;
* 3,释放资源。
public static void main(String[] args) throws IOException {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("src\\IO\\ObjectStream\\tes1.txt"));
oos.writeObject("111");
oos.close();
}
Serializable
* 类通过实现java.io.Serializable接口启用序列化功能,未实现此接口的类无法使用,抛出NotSerializableException异常
* Serialable接口也叫标记型接口:类实现接口会给类添加一个标记
* transient:瞬态关键字;修饰的成员变量不能被序列化
* 序列化冲突异常:
* 每次修改类的定义,都会给class文件生成一个新的序列号
* 解决方法:
* 无论是否对类的定义修改,都不重新生成序列号;
* 手动给类添加一个序列号。
* 格式在Serializable接口规定:
* 可序列化类可以通过声明名为“serialVersionUID”的字段,该字段必须是静态static,final,long型字段,显式声明
ObjectInputStream
* java.io.ObjectInputStream extends InputStream 反序列化流
* 把文件中保存的对象,以流的方式读取出来使用
* 构造方法:
* ObjectInputStream(InputStream in)创建从指定InputStream读取的ObjectInputStream
* 特有成员方法: Object readObject()
* 使用步骤:
* 1,创建对象;
* 2,使用readObject读取保存对象文件;
* 3,释放资源。
* ClassNotFoundExceptionbucun 不存在对象class文件时抛出异常
* 反序列化前提:类必须实现Serializable;必须存在类对应的class文件
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("src\\IO\\ObjectStream\\tes1.txt"));
Object obj=ois.readObject();
ois.close();
System.out.println(obj);
}
Prac
* 序列化集合:
* 多个对象存储到一个集合中,集合序列化和反序列化。
* 步骤:
* 1,定义一个存储Person对象的ArrayList集合;
* 2,使用集合存储Person对象;
* 3,创建ObjectOutputStream对象;
* 4,使用WriteObject方法对集合序列化;
* 5,创建反序列化ObjecInputStream对象;
* 6,将Object对象转换为ArrayList类型;
* 7,遍历ArrryList集合;
* 8,释放资源。
public class Person implements Serializable {
private String name;
private int age;
private static final long serialVersionID = 111L;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Prac {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ArrayList<Person> arr = new ArrayList<>();
Person p1 = new Person("aa", 12);
Person p2 = new Person("bb", 13);
Person p3 = new Person("cc", 14);
arr.add(p1);
arr.add(p2);
arr.add(p3);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src\\IO\\ObjectStream\\test1.txt"));
oos.writeObject(arr);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\\IO\\ObjectStream\\test1.txt"));
Object o = ois.readObject();
ArrayList<Object> arr1 =new ArrayList<>();
arr1.add(o);
for (Object p : arr1) {
System.out.println(p);
}
oos.close();
ois.close();
}
}
打印流PrintStream
* java.io.PrintStream extends OutputStream:为其他输出流添加了功能,便于打印各种数据值
* 特点:
* 1,只负责数据的输出,不负责数据的读取
* 2,与其他输出流不同,PrintStream永远不会抛出IOException
* 3,特有方法:
* void print()
* void println()
* 构造方法:
* PrintStream(File file)
* PrintStream(OutputStream out)
* PrintStream(String fileName)
*
* 继承自父类的成员方法:
* close(); flush();
* public void write(byte[] b)
* public void write(byte[] b,int off,int len)
* public abstract void write (int b)
* 注意事项:
* 使用write相关方法,会查询编码表;
* 使用print相关方法,数据原样输出。
public static void main(String[] args) throws FileNotFoundException {
PrintStream ps=new PrintStream("src\\IO\\PrintStream\\test1.txt");
ps.print(1234);
ps.close();
}