javaday22-IO序列流

22.01_IO流(序列流)(了解)

  • 1.什么是序列流
    • 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推.
  • 2.使用方式

    • 整合两个: SequenceInputStream(InputStream, InputStream)

      FileInputStream fis1 = new FileInputStream("a.txt");            //创建输入流对象,关联a.txt
      FileInputStream fis2 = new FileInputStream("b.txt");            //创建输入流对象,关联b.txt
      SequenceInputStream sis = new SequenceInputStream(fis1, fis2);  //将两个流整合成一个流
      FileOutputStream fos = new FileOutputStream("c.txt");           //创建输出流对象,关联c.txt
      
      int b;
      while((b = sis.read()) != -1) {                                 //用整合后的读
          fos.write(b);                                               //写到指定文件上
      }
      
      sis.close();
      fos.close(); 
      

      案例:

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.SequenceInputStream;

import java.util.Enumeration;

import java.util.Vector;

public class Demo01_SequenceInputStream {

/**
 * @param args
 * 整合两个输入流
 * SequenceInputStream(InputStream s1, InputStream s2)
 * 整合多个输入流
 * SequenceInputStream(Enumeration<? extends InputStream> e)
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    //将a.txt文件(内容"大家好,")和b.txt文件(内容"才是真的好!")复制到c.txt文件中
    //demo1();
    demo2();        
}
public static void demo2() throws FileNotFoundException, IOException {
    FileInputStream fis1 = new FileInputStream("a.txt");
    FileInputStream fis2 = new FileInputStream("b.txt");
    SequenceInputStream sis = new SequenceInputStream(fis1, fis2);
    FileOutputStream fos = new FileOutputStream("c.txt");

    int b;
    while((b = sis.read()) != -1) {
        fos.write(b);
    }

    sis.close();                    //sis在关闭的时候,会将构造方法中传入的流对象也都关闭
    fos.close();
}
public static void demo1() throws FileNotFoundException, IOException {
    FileInputStream fis1 = new FileInputStream("a.txt");        //创建字节输入流关联a.txt
    FileOutputStream fos = new FileOutputStream("c.txt");       //创建字节输出流关联c.txt

    int b1;
    while((b1 = fis1.read()) != -1) {                           //不断的在a.txt上读取字节
        fos.write(b1);                                          //将读到的字节写到c.txt上
    }
    fis1.close();                                               //关闭字节输入流

    FileInputStream fis2 = new FileInputStream("b.txt");
    int b2;
    while((b2 = fis2.read()) != -1) {
        fos.write(b2);
    }

    fis2.close();
    fos.close();
}

}

22.02_IO流(序列流整合多个)(了解)

  • 整合多个: SequenceInputStream(Enumeration)
  • FileInputStream fis1 = new FileInputStream("a.txt");    //创建输入流对象,关联a.txt
    FileInputStream fis2 = new FileInputStream("b.txt");    //创建输入流对象,关联b.txt
    FileInputStream fis3 = new FileInputStream("c.txt");    //创建输入流对象,关联c.txt
    Vector<InputStream> v = new Vector<>();                 //创建vector集合对象
    v.add(fis1);                                            //将流对象添加
    v.add(fis2);
    v.add(fis3);
    Enumeration<InputStream> en = v.elements();             //获取枚举引用
    SequenceInputStream sis = new SequenceInputStream(en);  //传递给SequenceInputStream构造
    FileOutputStream fos = new FileOutputStream("d.txt");
    int b;
    while((b = sis.read()) != -1) {
        fos.write(b);
    }
    
    sis.close();
    fos.close();
    

    案例:

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.SequenceInputStream;

import java.util.Enumeration;

import java.util.Vector;

public class Demo01_SequenceInputStream {

/**
 * @param args
 * 整合两个输入流
 * SequenceInputStream(InputStream s1, InputStream s2)
 * 整合多个输入流
 * SequenceInputStream(Enumeration<? extends InputStream> e)
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    //将a.txt文件(内容"大家好,")和b.txt文件(内容"才是真的好!")复制到c.txt文件中 
    demo3();
}
public static void demo3() throws FileNotFoundException, IOException {
    FileInputStream fis1 = new FileInputStream("a.txt");
    FileInputStream fis2 = new FileInputStream("b.txt");
    FileInputStream fis3 = new FileInputStream("c.txt");

    Vector<FileInputStream> v = new Vector<>();                 //创建集合对象
    v.add(fis1);                                                //将流对象存储进来
    v.add(fis2);
    v.add(fis3);

    Enumeration<FileInputStream> en = v.elements();
    SequenceInputStream sis = new SequenceInputStream(en);      //将枚举中的输入流整合成一个
    FileOutputStream fos = new FileOutputStream("d.txt");

    int b;
    while((b = sis.read()) != -1) {
        fos.write(b);
    }

    sis.close();
    fos.close();
}

}

22.03_IO流(内存输出流*)(掌握)

  • 1.什么是内存输出流
    • 该输出流可以向内存中写数据, 把内存当作一个缓冲区, 写出之后可以一次性获取出所有数据
  • 2.使用方式

    • 创建对象: new ByteArrayOutputStream()
    • 写出数据: write(int), write(byte[])
    • 获取数据: toByteArray()
    • FileInputStream fis = new FileInputStream("a.txt");
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      int b;
      while((b = fis.read()) != -1) {
          baos.write(b);
      }
      
      //byte[] newArr = baos.toByteArray();               //将内存缓冲区中所有的字节存储在newArr中
      //System.out.println(new String(newArr));
      System.out.println(baos);
      fis.close();
      

      案例:

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

public class Demo02_ByteArrayOutputStream {

/**
 * @param args
 * ByteArrayOutputStream
 * 内存输出流
 * 
 * FileInputStream读取中文的时候出现了乱码
 * 
 * 解决方案
 * 1,字符流读取
 * 2,ByteArrayOutputStream
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    //demo1();
    demo2();
}
public static void demo2() throws FileNotFoundException, IOException {
    FileInputStream fis = new FileInputStream("e.txt");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();           //在内存中创建了可以增长的内存数组

    int b;
    while((b = fis.read()) != -1) {
        baos.write(b);                                                  //将读取到的数据逐个写到内存中
    }

    //byte[] arr = baos.toByteArray();                                  //将缓冲区的数据全部获取出来,并赋值给arr数组
    //System.out.println(new String(arr));

    System.out.println(baos.toString());                                //将缓冲区的内容转换为了字符串,在输出语句中可以省略调用toString方法
    fis.close();
}
public static void demo1() throws FileNotFoundException, IOException {
    FileInputStream fis = new FileInputStream("e.txt");
    byte[] arr = new byte[3];
    int len;
    while((len = fis.read(arr)) != -1) {
        System.out.println(new String(arr,0,len));
    }

    fis.close();
}

}

22.04_IO流(内存输出流之黑马面试题)(掌握)

  • 定义一个文件输入流,调用read(byte[] b)方法,将a.txt文件中的内容打印出来(byte数组大小限制为5)

  • FileInputStream fis = new FileInputStream(“a.txt”); //创建字节输入流,关联a.txt
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); //创建内存输出流
    byte[] arr = new byte[5]; //创建字节数组,大小为5
    int len;
    while((len = fis.read(arr)) != -1) { //将文件上的数据读到字节数组中
    baos.write(arr, 0, len); //将字节数组的数据写到内存缓冲区中
    }
    System.out.println(baos); //将内存缓冲区的内容转换为字符串打印
    fis.close();

案例:

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

public class Test1 {

/**
 * @param args
 * 定义一个文件输入流,调用read(byte[] b)方法,将a.txt文件中的内容打印出来(byte数组大小限制为5)
 * 
 * 分析:
 * 1,reda(byte[] b)是字节输入流的方法,创建FileInputStream,关联a.txt
 * 2,创建内存输出流,将读到的数据写到内存输出流中
 * 3,创建字节数组,长度为5,如果直接打印可能出现乱码
 * 4,将内存输出流的数据全部转换为字符串打印
 * 5,关闭输入流
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    //1,reda(byte[] b)是字节输入流的方法,创建FileInputStream,关联a.txt
    FileInputStream fis = new FileInputStream("a.txt");
    //2,创建内存输出流,将读到的数据写到内存输出流中
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //3,创建字节数组,长度为5
    byte[] arr = new byte[5];
    int len;

    while((len = fis.read(arr)) != -1) {
        baos.write(arr, 0, len);
        //System.out.println(new String(arr,0,len));
    }
    //4,将内存输出流的数据全部转换为字符串打印
    System.out.println(baos);                   //即使没有调用,底层也会默认帮我们调用toString()方法
    //5,关闭输入流
    fis.close();
}

}

22.05_IO流(随机访问流概述和读写数据)(了解)

  • A:随机访问流概述

    • RandomAccessFile概述
    • RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。
    • 支持对随机访问文件的读取和写入。
  • B:read(),write(),seek()
    案例:

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.RandomAccessFile;

public class Demo08_RandomAccessFile {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    RandomAccessFile raf = new RandomAccessFile("g.txt", "rw");
    //raf.write(97);
    //int x = raf.read();
    //System.out.println(x);
    raf.seek(0);                    //在指定位置设置指针
    raf.write(98);
    raf.close();
}

}

22.06_IO流(对象操作流ObjecOutputStream)(了解)

  • 1.什么是对象操作流
    • 该流可以将一个对象写出, 或者读取一个对象到程序中. 也就是执行了序列化和反序列化的操作.
      *序列化:将对象写出到文件
      *反序列化:将文件中的字节数据翻译成对象
  • 2.使用方式

    • 写出: new ObjectOutputStream(OutputStream), writeObject()

      public class Demo3_ObjectOutputStream {
      
          /**
           * @param args
           * @throws IOException 
           * 将对象写出,序列化
           */
          public static void main(String[] args) throws IOException {
              Person p1 = new Person("张三", 23);
              Person p2 = new Person("李四", 24);
      //      FileOutputStream fos = new FileOutputStream("e.txt");
      //      fos.write(p1);
      //      FileWriter fw = new FileWriter("e.txt");
      //      fw.write(p1);
              //无论是字节输出流,还是字符输出流都不能直接写出对象
              ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));//创建对象输出流
              oos.writeObject(p1);
              oos.writeObject(p2);
              oos.close();
          }
      
      }
      

      案例:
      实体类Person内容:

