IO流(操作文件内容): 转换流, 输出/输入字符流, 对象的序列化, 序列流, Properties, 打印流, 操作数组/基本数据类型的流对象

转换流

  • 输入字节流的转换流:InputStreamReader 是字节流通向字符流的桥InputStreamReader
  • 输出字节流的转换流:OutputStreamWriter 可以把输出字节流转换成输出字符流
  • 转换流的作用:
    1. 如果目前所 获取到的是一个字节流需要转换字符流使用,这时候就可以使用转换流。 字节流----> 字符流
    2. 使用转换流可以指定编码表进行读写文件。
public class Demo{
    public static void main(String[] args) throws IOException {
//      readTest();
//      writeTest();'
        
//      writeTest2();
        readTest2();
      }
    
    //使用输入字节流的转换流指定码表进行读取文件数据
    public static void readTest2() throws IOException{
        File file = new File("F:\\a.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        //创建字节流的转换流并且指定码表进行读取
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"utf-8");
        char[] buf = new char[1024];
        int length = 0;
        while((length = inputStreamReader.read(buf))!=-1){
            System.out.println(new String(buf,0,length));
        }   
    }
    
    //使用输出字节流的转换流指定码表写出数据
    public static void writeTest2() throws IOException{
        File file = new File("F:\\a.txt");
        //建立数据的输出通道
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        //把输出字节流转换成字符流并且指定编码表。
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
        outputStreamWriter.write("新中国好啊");
        //关闭资源
        outputStreamWriter.close(); 
    }
    
    public static void writeTest() throws IOException{
        File file = new File("F:\\a.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        //把输出字节流转换成输出字符流。
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
        
        outputStreamWriter.write("大家好");
        outputStreamWriter.close();
    }
    
    
    public static void readTest() throws IOException{
        InputStream in = System.in; //获取了标准的输入流。
//      System.out.println("读到 的字符:"+ (char)in.read());  //read()一次只能读取一个字节。
        //需要把字节流转换成字符流。
        InputStreamReader inputStreamReader = new InputStreamReader(in);
        //使用字符流的缓冲类
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line = null;
        while((line = bufferedReader.readLine())!=null){
            System.out.println("内容:"+ line);
        }
    }
}

输入字符流

  • Reader 输入字符流的基类抽象类

    • FileReader 读取文件的输入字符流
  • FileReader的用法:

    1. 找到目标文件
    2. 建立数据的输入通道
    3. 读取数据
    4. 关闭资源
public class Demo {

   public static void main(String[] args) throws IOException {
       readTest2();
   }
   
   //使用缓冲字符数组读取文件。
   public static void readTest2() throws IOException{
       //找到目标文件
       File file = new File("F:\\my.java");
       // 建立数据的输入通道
       FileReader fileReader = new FileReader(file);
       //建立缓冲字符数组读取文件数据
       char[] buf = new char[1024];
       int length = 0 ; 
       while((length = fileReader.read(buf))!=-1){
           System.out.print(new String(buf,0,length));
       }
   }
   
   public static void readTest1() throws IOException{
       //找到目标文件
       File file = new File("my.java");
       //建立数据的输入通道
       FileReader fileReader = new FileReader(file);
       int content = 0 ;
       while((content = fileReader.read())!=-1){ //每次只会读取一个字符,效率低。
           System.out.print((char)content);
       }
       //关闭资源
       fileReader.close();
   }
}

输出字符流

  • Writer 输出字符流的基类 (抽象类)

    • FileWriter 向文件数据数据的输出字符流
  • FileWriter的使用步骤:

    1. 找到目标文件
    2. 建立数据输出通道
    3. 写出数据
    4. 关闭资源
  • FileWriter要注意的事项:

    1. 使用FileWriter写数据的时候,FileWriter内部是维护了一个1024个字符数组的,写数据的时候会先写入到它内部维护的字符数组中,如果需要把数据真正写到硬盘上,需要调用flush或者是close方法或者是填满了内部的字符数组。
    2. 使用FileWriter的时候,如果目标文件不存在,那么会自动创建目标文件。
      3.使用FileWriter的时候, 如果目标文件已经存在了,那么默认情况会先情况文件中的数据,然后再写入数据 , 如果需要在原来的基础上追加数据,需要使用“new FileWriter(File , boolean)”的构造方法,第二参数为true
  • 练习: 使用字符流拷贝一个文本文件(java文件)接着使用字符流拷贝一个图片(观察图片的大小变化,思考为什么会这样子?)

public class Demo {

    public static void main(String[] args) throws IOException {
        writeTest1();
    }
    
