栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java---IO流:特殊操作流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java---IO流:特殊操作流

打印流
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 keySet = prop.keySet();
        for (Object key : keySet) {
            Object value = prop.get(key);
            System.out.println(key + "," + value);
        }

    }
}
 
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();


    }

}

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号