import java.io.Serializable;

public class Person implements Serializable {

private String name;
private int age;

public Person() {
    super();

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

}

测试类内容:

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import com.heima.bean.Person;

public class Demo03_ObjectOutputStream {

/**
 * @param args
 * 序列化:将对象写到文件上
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    demo1();        
}
public static void demo1() throws IOException, FileNotFoundException {
    Person p1 = new Person("张三", 23);
    Person p2 = new Person("李四", 24);

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));
    oos.writeObject(p1);
    oos.writeObject(p2);

    oos.close();
}

}

22.07_IO流(对象操作流ObjectInputStream)(了解)

  • 读取: new ObjectInputStream(InputStream), readObject()

    • public class Demo3_ObjectInputStream {
      
          /**
           * @param args
           * @throws IOException 
           * @throws ClassNotFoundException 
           * @throws FileNotFoundException 
           * 读取对象,反序列化
           */
          public static void main(String[] args) throws IOException, ClassNotFoundException {
              ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));
              Person p1 = (Person) ois.readObject();
              Person p2 = (Person) ois.readObject();
              System.out.println(p1);
              System.out.println(p2);
              ois.close();
          }
      
      }
      

      案例:

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.util.ArrayList;

import com.heima.bean.Person;

public class Demo04_ObjectInputStream {

/**
 * @param args
 * @throws IOException 
 * @throws FileNotFoundException 
 * @throws ClassNotFoundException 
 * ObjectInputStream
 * 对象输入流,反序列化
 */
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    demo1();
}
public static void demo1() throws IOException, FileNotFoundException,
        ClassNotFoundException {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));

    Person p1 = (Person) ois.readObject();
    Person p2 = (Person) ois.readObject();
    //Person p3 = (Person) ois.readObject();            //当文件读取到了末尾时出现EOFException

    System.out.println(p1);
    System.out.println(p2);

    ois.close();
}

}