    public static void  writeTest1() throws IOException{
        //找到目标文件
        File file = new File("F:\\a.txt");
        //建立数据输出通道
        FileWriter fileWriter = new FileWriter(file,true);
        //准备数据,把数据写出
        String data = "今天天气非常好!!";
        fileWriter.write(data);  //字符流具备解码的功能。
        //刷新字符流
//      fileWriter.flush();
        //关闭资源
        fileWriter.close();
    }
}

对象的序列化

  • 当创建对象时, 程序运行时它就会存在, 但是程序停止时, 对象也就消失了. 但是如果希望对象在程序不运行的情况下仍能存在并保存其信息,将会非常有用,对象将被重建并且拥有与程序上次运行时拥有的信息相同。可以使用对象的序列化

    • 对象的序列化 : 将内存中的对象直接写入到文件设备中
    • 对象的反序列化 : 将文件设备中持久化的数据转换为内存对象
  • 基本的序列化由两个方法产生 : 一个方法用于序列化对象并将它们写入一个流,另一个方法用于读取流并反序列化对象

    • ObjectOutput - writeObject(Object obj) : 将对象写入底层存储或流
    • ObjectInput - readObject() : 读取并返回对象
  • ObjectOutputStream(对象的输出流) / ObjectInputStream(对象的输入流)

    • 对象的输入输出流 : 对象的输入输出流主要的作用是用于写对象的信息与读取对象的信息, 对象信息一旦写到文件上那么对象的信息就可以做到持久化了
    • ObjectOutputStream的使用步骤:(对象输入输出流要注意的细节)
      1. 如果对象需要被写出到文件上,那么对象所属的类必须要实现Serializable接口Serializable接口没有任何的方法,是一个标识接口而已
      2. 对象的反序列化创建对象的时候并不会调用到构造方法的、
      3. serialVersionUID 是用于记录class文件的版本信息的,serialVersionUID这个数字是通过一个类的类名、成员、包名、工程名算出的一个数字
      4. 使用ObjectInputStream反序列化的时候,ObjeectInputStream会先读取文件中的serialVersionUID,然后与本地的class文件的serialVersionUID进行对比,如果这两个id不一致,那么反序列化就失败了
      5. 如果序列化与反序列化的时候可能会修改类的成员,那么最好一开始就给这个类指定一个serialVersionUID,如果一类已经指定的serialVersionUID,然后在序列化与反序列化的时候,jvm都不会再自己算这个 class的serialVersionUID了
      6. 如果一个对象某个数据不想被序列化到硬盘上,可以使用关键字transient修饰
      7. 如果一个类维护了另外一个类的引用,那么另外一个类也需要实现Serializable接口
    • 案例: 序列化和反序列化Cat对象
public class Demo3 {
      public static void main(String[] args) throws IOException,
            ClassNotFoundException {
                Cat cat = new Cat("tom", 3);
        FileOutputStream fos = new FileOutputStream(new File("c:\\Cat.txt"));
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(cat);
        System.out.println(cat);
        oos.close();
        // 反序列化
        FileInputStream fis = new FileInputStream(new File("c:\\Cat.txt"));
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object readObject = ois.readObject();
        Cat cat2 = (Cat) readObject;
        System.out.println(cat2);
        fis.close();
  }
class Cat implements Serializable {
    public String name;
    public int age;

    public Cat() {
    }
    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Cat [name=" + name + ", age=" + age + "]";
    }
}
- 例子关键点:
  1. 声明Cat类实现了Serializable接口。是一个标示器,没有要实现的方法
  2. 新建Cat对象
  3. 新建字节流对象(FileOutputStream)进序列化对象保存在本地文件中
  4. 新建ObjectOutputStream对象,调用writeObject方法序列化Cat对象
  5. writeObject方法会执行两个工作:序列化对象,然后将序列化的对象写入文件中
  6. 反序列化就是调用ObjectInputStream的readObject()方法
  7. 异常处理和流的关闭动作要执行
  • Serializable
    • 类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。所以需要被序列化的类必须是实现Serializable接口,该接口中没有描述任何的属性和方法,称之为标记接口。如果对象没有实现接口Serializable,在进行序列化时会抛出:NotSerializableException 异常

    • 注意:保存一个对象的真正含义是什么?如果对象的实例变量都是基本数据类型,那么就非常简单。但是如果实例变量是包含对象的引用,会怎么样?保存的会是什么?很显然在Java中保存引用变量的实际值没有任何意义,因为Java引用的值是通过JVM的单一实例的上下文中才有意义。通过序列化后,尝试在JVM的另一个实例中恢复对象,是没有用处的

    • 如下:

      • 首先建立一个Dog对象,也建立了一个Collar对象。Dog中包含了一个Collar(项圈)现在想要保存Dog对象,但是Dog中有一个Collar,意味着保存Dog时也应该保存Collar。假如Collar也包含了其他对象的引用,那么会发生什么?意味着保存一个Dog对象需要清楚的知道Dog对象的内部结构。会是一件很麻烦的事情。
      • Java的序列化机制可以解决该类问题,当序列化一个对象时,Java的序列化机制会负责保存对象的所有关联的对象(就是对象图),反序列化时,也会恢复所有的相关内容。本例中:如果序列化Dog会自动序列化Collar。但是,只有实现了Serializable接口的类才可以序列化。如果只是Dog实现了该接口,而Collar没有实现该接口。会发生什么?
    • Dog类和Collar类

import java.io.Serializable;
public class Dog implements Serializable {
    private Collar collar;
    private String name;

    public Dog(Collar collar, String name) {

        this.collar = collar;
        this.name = name;
    }

    public Collar getCollar() {
        return collar;
    }
}
class Collar {
      private int size;

      public int getSize() {
        return size;
      }
      public Collar(int size) {
        this.size = size;
      }
}
  • 序列化
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Demo4 {
    public static void main(String[] args) throws IOException {
        Collar coll = new Collar(10);
        Dog dog = new Dog(coll, "旺财");
        FileOutputStream fis = new FileOutputStream(new File("c:\\dog.txt"));
        ObjectOutputStream os = new ObjectOutputStream(fis);
        os.writeObject(dog);
    }
}
  • 执行程序,出现了运行时异常: Exception in thread "main" java.io.NotSerializableException: Collar
  • 所以我们也必须将Dog中使用的Collar序列化。但是如果我们无法访问Collar的源代码,或者无法使Collar可序列化,如何处理?
    • 两种解决方法:
      1. 继承Collar类, 使子类可序列化, 但是如果Collar是final类, 就无法继承了. 并且, 如果Collar引用了其他非序列化对象, 也无法解决该问题
      2. transient : 此时就可以使用transient修饰符, 可以将Dog类中的成员变量标识为transient, 那么在序列化Dog对象时, 序列化就会跳过Collar
public class Demo4 {
            public static void main(String[] args) throws IOException,
            ClassNotFoundException {
                    Collar coll = new Collar(10);
                    Dog dog = new Dog(coll, "旺财");
                    System.out.println(dog.getCollar().getSize());

                    FileOutputStream fis = new FileOutputStream(new File("c:\\d         og.txt"));
                    ObjectOutputStream os = new ObjectOutputStream(fis);
        os.writeObject(dog);

                    // 反序列化
                    FileInputStream fos = new FileInputStream(new File("c:\\dog.txt"));
                    ObjectInputStream ois = new ObjectInputStream(fos);
                    Object readObject = ois.readObject();
                    Dog dog2 = (Dog) readObject;
                    // Collar未序列化
                    dog2.getCollar().getSize();
            }
}
- 这样我们具有一个序列化的Dog和非序列化的Collar。此时反序列化Dog后,访问Collar,就会出现运行时异常 `` Exception in thread "main" java.lang.NullPointerException ``
- 注意:序列化不适用于静态变量,因为静态变量并不属于对象的实例变量的一部分。静态变量随着类的加载而加载,是类变量。由于序列化只适用于对象。
  • 基本数据类型可以被序列化
public class Demo5 {
      public static void main(String[] args) throws IOException {
        // 创建序列化流对象
        FileOutputStream fis = new FileOutputStream(new File("c:\\basic.txt"));
        ObjectOutputStream os = new ObjectOutputStream(fis);
        // 序列化基本数据类型
        os.writeDouble(3.14);
        os.writeBoolean(true);
        os.writeInt(100);
        os.writeInt(200);

        // 关闭流
        os.close();

        // 反序列化
        FileInputStream fos = new FileInputStream(new File("c:\\basic.txt"));
        ObjectInputStream ois = new ObjectInputStream(fos);

        System.out.println(ois.readDouble());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readInt());
        System.out.println(ois.readInt());

        fos.close();
      }
}
  • serialVersionUID : 用于给类指定一个UID。该UID是通过类中的可序列化成员的数字签名运算出来的一个long型的值。只要是这些成员没有变化,那么该值每次运算都一样。该值用于判断被序列化的对象和类文件是否兼容。如果被序列化的对象需要被不同的类版本所兼容。可以在类中自定义UID。

    • 定义方式:static final long serialVersionUID = 42L;
  • 对象序列化示例:

class Address implements Serializable{
    String country;
    String city;
    public Address(String country,String city){
        this.country = country;
        this.city = city;
    }   
}
class User implements Serializable{
    
