io流(三)

SequenceInputStream 整合流对象

package cn.itcast.otherio;

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
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        //demo1();
        //demo2();
        FileInputStream fis1 = new FileInputStream("a.txt");
        FileInputStream fis2 = new FileInputStream("b.txt");
        FileInputStream fis3 = new FileInputStream("c.txt");
        FileInputStream fis4 = new FileInputStream("d.txt");
        Vector<FileInputStream> v = new Vector<>();
        v.add(fis1);
        v.add(fis2);
        v.add(fis3);
        v.add(fis4);

        Enumeration<FileInputStream> en = v.elements();
        SequenceInputStream sis = new SequenceInputStream(en);
        FileOutputStream fos = new FileOutputStream("e.txt");

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

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

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

    public static void demo1() throws FileNotFoundException, IOException {
        FileInputStream fis1 = new FileInputStream("a.txt");
        FileOutputStream fos = new FileOutputStream("c.txt");

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

}

ByteArrayOutputStream

package cn.itcast.otherio;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo02_ByteArrayOutputStream {

    /**
     * @param args
     * 字节流在读中文会出现半个中文,乱码
     * 解决
     * 1,字符流
     * 2,ByteArrayOutputStream
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        //demo1();
        FileInputStream fis = new FileInputStream("e.txt");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        int b;
        while((b = fis.read()) != -1) {
            baos.write(b);                      //将字节写到内存中
        }

        //byte[] arr = baos.toByteArray();
        //System.out.println(new String(arr));
        //String str = baos.toString();         //将内存中的字节转换为字符串
        System.out.println(baos.toString());
        fis.close();
    }

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

        fis.close();
    }

}

ObjectOutputStream

package cn.itcast.otherio;

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

import cn.itcast.bean.Person;

public class Demo03_ObjectOutputStream {

    /**
     * @param args
     * @throws IOException 
     * 序列化,将对象写到文件上(存档)
     */
    public static void main(String[] args) throws IOException {
        //demo1();
        //demo2();
        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();
    }

    public static void demo2() 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();
    }

    public static void demo1() throws FileNotFoundException, IOException {
        Person p1 = new Person("张三", 23);
        FileOutputStream fos = new FileOutputStream("e.txt");
        //fos.write(p1);                                        //字节输出流不可以写出对象
        FileWriter fw = new FileWriter("e.txt");
        //fw.write(p1);                                         //字符输出流不可以写出对象
    }

}

ObjectInputStream

package cn.itcast.otherio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import cn.itcast.bean.Person;

public class Demo04_ObjectInputStream {

    /**
     * @param args
     * @throws IOException 
     * @throws ClassNotFoundException 
     * @throws FileNotFoundException 
     * 反序列化,将文件上的对象读出来(读档)
     * 如果写出两个对象,读三个对象会抛出EOFException
     * EOF end of File 文件到末尾异常
     */
    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //demo1();
        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();
    }

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

        System.out.println(p1);
        System.out.println(p2);
        //System.out.println(p3);
        ois.close();
    }

}

PrintStream

public static void main(String[] args) throws IOException {
        //demo1();
        PrintStream ps = new PrintStream("e.txt");
        ps.write(97);
        ps.println(97);

        ps.close();
    }

    public static void demo1() {
        Person p = new Person("张三", 23);
        PrintStream ps = System.out;
        ps.println(p);
        ps.println(97);                 //底层通过Integer.toString(97)将97转换成字符串
        ps.write(97);                   //不会将其转换成对应的字符串

        ps.close();
    }

PrintWriter

public static void main(String[] args) throws IOException {
        PrintWriter pw = new PrintWriter(new FileOutputStream("f.txt"), true);
        pw.write(97);
        pw.print(97);
        //pw.println(97);

        pw.close();
    }

改变标准输入流

package cn.itcast.otherio;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Demo07_SetInOut {

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

        InputStream is = System.in;                             //标准键盘输入流已经被改变,到a.txt读取文件
        PrintStream ps = System.out;                            //标准键盘输出流已经被改变,写到h.txt上
        int b;
        while((b = is.read()) != -1) {
            ps.write(b);
        }

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

    public static void demo2() {
        System.out.println(97);                                 //标准的输出流,默认指向控制
        System.out.close();                                     //不用关闭
        System.out.println(97);
    }

    public static void demo1() throws IOException {
        InputStream is = System.in;                         //标准的输入流,默认指向键盘
        int x = is.read();

        int y = is.read();
        System.out.println(x);
        System.out.println(y);
    }

}

RandomAccessFile

package cn.itcast.otherio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Demo09_RandomAccessFile {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile("h.txt", "rw");
        //int x = raf.read();
        //System.out.println(x);
        raf.seek(10);                                   //设置指针的位置为10
        raf.write(102);

        raf.close();
    }

}

Prop写入文件

package cn.itcast.otherio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Properties;

public class Demo12_Properties {

    /**
     * @param args
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws FileNotFoundException, IOException {
        //demo1();
        //demo2();
        Properties prop = new Properties();
        prop.load(new FileInputStream("config.properties"));        //获取文件上的数据
        prop.setProperty("username", "lisi");
        prop.list(new PrintStream("config.properties"));            //将数据写到文件上
        System.out.println(prop);
    }

    public static void demo2() {
        Properties prop = new Properties();
        prop.setProperty("username", "zhangsan");
        prop.setProperty("password", "123456");
        prop.setProperty("qq", "654321");
        prop.setProperty("tel", "18987654321");

        Enumeration<String> en = (Enumeration<String>) prop.propertyNames();
        while(en.hasMoreElements()) {
            String key = en.nextElement();
            String value = prop.getProperty(key);
            System.out.println(key + "=" + value);
        }
    }

    public static void demo1() {
        Properties prop = new Properties();
        prop.setProperty("username", "zhangsan");
        prop.setProperty("password", "123456");
        prop.setProperty("qq", "654321");
        prop.setProperty("tel", "18987654321");

        String value = prop.getProperty("username");
        System.out.println(value);
        System.out.println(prop);
    }

}

猜你喜欢

转载自blog.csdn.net/kebiaoy/article/details/80959727
今日推荐