22.08_IO流(对象操作流优化)(了解)

* 将对象存储在集合中写出

Person p1 = new Person("张三", 23);
Person p2 = new Person("李四", 24);
Person p3 = new Person("马哥", 18);
Person p4 = new Person("辉哥", 20);

ArrayList<Person> list = new ArrayList<>();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f.txt"));
oos.writeObject(list);                                  //写出集合对象

oos.close();

* 读取到的是一个集合对象

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f.txt"));
        ArrayList<Person> list = (ArrayList<Person>)ois.readObject();   //泛型在运行期会被擦除,索引运行期相当于没有泛型
    //想去掉黄色可以加注解            
    @SuppressWarnings("unchecked")
        for (Person person : list) {
            System.out.println(person);
        }

    ois.close();

案例:
将集合对象写到文件上

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import com.heima.bean.Person;

public class Demo03_ObjectOutputStream {

/**
 * @param args
 * 序列化:将对象写到文件上
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    Person p1 = new Person("张三", 23);
    Person p2 = new Person("李四", 24);
    Person p3 = new Person("王五", 25);
    Person p4 = new Person("赵六", 26);

    ArrayList<Person> list = new ArrayList<>();
    list.add(p1);
    list.add(p2);
    list.add(p3);
    list.add(p4);

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));
    oos.writeObject(list);                              //把整个集合对象一次写出
    oos.close();
}

}

从文件中读取出集合对象并遍历:

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.util.ArrayList;

import com.heima.bean.Person;

public class Demo04_ObjectInputStream {

/**
 * @param args
 * @throws IOException 
 * @throws FileNotFoundException 
 * @throws ClassNotFoundException 
 * ObjectInputStream
 * 对象输入流,反序列化
 */
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));
    ArrayList<Person> list = (ArrayList<Person>) ois.readObject();      //将集合对象一次读取

    for (Person person : list) {
        System.out.println(person);
    }

    ois.close();
}

}