    private static final long serialVersionUID = 1L;
    String userName ;
    String password;
    transient int age;  // transient 透明
    Address address ;
    
    public User(String userName , String passwrod) {
        this.userName = userName;
        this.password = passwrod;
    }

    public User(String userName , String passwrod,int age,Address address) {
        this.userName = userName;
        this.password = passwrod;
        this.age = age;
        this.address = address;
    }
    
    @Override
    public String toString() {
        return "用户名:"+this.userName+ " 密码:"+ this.password+" 年龄:"+this.age+" 地址:"+this.address.city;
    }
}
public class Demo {
    
    public static void main(String[] args) throws IOException, Exception {
        writeObj();
//      readObj();
    }
    
    //把文件中的对象信息读取出来-------->对象的反序列化
    public static void readObj() throws  IOException, ClassNotFoundException{
        //找到目标文件
        File file = new File("F:\\obj.txt");
        //建立数据的输入通道
        FileInputStream fileInputStream = new FileInputStream(file);
        //建立对象的输入流对象
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        //读取对象信息
        User user = (User) objectInputStream.readObject(); //创建对象肯定要依赖对象所属 的class文件。
        System.out.println("对象的信息:"+ user);
    }

    //定义方法把对象的信息写到硬盘上------>对象的序列化。
    public static void writeObj() throws IOException{
        //把user对象的信息持久化存储。
        Address address = new Address("中国","广州");
        User user = new User("admin","123",15,address);
        //找到目标文件
        File file = new File("F:\\obj.txt");
        //建立数据输出流对象
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        //建立对象的输出流对象
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        //把对象写出
        objectOutputStream.writeObject(user);
        //关闭资源
        objectOutputStream.close();
    }
}

序列流 : SequenceInputStream

  • 序列流,对多个流进行合并。SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。

  • 合并两个流 : 使用构造函数 SequenceInputStream(InputStream s1, InputStream s2)

private static void testSequenceInputStream() throws IOException {
        FileInputStream fis1 = new FileInputStream("c:\\a.txt");
        FileInputStream fis2 = new FileInputStream("c:\\b.txt");

        SequenceInputStream s1 = new SequenceInputStream(fis1, fis2);
        int len = 0;
        byte[] byt = new byte[1024];

        FileOutputStream fos = new FileOutputStream("c:\\z.txt");

        while ((len = s1.read(byt)) != -1) {
            fos.write(byt, 0, len);
        }
        s1.close();
    }
  • 合并多个流:
public static void testSequenceInputStream() throws Exception {
        InputStream in1 = new FileInputStream("c:/a.txt");
        InputStream in2 = new FileInputStream("c:/b.txt");
        InputStream in3 = new FileInputStream("c:/c.txt");

        LinkedHashSet<InputStream> set = new LinkedHashSet<InputStream>();
        set.add(in1);
        set.add(in2);
        set.add(in3);
        final Iterator<InputStream> iter = set.iterator();

        SequenceInputStream sin = new SequenceInputStream(
                new Enumeration<InputStream>() {
                    @Override
                    public boolean hasMoreElements() {
                        return iter.hasNext();
                    }

                    @Override
                    public InputStream nextElement() {
                        return iter.next();
                    }
                });

        FileOutputStream out = new FileOutputStream("c:/z.txt");

        for (int b = -1; (b = sin.read()) != -1;) {
            out.write(b);
        }
        sin.close();
        out.close();
    }
  • 案例:将map3歌曲文件进行切割拷贝,并合并.
public class Demo2 {
    public static void main(String[] args) throws IOException {
        
        split(new File("c:\\a.mp3"), 10, new File("c:\\"));
        System.out.println("切割完毕");

        LinkedHashSet<InputStream> hs = new LinkedHashSet<InputStream>();   
        hs.add(new FileInputStream(new File("c:\\part.1.mp3")));
        hs.add(new FileInputStream(new File("c:\\part.2.mp3")));
        hs.add(new FileInputStream(new File("c:\\part.3.mp3")));
        hs.add(new FileInputStream(new File("c:\\part.4.mp3")));
        merage(hs, new File("c:\\merage.mp3"));
        System.out.println("合并完毕");
    }

    private static void merage(LinkedHashSet<InputStream> hs, File dest)
            throws IOException {

        final Iterator<InputStream> it = hs.iterator();
        FileOutputStream fos = new FileOutputStream(dest);
        SequenceInputStream seq = new SequenceInputStream(
                new Enumeration<InputStream>() {

                    @Override
                    public boolean hasMoreElements() {

                        return it.hasNext();
                    }

                    @Override
                    public InputStream nextElement() {
                        return it.next();
                    }
                });
        byte[] byt = new byte[1024 * 1024];
        int len = 0;
        while ((len = seq.read(byt)) != -1) {
            fos.write(byt, 0, len);
        }
        seq.close();
        fos.close();
    }

