2019-06-01 Java学习日记 day22 io其他流

序列流

  序列流可以把多个字节输入流整合一个,从序列流中读取数据时,将从被整合的第一个流开始读,读完一个之后继续读第二个,以此类推

使用方法

  *整合两个:SequenceInputStream(InputStream,InputSream)

public class demo1_SequenceInputStream {

    public static void main(String[] args) throws IOException {
        //demo1();
        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(); //在庀的时候,会将构造方法重传入的流对象也都关闭
        fos.close();
    }

    public static void demo1() throws FileNotFoundException, IOException {
        FileInputStream fis1 =new FileInputStream("a.txt");//输入流
        
        FileOutputStream fos =new FileOutputStream("c.txt");//输出流
        int b;
        while((b =fis1.read()) != -1){ //不断的在a.txt上读取字节
            fos.write(b);            //将读到的字节写到c.txt上
        }
        fis1.close();            //关闭字节输入流
        FileInputStream fis2 =new FileInputStream("b.txt");
        int b2;
        while((b2 =fis2.read()) != -1){
            fos.write(b2);
        }
        fis2.close();
        fos.close();
    }

}
案例

整合多个输入流

        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();
        
案例

内存输出流

  该输出流可以向内存中写数据,把内存当做一个缓冲区,写出之后可以一次性获取所有数据

使用方式

  创建对象:new ByteArrayOutputStream()

  写出数据:write(int),write(byte [ ])

  获取数据:toByteArray()

public class demo2_ByteArrayOutputStream {

    public static void main(String[] args) throws IOException {
        //demo1();
        FileInputStream fis1=new FileInputStream("e.txt");
        ByteArrayOutputStream baos =new ByteArrayOutputStream();
        
        int b;
        while((b =fis1.read()) != -1){
            baos.write(b);
        }
        
        byte []arr =baos.toByteArray();
        System.out.println(new String(arr));
        System.out.println(baos.toString());
        fis1.close();

    }

    public static void demo1() throws FileNotFoundException, IOException {
        FileInputStream fis1=new FileInputStream("e.txt");
        byte [] arr =new byte[3];
        int len;
        while ((len =fis1.read(arr)) != -1) {
            System.out.println(new String(arr, 0, len));
            
        }
        fis1.close();
    }

}
案例
public class test1 {

    public static void main(String[] args) throws IOException {
        FileInputStream fis =new FileInputStream("a.txt");
        ByteArrayOutputStream baos =new ByteArrayOutputStream();
        //创建字节数组,长度为5
        byte [] arr =new byte[5];
        int len;
        while((len =fis.read(arr)) != -1){
            baos.write(arr,0,len);
        }
        //将内存输出流的数据全部转换为字符串打印
        System.out.println(baos);
        //关闭输入流
        fis.close();        
    }

}

随机访问流

  RandomAccessFile类不属于流,是object类的子类,但它融合了InputStream的功能

  支持岁随机访问的读取和写入

read() write() seek()

public class demo3_RandomAccessFile {

    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(1);  //在指定位置设置指针
        raf.write(98);
        raf.close();

    }

}

对象操作流 ObjectOutPutStream

  该流可以将一个对象写出,或者读取一个对象到程序中,也就是执行了序列化和反序列化的操作

使用方式

  写出:new  ObjectOutPutStream(OutputStream),writeObject ( )

对象操作流优化

1.存档,2读取,3class

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import tan.jung.bean.Person;

public class demo4_ObjectOutPutStream  {
    public static void main(String[] args) throws FileNotFoundException, IOException{
    //demo1();
        Person p1=new Person("张三",20);
        Person p2=new Person("李四",21);
        Person p3=new Person("王五",22);
        Person p4=new Person("赵六",24);
        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();
                
    }

    public static void demo1() throws IOException, FileNotFoundException {
        Person p1=new Person("张三",20);
        Person p2=new Person("李四",21);
        
        ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("e.txt"));
        oos.writeObject(p1);
        oos.writeObject(p2);
        