22.09_IO流(加上id号)(了解)

  • 注意
    • 要写出的对象必须实现Serializable接口才能被序列化
    • 不用必须加id号
      案例:修改上面的Person类

package com.heima.bean;

import java.io.Serializable;

public class Person implements Serializable {

//首先让serialVersionUID=1L;执行写的操作,然后serialVersionUID改为2L,并添加String gender;属性,///不执行写的操作,直接执行读取的操作,观察报错信息
private static final long serialVersionUID = 1L;
private String name;
private int age;

public Person() {
    super();

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

}

22.10_IO流(数据输入输出流)(了解)

  • 1.什么是数据输入输出流
    • DataInputStream, DataOutputStream可以按照基本数据类型大小读写数据
    • 例如按Long大小写出一个数字, 写出时该数据占8字节. 读取的时候也可以按照Long类型读取, 一次读取8个字节.
  • 2.使用方式

    • DataOutputStream(OutputStream), writeInt(), writeLong()

      DataOutputStream dos = new DataOutputStream(new FileOutputStream("b.txt"));
      dos.writeInt(997);
      dos.writeInt(998);
      dos.writeInt(999);
      
      dos.close();
      
    • DataInputStream(InputStream), readInt(), readLong()

      DataInputStream dis = new DataInputStream(new FileInputStream("b.txt"));
      int x = dis.readInt();
      int y = dis.readInt();
      int z = dis.readInt();
      System.out.println(x);
      System.out.println(y);
      System.out.println(z);
      dis.close();
      

      案例:

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class Demo09_Data {

/**
 * @param args
 * @throws IOException 
 * 00000000 00000000 00000011 11100101  int类型997
 * 11100101:写的时候将前面的24位去掉,只写后8位
 * 00000000 00000000 00000000 11100101:读的时候,会在前面再加24个0
 */
public static void main(String[] args) throws IOException {
    //demo1();
    //demo2();
    //demo3();
    DataInputStream dis = new DataInputStream(new FileInputStream("h.txt"));
    int x = dis.readInt();
    int y = dis.readInt();
    int z = dis.readInt();

    System.out.println(x);
    System.out.println(y);
    System.out.println(z);

    dis.close();
}
public static void demo3() throws FileNotFoundException, IOException {
    DataOutputStream dos = new DataOutputStream(new FileOutputStream("h.txt"));
    dos.writeInt(997);
    dos.writeInt(998);
    dos.writeInt(999);
    dos.close();
}
public static void demo2() throws FileNotFoundException, IOException {
    FileInputStream fis = new FileInputStream("h.txt");
    int x = fis.read();
    int y = fis.read();
    int z = fis.read();

    System.out.println(x);
    System.out.println(y);
    System.out.println(z);

    fis.close();
}
public static void demo1() throws FileNotFoundException, IOException {
    FileOutputStream fos = new FileOutputStream("h.txt");
    fos.write(997);
    fos.write(998);
    fos.write(999);

    fos.close();
}

}

22.11_IO流(打印流的概述和特点)(掌握)

  • 1.什么是打印流

    • 该流可以很方便的将对象的toString()结果输出, 并且自动加上换行, 而且可以使用自动刷出的模式
    • System.out就是一个PrintStream, 其默认向控制台输出信息

      PrintStream ps = System.out;
      ps.println(97);                 //其实底层用的是Integer.toString(x),将x转换为数字字符串打印
      ps.println("xxx");
      ps.println(new Person("张三", 23));
      Person p = null;
      ps.println(p);                  //如果是null,就返回null,如果不是null,就调用对象的toString()
      