    // 1. 切割文件
    /*
     * 切割文件,切割份数, 切割后保存路径
     */
    private static void split(File src, int count, File dir) throws IOException {
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = null;
        byte[] byt = new byte[1024 * 1024];
        int len = 0;
        for (int i = 1; i <= count; i++) {
            len = fis.read(byt);
            if (len != -1) {
                fos = new FileOutputStream(dir + "part." + i + ".mp3");
                fos.write(byt, 0, len);
            }

            // fos.close();

        }
        fis.close();

    }
}
  • 合并文件
public class Demo {
    public static void main(String[] args) throws IOException {
        merge3();
    }
    // 把三个文件合并成一个文件
    public static void merge3() throws IOException{
        //找到目标文件
        File file1 = new File("F:\\a.txt");
        File file2 = new File("F:\\b.txt");
        File file3 = new File("F:\\c.txt");
        File file4 = new File("F:\\d.txt");

        //建立对应 的输入输出流对象
        FileOutputStream fileOutputStream = new FileOutputStream(file4);
        FileInputStream fileInputStream1 = new FileInputStream(file1);
        FileInputStream fileInputStream2 = new FileInputStream(file2);
        FileInputStream fileInputStream3 = new FileInputStream(file3);

        //创建序列流对象
        Vector<FileInputStream> vector = new Vector<FileInputStream>();
        vector.add(fileInputStream1);
        vector.add(fileInputStream2);
        vector.add(fileInputStream3);
        Enumeration<FileInputStream> e = vector.elements();

        SequenceInputStream sequenceInputStream = new SequenceInputStream(e);
        
        //读取文件数据
        byte[] buf = new byte[1024];
        int length = 0; 
        
        while((length = sequenceInputStream.read(buf))!=-1){
            fileOutputStream.write(buf,0,length);
        }
        
        //关闭资源
        sequenceInputStream.close();
        fileOutputStream.close();
    }
    
          //使用SequenceInputStream合并文件
    public static void merge2() throws IOException{
        //找到目标文件
        File inFile1 = new File("F:\\a.txt");
        File inFile2 = new File("F:\\b.txt");
        File outFile = new File("F:\\c.txt");
        //建立数据的输入输出通道
        FileOutputStream fileOutputStream = new FileOutputStream(outFile);
        
        FileInputStream fileInputStream1 = new FileInputStream(inFile1);
        FileInputStream fileInputStream2 = new FileInputStream(inFile2);
        //建立序列流对象
        SequenceInputStream inputStream = new SequenceInputStream(fileInputStream1,fileInputStream2);
        byte[] buf = new byte[1024];
        int length = 0 ; 
        
        while((length = inputStream.read(buf))!=-1){
            fileOutputStream.write(buf,0,length);
        }
        //关闭资源
        inputStream.close();
        fileOutputStream.close();
    }
    
    //需求:把a.txt与b.txt 文件的内容合并。
    public static void merge1() throws IOException{
        //找到目标文件
        File inFile1 = new File("F:\\a.txt");
        File inFile2 = new File("F:\\b.txt");
        File outFile = new File("F:\\c.txt");
        //建立数据的输入输出通道
        FileOutputStream fileOutputStream = new FileOutputStream(outFile);
        FileInputStream fileInputStream1 = new FileInputStream(inFile1);
        FileInputStream fileInputStream2 = new FileInputStream(inFile2);
        
        //把输入流存储到集合中,然后再从集合中读取
        ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();
        list.add(fileInputStream1);
        list.add(fileInputStream2);
        
        // 准备一个缓冲数组
        byte[] buf = new byte[1024];
        int length = 0 ; 
        
        for(int i = 0 ; i< list.size() ; i++){
            FileInputStream fileInputStream = list.get(i);
            while((length = fileInputStream.read(buf))!=-1){
                fileOutputStream.write(buf,0,length);
            }
            //关闭资源
            fileInputStream.close();
        }
        fileOutputStream.close();
    }
}

