8.5IO流对象学习2

2017-08-07  本文已影响0人  lufaqiang

子符输出流缓冲流

BufferedWrite继承Writer
方法 writer() 写单个字符 字符数组 字符串
构造方法:
BufferedReader(Reader r)
可以传递任意的字符输出流
传递谁,就高效谁
能传递的字符输出流 FileWriter, OutputStreamWriter

 BufferedWriter 具有自己特有的方法
   void  newLine() 写换行
  
 newLine()文本中换行, \r\n也是文本换行
  方法具有平台无关性
Windows  \r\n
 Linux    \n
  
newLine()运行结果,和操作系统是相互关系
  JVM: 安装的是Windows版本,newLine()写的就是\r\n
         安装的是Linux版本,newLine()写的就是\n

代码

public class BufferedWrierDemo {
    public static void main(String[] args) throws IOException{
        //创建字符输出流,封装文件
        FileWriter fw = new FileWriter("c:\\buffer.txt");
        BufferedWriter bfw = new BufferedWriter(fw);
        
        bfw.write("你好");
        bfw.newLine();
        bfw.flush();
        
        
        bfw.write("我好好");
        bfw.newLine();
        bfw.flush();

        bfw.write("大家都好");
        bfw.flush();
        
        bfw.close();
        
    }
}

字符输入缓冲流

读取功能 read() 单个字符,字符数组
构造方法:
BufferedReader(Reader r)
可以任意的字符输入流
FileReader InputStreamReader

 BufferedReader自己的功能
  String readLine() 读取文本行 \r\n

方法读取到流末尾,返回null

readLine()方法返回行的有效字符,没有\r\n

public class BufferedReaderDemo {
    public static void main(String[] args) throws IOException {
        int lineNumber = 0;
        //创建字符输入流缓冲流对象,构造方法传递字符输入流,包装数据源文件
        BufferedReader bfr = new BufferedReader(new FileReader("c:\\a.txt"));
        //调用缓冲流的方法 readLine()读取文本行
        //循环读取文本行, 结束条件 readLine()返回null
        String line = null;
        while((line = bfr.readLine())!=null){
            lineNumber++;
            System.out.println(lineNumber+"  "+line);
        }
        bfr.close();
    }
}

Properties类

Properties集合的特有方法

1、store(OutputStream out)
store(Writer w)
接收所有的字节或者字符的输出流,将集合中的键值对,写回文件中保存

public static void function()throws IOException{
        Properties pro = new Properties();
        pro.setProperty("name", "zhangsan");
        pro.setProperty("age", "31");
        pro.setProperty("email", "123456789@163.com");
        FileWriter fw = new FileWriter("c:\\pro.properties");
        //键值对,存回文件,使用集合的方法store传递字符输出流
        pro.store(fw, "");
        fw.close();
    }

2、load(InputStream in)
load(Reader r)
传递任意的字节或者字符输入流
流对象读取文件中的键值对,保存到集合

public static void function_1()throws IOException{
        Properties pro = new Properties();
        FileReader fr = new FileReader("c:\\pro.properties");
        //调用集合的方法load,传递字符输入流
        pro.load(fr);
        fr.close();
        System.out.println(pro);
    }

使用Properties集合,存储键值对
* setProperty等同与Map接口中的put
* setProperty(String key, String value)
* 通过键获取值, getProperty(String key)

    public static void function(){
        Properties pro = new Properties();
        pro.setProperty("a", "1");
        pro.setProperty("b", "2");
        pro.setProperty("c", "3");
        System.out.println(pro);
        
        String value = pro.getProperty("c");
        System.out.println(value);
        
        //方法stringPropertyNames,将集合中的键存储到Set集合,类似于Map接口的方法keySet
        Set<String> set = pro.stringPropertyNames();
        for(String key : set){
            System.out.println(key+"..."+pro.getProperty(key));
        }
    }

对象的序列化和反序列化

IO流对象,实现对象Person序列化,和反序列化

public class ObjectStreamDemo {
    public static void main(String[] args)throws IOException, ClassNotFoundException {
//      writeObject();
        readObject();
    }
    /*
     * ObjectInputStream
     * 构造方法:ObjectInputStream(InputStream in)
     * 传递任意的字节输入流,输入流封装文件,必须是序列化的文件
     * Object readObject()  读取对象
     */
    public static void readObject() throws IOException, ClassNotFoundException{
        FileInputStream fis = new FileInputStream("c:\\person.txt");
        //创建反序列化流,构造方法中,传递字节输入流
        ObjectInputStream ois = new ObjectInputStream(fis);
        //调用反序列化流的方法 readObject()读取对象
        Object obj =ois.readObject();
        System.out.println(obj);
        ois.close();
    }
    /*
     * ObjectOutputStream
     * 构造方法: ObjectOutputStream(OutputSteam out)
     * 传递任意的字节输出流
     * void writeObject(Object obj)写出对象的方法
     */
    public static void writeObject() throws IOException{
        //创建字节输出流,封装文件
        FileOutputStream fos = new FileOutputStream("c:\\person.txt");
        //创建写出对象的序列化流的对象,构造方法传递字节输出流
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        Person p = new Person("lisi",25);
        //调用序列化流的方法writeObject,写出对象
        oos.writeObject(p);
        oos.close();
    }
}

person类

public class Person implements Serializable{
    public String name;
    public int age;
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Person(){}
    
    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 + "]";
    }

    
}

注:1、序列化必须实现serializable接口
serializable接口没有抽象方法,起标记作用
2、transinent瞬态关键字
阻止成员变量序列化
String StringBuilder StringBuffer

上一篇 下一篇

猜你喜欢

热点阅读