JAVA Class21

学习内容:

1.Properties类:

Properties类继承自HashTable,故而以键值形式储存元素。常用于IO流中储存元素的容器,可以从硬盘读写数据。

常用方法:

store,将数据写入硬盘

load,从硬盘读取数据

stringPropertyNames,返回所有键Set

setProperty(String,String),添加元素

getProperty,获取元素

public class Test {
    public static void write() {
        File f = new File("d:\\test\\properties.test");
        Properties p = new Properties();
        p.setProperty("g","stu");
        /*try (FileOutputStream fos = new FileOutputStream(f,true);){
            p.store(fos,"新增信息");
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        try (FileWriter fw = new FileWriter(f)){
            p.store(fw,"新增信息");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void read() {
        File f = new File("d:\\test\\properties.test");
        Properties p = new Properties();
        try (FileInputStream fis = new FileInputStream(f);){
            p.load(fis);
            Set<String> set = p.stringPropertyNames();
            for(Iterator<String> it = set.iterator();it.hasNext();) {
                String key = it.next();
                String value = p.getProperty(key);
                System.out.println(key+":"+value);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.对象流(对象序列化与反序列化)

将实例化的对象进行读写,注意,该类必须使用Serializable(序列化)接口

为什么要序列化、反序列化对象:进行数据传输时,为了统一起见,我们传输的数据或者经过文件保存的数据需要经过序列化和编码等操作,相当于交互双方有一个公共的标准,按照这种标准来做,不管各自的环境是否有差异,各自都可以根据这种标准来翻译出自己能理解的正确的数据。

(1)Serializable(序列化)接口是一个空接口,没有成员变量也没有抽象方法,这种接口被称为标记接口,使用该接口的类才能被序列化

需要注意的是:静态成员是不能被序列化的,因为静态成员是随着类的加载而加载的,与类共存亡,如果在另一个JVM读取“被序列化”的静态成员变量,则会使用当前JVM的静态成员变量值。

(2)transient修饰词:可以禁止成员变量被序列化。

(3)serialVersionUID:当修改修饰词时,前后自动生成序列号不匹配,已经序列化的对象无法在读取,serialVersionUID用来表示这个类当前的版本,如果有了变化,比如新设计了属性,就应该修改这个版本号,解决序列号不匹配问题。

import java.io.Serializable;

public class Hero implements Serializable{//空接口,标记型接口
    private String name;
    //表示这个类当前的版本,如果有了变化,比如新设计了属性,就应该修改这个版本号,解决序列号不匹配问题
    private static final long serialVersionUID = 1L;
    private int hp;//当修改修饰词时,前后序列号不匹配,已经序列化的对象无法在读取
    //静态成员是不能被序列化的,因为静态成员是随着类的加载而加载的,
    //与类共存亡,并且静态成员都是默认初始值;
    //禁止成员变量序列化
    //transient private String sex;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getHp() {
        return hp;
    }
    public void setHp(int hp) {
        this.hp = hp;
    }
    public Hero(String name, int hp) {
        super();
        this.name = name;
        this.hp = hp;
    }
    public Hero() {
        super();
    }
    
}
public class Test {
    public static void writeHero(ArrayList<Hero> herolist) {
        File f = new File("d:\\test\\Dota2.dota");
        try (FileOutputStream fis = new FileOutputStream(f);
              ObjectOutputStream oos = new ObjectOutputStream(fis);
              ){
            oos.writeObject(herolist);
            System.out.println("写入完成!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static ArrayList<Hero> readHero() {
        File f = new File("d:\\test\\Dota2.dota");
        ArrayList<Hero> herolist = new ArrayList<Hero>();
        try (FileInputStream fos = new FileInputStream(f);
              ObjectInputStream ois = new ObjectInputStream(fos);
              ){
             herolist = (ArrayList<Hero>)ois.readObject();
             System.out.println("读取完成!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return herolist;
    }
}

 3.Print流

PrintStream,PrintWriter

特点:不负责数据源,只负责数据目的,为其他输出流添加功能,永远不抛IO异常。

public class Test5 {
    public static void write() {
    public static void copy() {
        File f = new File("d:\\test\\LOL.txt");
        try ( BufferedReader br = new BufferedReader(new FileReader(f));
                PrintWriter pw = new PrintWriter(new FileWriter("d:\\test\\copy.txt"));
                ){
            String line =null;
            while((line=br.readLine())!=null) {
                pw.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        File f = new File("d:\\test\\print.txt");
        try (PrintStream ps = new PrintStream(f);
                //PrintWriter pw = new PrintWriter(f);
                FileOutputStream fos = new FileOutputStream(f);
                PrintWriter pw = new PrintWriter(fos,true);//传入流对象,调用println、printf、format自动flush
                ){//不负责数据源,只负责数据目的
            //为其他输出流添加功能,永远不抛IO异常
            ps.println("a");
            ps.println("b");
            pw.println("c");
            pw.println("d");

        } catch (IOException e) {
            e.printStackTrace();
        }
        int[] i = {1};
        System.out.println(i);//void java.io.PrintStream.println(Object x) 打印地址
        char[] ch = {'a','b'};
        System.out.println(ch);//void java.io.PrintStream.println(char[] x) 打印字符串
    }
}

 4.common.io包

提供操作文件、文件夹的封装工具

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

public class Test6 {
    public static void fn() {
        FilenameUtils fnu = new FilenameUtils();
        String name = fnu.getExtension("d:\\test\\LOL.txt");//获取扩展名
        String fname = fnu.getName("d:\\test\\LOL.lol");//获取文件名
        boolean flag = fnu.isExtension("d:\\test\\LOL.txt","txt");//检查文件类型
        System.out.println(name+""+fname+""+flag);
    }
    public static void main(String[] args) throws IOException {
        fn();
        FileUtils fu = new FileUtils();
        System.out.println();
        String s =fu.readFileToString(new File("d:\\test\\LOL.txt"),"UTF-8");//读取文件内容
        fu.copyDirectory(new File("d:\\study"), new File("d:\\tocopy"));//复制文件夹
    }
}

猜你喜欢

转载自www.cnblogs.com/whwjava/p/8917728.html