Properties

  • 可以和流相关联的集合对象Properties。Map的子类: Hashtable & Properties
  • Properties:该集合不需要泛型,因为该集合中的键值对都是String类型
    1. 存入键值对 : setProperty(key,value);
    2. 获取指定键对应的值 : value getProperty(key);
    3. 获取集合中所有键元素 : Enumeration propertyNames(); 在jdk1.6版本给该类提供一个新的方法 : Set<String> stringPropertyNames();
    4. 列出该集合中的所有键值对,可以通过参数打印流指定列出到的目的地。list(PrintStream); list(PrintWriter);
    • 例:
      list(System.out) : 将集合中的键值对打印到控制台
      list(new PrintStream("prop.txt")) : 将集合中的键值对存储到prop.txt文件中
    1. 可以将流中的规则数据加载进行集合,并称为键值对。load(InputStream): jdk1.6版本提供了新的方法 - load(Reader):
    • 注意:流中的数据要是"键=值" 的规则数据
    1. 可以将集合中的数据进行指定目的的存储。store(OutputStram,String comment) , jdk1.6版本提供了新的方法 - store(Writer ,String comment):
      使用该方法存储时,会带着当时存储的时间
  • 注意:Properties只加载key=value这样的键值对,与文件名无关,注释使用#
public static void sysPropList() throws IOException {
        Properties prop = System.getProperties();
        // prop.list(System.out);// 目的是控制台。
        // 需求是:将jvm的属性信息存储到一个文件中。
        prop.list(new PrintStream("java.txt"));
    }

    public static void sysProp() {
        Properties prop = System.getProperties();

        Set<String> keys = prop.stringPropertyNames();

        for (String key : keys) {
            System.out.println(key + ":" + prop.getProperty(key));
        }
    }
  • Properties类与配置文件
    • Properties类是一个Map集合,该集合中的键值对都是字符串。该集合通常用于对键值对形式的配置文件进行操作, 提供了可以快速操作配置文件的方法
    • 配置文件:将软件中可变的部分数据可以定义到一个文件中,方便以后更改,该文件称之为配置文件
    • 优势: Properties类提高代码的维护性
    • Properties类的基本方法
      • load() : 将文件设备数据装载为Map集合数据
      • get(key) : 获取Map中的数据
      • getProperty() : 获取Map中的数据特有方法
    • 案例 : 将配置文件中的数据通过流加载到集合中
  public static void loadFile() throws IOException {
        // 1,创建Properties(Map)对象
        Properties prop = new Properties();

        // 2.使用流加载配置文件。
        FileInputStream fis = new FileInputStream("c:\\qq.txt");

        // 3。使用Properties 对象的load方法将流中数据加载到集合中。
        prop.load(fis);

        // 遍历该集合
        Set<Entry<Object, Object>> entrySet = prop.entrySet();
        Iterator<Entry<Object, Object>> it = entrySet.iterator();
        while (it.hasNext()) {
            Entry<Object, Object> next = it.next();
            Object key = next.getKey();
            Object value = next.getValue();
        }
        // 通过键获取指定的值
        Object object = prop.get("jack");
        System.out.println(object);

        // 通过键修改值
        prop.setProperty("jack", "888888");

        // 将集合中的数据写入到配置文件中。
        FileOutputStream fos = new FileOutputStream("c:\\qq.txt");

        // 注释:
        prop.store(fos, "yes,qq");

        fos.close();
        fis.close();
    }
  • 综合示例: 获取记录程序运行次数:
public class Demo {
    public static void main(String[] args) throws IOException {
        int count = 0;
        Properties pro = new Properties();

        File file = new File("c:\\count.ini");
        FileInputStream fis = null;
        if (!file.exists()) {
            file.createNewFile();
        }
        fis = new FileInputStream(file);
        pro.load(fis);
        String str = pro.getProperty("count");
        if (str != null) {
            count = Integer.parseInt(str);
        }
        if (count == 3) {
            System.out.println("使用次数已到,请付费");
            System.exit(0);
        }

        count++;
        System.out.println("欢迎使用本软件" + "你已经使用了:" + count + " 次");

        pro.setProperty("count", count + "");
        FileOutputStream fos = new FileOutputStream(new File("c:\\count.ini"));
        pro.store(fos, "请保护知识产权");

        fis.close();
        fos.close();

    }
}

打印流 (PrintStream)

  • 打印流概述:
    • 打印流可以接受文件和其他字节输出流,所以打印流是对普通字节输出流的增强,其中定义了很多的重载的print()println(), 方便输出各种类型的数据. 打印流可以打印任意类型的数据,而且打印数据之前都会先把数据转换成字符串再进行打印
    • 示例:
class Animal{
    String name;
    String color;
    public Animal(String name,String color){
        this.name = name;
        this.color = color;
    }
    @Override
    public String toString() {
        return "名字:"+this.name+ " 颜色:"+ this.color;
    }   
}
public class Demo {
    public static void main(String[] args) throws IOException {
        /*FileOutputStream fileOutputStream = new FileOutputStream("F:\\a.txt");
        fileOutputStream.write("97".getBytes());
        fileOutputStream.close();*/
        
        //打印流可以打印任何类型的数据,而且打印数据之前都会先把数据转换成字符串再进行打印。
        File file = new  File("F:\\a.txt");
        //创建一个打印流
        PrintStream printStream = new PrintStream(file);
        /*
        printStream.println(97);
        printStream.println(3.14);
        printStream.println('a');
        printStream.println(true);
        Animal a = new Animal("老鼠", "黑色");
        printStream.println(a);
        
        //默认标准的输出流就是向控制台输出的,
        System.setOut(printStream); //重新设置了标准的输出流对象
        System.out.println("哈哈,猜猜我在哪里!!");
        */
        
        //收集异常的日志信息。
        File logFile = new File("F:\\2015年1月8日.log");
        PrintStream logPrintStream = new PrintStream( new FileOutputStream(logFile,true) );
        try{
            int c = 4/0;
            System.out.println("c="+c);
            int[] arr = null;
            System.out.println(arr.length);
            
        }catch(Exception e){
            e.printStackTrace(logPrintStream);
            
        }   
    }
}
  • PrintStream
    • 打印流(PrintStream) : 是一个字节打印流,System.out对应的类型就是PrintStream, 它的构造函数可以接收三种数据类型的值
      1. 字符串路径
      2. File对象
      3. OutputStream
public static void main(String[] args) throws IOException {
            PrintStream ps = System.out;

        // 普通write方法需要调用flush或者close方法才会在控制台显示
        // ps.write(100);
        // ps.close();

        // 不换行打印
            ps.print(100);
            ps.print('a');
            ps.print(100.5);
            ps.print("世界");
            ps.print(new Object());
            System.out.println("--------------");

        // 换行
            ps.println(100);
            ps.println('a');
            ps.println(100.5);
            ps.println("世界");
            ps.println(new Object());

        // 重定向打印流
            PrintStream ps2 = new PrintStream(new File("c:\\a.txt"));
            System.setOut(ps2);

        // 换行
            ps2.println(100);
            ps2.println('a');
            ps2.println(100.5);
            ps2.println("世界");
            ps2.println(new Object());

        // printf(); 格式化
            ps2.printf("%d,%f,%c,%s", 100, 3.14, '中', "世界你好!!!");
            ps2.printf("%4s和%8s 打价格战", "京东", "苏宁");     
          } 
}
  • 注意 : print 方法和write方法的却别在于, print提供自动刷新。普通的write方法需要调用flush或者close方法才可以看到数据, JDK1.5之后Java对PrintStream进行了扩展, 增加了格式化输出方式, 可以使用printf()重载方法直接格式化输出. 但是在格式化输出的时候需要指定输出的数据类型格式

  • PrintWriter

    • 是一个字符打印流, 构造函数可以接收四种类型的值
      1. 字符串路径。
      2. File对象。
      3. OutputStream
      4. Writer
    • 对于1/2类型的数据, 还可以指定编码表(也就是字符集); 对于3/4类型的数据, 可以指定自动刷新
    • 注意:该自动刷新值为true时,只有三个方法可以用:println,printf,format.
      如果想要既有自动刷新,又可执行编码。如何完成流对象的包装?
      PrintWrter pw = new PrintWriter(new OutputSteamWriter(new FileOutputStream("a.txt"),"utf-8"), true);
    • 如果想要提高效率。还要使用打印方法: PrintWrter pw = newPrintWriter(new BufferdWriter(new OutputSteamWriter( newFileOutputStream("a.txt"), "utf-8")), true);
public static void testPrintWriter() throws Exception {
        PrintWriter pw = new PrintWriter("c:/b.txt", "gbk");

        // pw.append("xxx");
        // pw.println(55);
        // pw.println('c');
        // pw.printf("%.1s与%4s打价格战, %c", "京东","苏宁", 'a');

        pw.close();
    }
  • Scanner
public static void testScanner() throws Exception {
        // Scanner scanner = new Scanner(new File("c:/test.txt"));
        Scanner scanner = new Scanner(System.in);

        System.out.println(scanner.nextInt());
        System.out.println(scanner.nextBoolean());

        scanner.close();
    }

操作数组的流对象

  • 操作字节数组: ByteArrayInputStream / ByteArrayOutputStream
    • 具体使用方法:
      • toByteArray();
      • toString();
      • writeTo(OutputStream);