  • 2.使用方式

    • 打印: print(), println()
    • 自动刷出: PrintWriter(OutputStream out, boolean autoFlush, String encoding)
    • 打印流只操作数据目的

      PrintWriter pw = new PrintWriter(new FileOutputStream("g.txt"), true);
      pw.write(97);
      pw.print("大家好");
      pw.println("你好");               //自动刷出,只针对的是println方法
      pw.close();
      

      案例:

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.PrintStream;

import java.io.PrintWriter;

import com.heima.bean.Person;

public class Demo05_PrintStream {

/**
 * @param args
 * @throws IOException 
 * PrintStream和PrintWriter分别是打印的字节流和字符流
 * 只操作数据目的的
 */
public static void main(String[] args) throws IOException {
    //demo1();
    PrintWriter pw = new PrintWriter(new FileOutputStream("f.txt"),true);//true:表示自动刷新
    //pw.println(97);                           //自动刷出功能只针对的是println方法
    //pw.write(97);
    pw.print(97);
    pw.println(97);
    pw.close();
}
public static void demo1() {
    System.out.println("aaa");
    PrintStream ps = System.out;            //获取标注输出流
    ps.println(97);                         //底层通过Integer.toString()将97转换成字符串并打印
    ps.write(97);                           //查找码表,找到对应的a并打印

    Person p1 = new Person("张三", 23);
    ps.println(p1);                         //默认调用p1的toString方法

    Person p2 = null;                       //打印引用数据类型,如果是null,就打印null,如果不是null就打印对象的toString方法
    ps.println(p2);
    ps.close();
}

}

22.12_IO流(标准输入输出流概述和输出语句)

  • 1.什么是标准输入输出流(掌握)
    • System.in是InputStream, 标准输入流, 默认可以从键盘输入读取字节数据
    • System.out是PrintStream, 标准输出流, 默认可以向Console中输出字符和字节数据
  • 2.修改标准输入输出流(了解)

    • 修改输入流: System.setIn(InputStream)
    • 修改输出流: System.setOut(PrintStream)
    • System.setIn(new FileInputStream("a.txt"));             //修改标准输入流
      System.setOut(new PrintStream("b.txt"));                //修改标准输出流
      
      InputStream in = System.in;                             //获取标准输入流
      PrintStream ps = System.out;                            //获取标准输出流
      int b;
      while((b = in.read()) != -1) {                          //从a.txt上读取数据
          ps.write(b);                                        //将数据写到b.txt上
      }
      
      in.close();
      ps.close();
      

      案例:

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintStream;

public class Demo06_SystemInOut {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    //demo1();
    System.setIn(new FileInputStream("a.txt"));         //改变标准输入流
    System.setOut(new PrintStream("b.txt"));            //改变标注输出流

    InputStream is = System.in;                         //获取标准的键盘输入流,默认指向键盘,改变后指向文件
    PrintStream ps = System.out;                        //获取标准输出流,默认指向的是控制台,改变后就指向文件

    int b;
    while((b = is.read()) != -1) {
        ps.write(b);
    }
    //System.out.println();                             //也是一个输出流,不用关,因为没有和硬盘上的文件产生关联的管道
    is.close();
    ps.close();

}
public static void demo1() throws IOException {
    //当输入的48,40,49,时,打印后都是52
    //因为is.read();一次读取一个字节,而我们录入到键盘上的都认为是字符,所以读的是第一个字符4,然后将其转换为对应的int类型值为52
    InputStream is = System.in;
    int x = is.read();
    System.out.println(x);

    is.close();

    InputStream is2 = System.in;
    int y = is2.read();
    System.out.println(y);
}

}

22.13_IO流(修改标准输入输出流拷贝图片)(了解)

    System.setIn(new FileInputStream("IO图片.png"));      //改变标准输入流
    System.setOut(new PrintStream("copy.png"));         //改变标准输出流

    InputStream is = System.in;                         //获取标准输入流
    PrintStream ps = System.out;                        //获取标准输出流

    int len;
    byte[] arr = new byte[1024 * 8];

    while((len = is.read(arr)) != -1) {
        ps.write(arr, 0, len);
    }

    is.close();
    ps.close();

案例:

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintStream;

public class Test2 {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    System.setIn(new FileInputStream("双元.jpg"));            //改变标准输入流
    System.setOut(new PrintStream("copy.jpg"));             //改变标准输出流

    InputStream is = System.in;
    PrintStream ps = System.out;