        oos.close();
    }    
}

//第二个包
import java.io.Serializable;

public class Person implements Serializable {
    private String name;
    private int age;
    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    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 + "]";
    }
    
}

//第三个包
public class demo5_objectInputStream {

    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
        //demo1();
        ObjectInputStream  oiss =new ObjectInputStream(new FileInputStream("f.txt"));
        
        ArrayList<Person> list =(ArrayList<Person>) oiss.readObject();
        for (Person person : list) {
            System.out.println(person);
        }
        oiss.close();
    }

    public static void demo1() throws IOException, FileNotFoundException, ClassNotFoundException {
        ObjectInputStream  oiss =new ObjectInputStream(new FileInputStream("e.txt"));
        
        Person p1=(Person) oiss.readObject();
        Person p2=(Person) oiss.readObject();
        System.out.println(p1);
        System.out.println(p2);
        
        oiss.close();
    }

}
练习题

加id号

数据输入输出流

  DataInputStream,DataOutputStream 可以按照基本数据类型大小读写数据

  例如按long大小写出一个数字,写出时该数据占8个字节,读取的时候也可以按照long尅性读取,一次读取8个字节

使用方式

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

  DataInputStream(InputStream),readInt(),readLong()

public class demo6_Data {

    public static void main(String[] args) throws IOException {
        //demo1();
        //demo2();
        
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("h.txt"));
        dos.writeInt(997);
        dos.writeInt(998);
        dos.writeInt(998);
        
        dos.close();
        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 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();
    }

}
案例

打印流

  该流可以很方便的将对象的toString()结果输出,并且自动加上换行,而且可以使用自动刷出的模式

  System.out就是一个printStream,其默认向控制台输出信息

使用方式

  打印:print(),printIn()

  自动刷出:printWriter(OutputStream out,boolean autoFlush,String encoding)

  打印流只操作数据目的   

      

标准输入流输出流

  System.in 是InputStream,标准输入流,默认括从键盘输入读取字节数据

  System.out 是PrintStream,标准输出流,默认可以向Console中输出字符和字节数据

修改标准输入输出流

  修改输入流:System.setIn(InputStream)

  修改输出流:System.setOut(PrintStream)

public class demo9_Systeminout {

    public static void main(String[] args) throws IOException {
        //demo1();
        //demo2();
        System.setIn(new FileInputStream("dog.jpg"));
        System.setOut(new PrintStream("copy.jpg"));
        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();

    }

    public static void demo2() throws FileNotFoundException, IOException {
        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);
        }
        is.close();
        ps.close();
    }

    public static void demo1() throws IOException {
        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);
    }

}
练习题

两种方式实现键盘录入

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class test2 {
    public static void main (String args []) throws IOException{
        BufferedReader br =new BufferedReader(new 
                 InputStreamReader(System.in));
        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();*/
    }

}

Properties

  Properties 类表示了一个持久的属性集

  Properties 可保存在流中或从流中加载

  属性列表中每个键及其对应值都是一个字符串

特殊功能

  public object setProperties(String key,String value)

  public String getProperties  (String key)

  public Enumeration<String>  StringPropertiesNames ( )

Properties prop =new Properties();
        prop.setProperty("name", "某某");
        prop.setProperty("age", "20");
        //System.out.println(prop);
        Enumeration<String> e =(Enumeration<String>)prop.propertyNames();
        while (e.hasMoreElements()) {
            String key=e.nextElement();//获取Properties中的每一个键
            String value=prop.getProperty(key);
            System.out.println(key+"="+value);

 Properties 的 load() 和store() 功能

public class test3 {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        Properties prop =new Properties();
        //System.out.println("读取前"+prop);
        prop.load(new FileInputStream("cofing.properties"));
        prop.setProperty("username", "今天天气真好");
        
        prop.store(new FileOutputStream("cofing.properties"), null);//第二个参数是相当于加注释
        System.out.println("读取后"+prop);
    }

}

  

原文地址:https://www.cnblogs.com/JungTan0113/p/10961637.html