第八篇 IO流技术(八)
package com.zzp.demo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; /** * * 转换流:InputStreamReader\OutputStreamWrite * 1、以字符流的形式操作字节流(纯文本) * 2、指定字符集 * @author java * */ public class ConvertTest01 { public static void main(String[] args) { try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));){ String msg = ""; while(!msg.equals("exit")){ msg = reader.readLine();//循环读取 writer.write(msg);//循环写出 writer.newLine(); writer.flush();//强制刷新 } } catch (Exception e) { System.out.println("异常"+e); } } }
package com.zzp.demo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; /** * * 转换流:InputStreamReader\OutputStreamWrite * 1、以字符流的形式操作字节流(纯文本) * 2、指定字符集 * @author java * */ public class ConvertTest02 { public static void main(String[] args) { test03(); } public static void test01(){ try(InputStream is = new URL("http://www.baidu.com").openStream();){ int temp; while((temp = is.read()) != -1){ System.out.print((char)temp); } } catch (Exception e) { System.out.println("异常"+e); } } public static void test02(){ try(InputStreamReader isr = new InputStreamReader( new URL("http://www.baidu.com").openStream(),"UTF-8");){ int temp; while((temp = isr.read()) != -1){ System.out.print((char)temp); } } catch (Exception e) { System.out.println("异常:"+e); } } public static void test03(){ try(BufferedReader reader = new BufferedReader( new InputStreamReader( new URL("http://www.baidu.com").openStream(),"UTF-8")); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream("baidu.html"),"UTF-8"))){ String msg; while((msg=reader.readLine())!=null){ writer.write(msg); writer.newLine(); } writer.flush(); } catch (Exception e) { System.out.println("异常" + e); } } }
package com.zzp.demo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutput; import java.io.DataOutputStream; import java.io.IOException; /** * * 数据流 * 1、写出后读取 * 2、读取的顺序和写出后一致 * DataoutputStream * DatainputStream * @author java * */ public class DataTest { public static void main(String[] args) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(baos)); dos.writeUTF("这小子真帅"); dos.writeInt(12); dos.writeBoolean(false); dos.writeByte('a'); dos.flush(); byte[] byteArray = baos.toByteArray(); System.out.println(byteArray.length); DataInputStream dis = new DataInputStream( new BufferedInputStream(new ByteArrayInputStream(byteArray))); String readUTF = dis.readUTF(); int readInt = dis.readInt(); boolean readBoolean = dis.readBoolean(); byte readByte = dis.readByte(); System.out.println(readUTF); } }
对象流
package com.zzp.demo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutput; import java.io.DataOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Date; /** * * 对象流: * 1、写出后读取 * 2、读取的顺序与写出保持一致 * 3、不是所有的对象都可以序列化Serializable * ObjectOutputStream * ObjectInputStream * @author java * */ public class ObjectTest { public static void main(String[] args) throws IOException, ClassNotFoundException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream dos = new ObjectOutputStream(new BufferedOutputStream(baos)); dos.writeUTF("这小子真帅"); dos.writeInt(12); dos.writeBoolean(false); dos.writeByte('a'); //对象 dos.writeObject("人生没有什么是大不了的"); dos.writeObject(new Date()); dos.writeObject(true); dos.writeObject(new Employee("张三", 400)); dos.flush(); byte[] byteArray = baos.toByteArray(); System.out.println(byteArray.length); ObjectInputStream dis = new ObjectInputStream( new BufferedInputStream(new ByteArrayInputStream(byteArray))); String readUTF = dis.readUTF(); int readInt = dis.readInt(); boolean readBoolean = dis.readBoolean(); byte readByte = dis.readByte(); System.out.println(readUTF); Object str = dis.readObject(); Object date = dis.readObject(); Object blo = dis.readObject(); Object emp = dis.readObject(); if(str instanceof String){ String strObj = (String)str; System.out.println(strObj); } if(date instanceof Date){ Date dateObj = (Date)date; System.out.println(dateObj); } if(blo instanceof Boolean){ Boolean bloObj = (Boolean)blo; System.out.println(bloObj); } if(emp instanceof Employee){ Employee empObj = (Employee)emp; System.out.println(empObj.getName()+"---"+empObj.getSalary()); } } } class Employee implements java.io.Serializable{ private transient String name;//该数据不需要序列化 private double salary; public Employee(String name, double salary) { super(); this.name = name; this.salary = salary; } public Employee() { super(); // TODO Auto-generated constructor stub } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
package com.zzp.demo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutput; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Date; /** * * 对象流: * 1、写出后读取 * 2、读取的顺序与写出保持一致 * 3、不是所有的对象都可以序列化Serializable * ObjectOutputStream * ObjectInputStream * @author java * */ public class ObjectTest02 { public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectOutputStream dos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("3.txt"))); dos.writeUTF("这小子真帅"); dos.writeInt(12); dos.writeBoolean(false); dos.writeByte('a'); //对象 dos.writeObject("人生没有什么是大不了的"); dos.writeObject(new Date()); dos.writeObject(true); dos.writeObject(new Employee("张三", 400)); dos.flush(); dos.close(); ObjectInputStream dis = new ObjectInputStream( new BufferedInputStream(new FileInputStream("3.txt"))); String readUTF = dis.readUTF(); int readInt = dis.readInt(); boolean readBoolean = dis.readBoolean(); byte readByte = dis.readByte(); System.out.println(readUTF); Object str = dis.readObject(); Object date = dis.readObject(); Object blo = dis.readObject(); Object emp = dis.readObject(); if(str instanceof String){ String strObj = (String)str; System.out.println(strObj); } if(date instanceof Date){ Date dateObj = (Date)date; System.out.println(dateObj); } if(blo instanceof Boolean){ Boolean bloObj = (Boolean)blo; System.out.println(bloObj); } if(emp instanceof Employee){ Employee empObj = (Employee)emp; System.out.println(empObj.getName()+"---"+empObj.getSalary()); } dis.close(); } }