public static void testByteArrayInputStream() throws Exception {
        InputStream in = new ByteArrayInputStream(new byte[] { 65, 66, 67 });
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        for (int b = -1; (b = in.read()) != -1;) {
            out.write(b);
        }

        in.close();
        out.close();

        System.out.println(Arrays.toString(out.toByteArray()));
        System.out.println(out);
    }
  • 操作字符数组 : CharArrayReader / CharArrayWriter
    • 对于这些流: 源是内存, 目的也是内存。而且这些流并未调用系统资源, 使用的就是内存中的数组, 所以在使用的时候无需close。操作数组的读取流在构造时,必须要明确一个数据源, 所以要传入相对应的数组。对于操作数组的写入流,在构造函数可以使用空参数。因为它内置了一个可变长度数组作为缓冲区
public static void testCharArrayReader() throws Exception {
        CharArrayReader reader = new CharArrayReader(new char[] { 'A', 'b', 'c' });
        CharArrayWriter writer = new CharArrayWriter();

        for (int b = -1; (b = reader.read()) != -1;) {
            writer.write(b);
        }

        reader.close();
        writer.close();

        System.out.println(writer.toCharArray());
    }
  • 这几个流的出现其实就是通过流的读写思想在操作数组。类似的对象同理:StringReader / StringWriter。
public static void testStringReader() throws Exception {
        StringReader reader = new StringReader("test 中国");
        StringWriter writer = new StringWriter();

        for (int b = -1; (b = reader.read()) != -1;) {
            writer.write(b);
        }

        reader.close();
        writer.close();

        System.out.println(writer.toString());
    }

操作基本数据类型的流对象

  • DataInputStream以及DataOutputStreamg概述 :

  • 查看API文档DataInputStream的信息 : 发现从底层输入流中读取基本 Java 数据类型。查看方法, 有读一个字节, 读一个char, 读一个double 的方法

  • DataInputStream从数据流读取字节,并将它们转换为正确的基本数据类型值或字符串。DataInputStream类继承FilterInputStream类并实现了DataInput接口

  • DataOutputStream将基本类型的值或字符串转换为字节,并且将字节输出到数据流。DataOutputStream类继承FilterOutputStream并实现了DataOutput接口

  • DataInputStream 的操作基本数据类型的方法:

    • int readInt() : 一次读取四个字节,并将其转成int值
    • boolean readBoolean() : 一次读取一个字节
    • short readShort()
    • long readLong()
    • 剩下的数据类型一样
    • String readUTF() : 按照utf-8修改版读取字符。注意,它只能读writeUTF()写入的字符数据
  • DataOutputStream 的操作基本数据类型的方法:

    • writeInt(int) : 一次写入四个字节。注意和write(int)不同。write(int)只将该整数的最低一个8位写入。剩余三个8位丢弃
    • writeBoolean(boolean)
    • writeShort(short)
    • writeLong(long)
    • 剩下是数据类型也也一样
    • writeUTF(String) : 按照utf-8修改版将字符数据进行存储。只能通过readUTF读取
  • 使用DataOutputStream写数据文件

public static void testDataInputStream() throws Exception {
        DataOutputStream out = new DataOutputStream(new FileOutputStream(
                "c:/a.txt"));

        out.writeBoolean(true);
        out.writeByte(15); // 0x05 1 个字节
        out.writeBytes("abc"); // 0x 0041 2个字节
        out.writeChar('X'); // ??
        out.writeChars("xyz");
        out.writeLong(111);
        out.writeUTF("中国");

        out.close();

        DataInputStream in = new DataInputStream(
                new FileInputStream("c:/a.txt"));
        System.out.println(in.readBoolean());
        System.out.println(in.readByte());
        
        System.out.println(in.readByte());
        System.out.println(in.readByte());
        System.out.println(in.readByte());
        
        System.out.println(in.readChar());
        
        System.out.println(in.readChar());
        System.out.println(in.readChar());
        System.out.println(in.readChar());
        
        System.out.println(in.readLong());
        
        System.out.println(in.readUTF());
        in.close();
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260

推荐阅读更多精彩内容

  • IO简单概述 IO解决问题 : 解决设备与设备之间的数据传输问题(硬盘 -> 内存 内存 -> 硬盘) 读和写文...
    奋斗的老王阅读 3,395评论 0 53
  • # 3.1 File # ## 3.1.1 File基本概念 ## 1.基本概念 -File类用于表示文件(目录)...
    闫子扬阅读 433评论 0 0
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,292评论 18 399
  • 一、流的概念和作用。 流是一种有顺序的,有起点和终点的字节集合,是对数据传输的总成或抽象。即数据在两设备之间的传输...
    布鲁斯不吐丝阅读 9,941评论 2 95
  • 我是这样看态度的 昨天下午,杨老师给我们讲了态度决定一切,那下面我就来说说态度吧! 态度决定一切。以欣...
    8b9542e8809c阅读 166评论 0 1