    byte[] arr = new byte[1024];
    int len;

    while((len = is.read(arr)) != -1) {
        ps.write(arr, 0, len);
    }

    is.close();
    ps.close();
}

}

22.14_IO流(两种方式实现键盘录入)(了解)

  • A:BufferedReader的readLine方法。
    • BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  • B:Scanner
    案例:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Scanner;

public class Demo07_SystemIn {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    /*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));     //InputStreamReader转换流
    String line = br.readLine();
    System.out.println(line);
    br.close();*/

    Scanner sc = new Scanner(System.in);
    String line = sc.nextLine();
    System.out.println(line);
    sc.close();
}

}

22.15_IO流(Properties的概述和作为Map集合的使用)(了解)

  • A:Properties的概述
    • Properties 类表示了一个持久的属性集。
    • Properties 可保存在流中或从流中加载。
    • 属性列表中每个键及其对应值都是一个字符串。
  • B:案例演示
    • Properties作为Map集合的使用
      案例:

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Enumeration;

import java.util.Properties;

public class Demo10_Properties {

/**
 * @param args
 * Properties是Hashtable的子类
 * @throws IOException 
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws FileNotFoundException, IOException {
    demo1();
}
public static void demo1() {
    Properties prop = new Properties();
    prop.put("abc", 123);
    System.out.println(prop);
}

}

22.16_IO流(Properties的特殊功能使用)(了解)

  • A:Properties的特殊功能
    • public Object setProperty(String key,String value)
    • public String getProperty(String key)
    • public Enumeration stringPropertyNames()
  • B:案例演示
    • Properties的特殊功能
      案例:

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Enumeration;

import java.util.Properties;

public class Demo10_Properties {

/**
 * @param args
 * Properties是Hashtable的子类
 * @throws IOException 
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws FileNotFoundException, IOException {
    demo2();
}
public static void demo2() {
    Properties prop = new Properties();
    prop.setProperty("name", "张三");
    prop.setProperty("tel", "18912345678");

    //System.out.println(prop);
    Enumeration<String> en = (Enumeration<String>) prop.propertyNames();
    while(en.hasMoreElements()) {
        String key = en.nextElement();              //获取Properties中的每一个键
        String value = prop.getProperty(key);       //根据键获取值
        System.out.println(key + "="+ value);
    }
}

}

22.17_IO流(Properties的load()和store()功能)(了解)

  • A:Properties的load()和store()功能
  • B:案例演示
    • Properties的load()和store()功能
      案例:

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Enumeration;

import java.util.Properties;

public class Demo10_Properties {

/**
 * @param args
 * Properties是Hashtable的子类
 * @throws IOException 
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws FileNotFoundException, IOException {  
    Properties prop = new Properties();
    prop.load(new FileInputStream("config.properties"));        //将文件上的键值对读取到集合中
    prop.setProperty("tel", "18912345678");//修改tel属性的值,但是此时知识改的内存中的数据
    prop.store(new FileOutputStream("config.properties"), null);//第二个参数是对列表参数的描述,可以给值,也可以给null
    System.out.println(prop);
}   

}

22.18_设计模式(模版(Template)设计模式概述和使用)

  • A:模版设计模式概述
    • 模版方法模式就是定义一个算法的骨架,而将具体的算法延迟到子类中来实现
  • B:优点和缺点
    • a:优点
      • 使用模版方法模式,在定义算法骨架的同时,可以很灵活的实现具体的算法,满足用户灵活多变的需求
    • b:缺点
      • 如果算法骨架有修改的话,则需要修改抽象类
        1,装饰
        2,单例
        3,简单工厂
        4,工厂方法
        5,适配器
        6,模版
        案例:

package com.heima.templatedesign;

public class Demo1_Template {

/**
 * @param args
 */
public static void main(String[] args) {
    /*long start = System.currentTimeMillis();
    for(int i = 0; i < 1000000; i++) {
        System.out.println("x");
    }
    long end = System.currentTimeMillis();
    System.out.println(end - start);*/
    Demo d = new Demo();
    System.out.println(d.getTime());
}

}

abstract class GetTime {
public final long getTime() {
long start = System.currentTimeMillis();
code();
long end = System.currentTimeMillis();
return end - start;
}

public abstract void code();

}

class Demo extends GetTime {

@Override
public void code() {
    int i = 0;
    while(i < 100000) {
        System.out.println("x");
        i++;
    }
}

}

今天更新的内容掌握的比较少,学习的轻松了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值