38.其他几种流

打印流:
该打印流提供了打印方法,可以将各种数据类型的数据都原样打印。

字节打印流
PrintStream
构造函数可以接收的参数类型:
1.file对象 File
2.字符串路径 String
3.字节输出流 OutputStream

字符打印流
PrintWriter
构造函数可以接收的参数类型:
1.file对象 File
2.字符串路径 String
3.字节输出流 OutputStream
4.字符输出流 Writer

public class PrintWriter_08 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        //源数据——键盘
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader bufr=new BufferedReader(in);
        
        
        //目的地——控制台
        PrintWriter out=new PrintWriter(System.out,true); //true,自动刷新
        
        //PrintWriter out1=new PrintWriter("d:\\print.txt",true); //自动刷新仅针对流,不针对文件
        
        PrintWriter out2=new PrintWriter(new FileWriter("d:\\print.txt"),true); //可转化为流,再操作
        
        String line=null;
        while((line=bufr.readLine())!=null){
            if("over".equals(line)){
                break;
            }
            out2.println(line.toUpperCase());
            //out.flush();
        }

        out.close();
        bufr.close();
    }
}

 

序列流:将多个读取流合并成一个读取流

public class Sequence_09 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        Vector<FileInputStream> v=new Vector();
        
        v.add(new FileInputStream("d:\\sequence\\1.txt"));
        v.add(new FileInputStream("d:\\sequence\\2.txt"));
        v.add(new FileInputStream("d:\\sequence\\3.txt"));
        
        Enumeration<FileInputStream> en=v.elements();
        
        SequenceInputStream sis=new SequenceInputStream(en);
        
        FileOutputStream fos=new FileOutputStream("d:\\sequence\\cons.txt");
        byte[] buf=new byte[1024];
        int len=0;
        while((len=sis.read(buf))!=-1){
            fos.write(buf,0,len);
        }
        
        fos.close();
        sis.close();
    }

}

 

切割文件

public class SplitFile_11 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        //splitFile();
        
        merge();
    }

    public static void  splitFile() throws IOException{
        
        FileInputStream fis=new FileInputStream("d:\\copy.mp3");
        
        FileOutputStream fos=null;
        
        byte []  buf=new byte[1024*1024];
        
        int len=0;
        
        int count=1;
        
        while((len=fis.read(buf))!=-1){
            fos=new FileOutputStream("d:\\split\\"+(count++)+".part");
            fos.write(buf,0,len);
            fos.close();
        }
        
        fis.close();
    }
    
    public static void merge() throws IOException{
        
        Vector<FileInputStream> v=new Vector();
        
        for(int i=1;i<10;i++){
            v.add(new FileInputStream("d:\\split\\"+i+".part"));
        }
        
        Enumeration<FileInputStream> en=v.elements();
        
        SequenceInputStream sis=new SequenceInputStream(en);
        
        FileOutputStream fos=new FileOutputStream("d:\\split\\cons.mp3");
        
        byte [] buf=new byte[1024];
        
        int len=0;
        
        while((len=sis.read(buf))!=-1){
            fos.write(buf, 0, len);
        }
    }
}

 

ByteArrayInputStream:在构造的时候,需要接收数据源,而且数据源是一个字节数组。
ByteArrayOutputSteram:在构造的时候,不用定义数据目的,因为该对象中已经内部封装了可变长度的字节数组。这就是数据目的地。
因为这两个流对象都操作的数组,并没有使用系统资源。所以不用进行close关闭

在流操作规律讲解时,
源设备:键盘 :System.in
硬盘 : FileStream
内存: ArrayStream
目的设备:控制台:System.out
硬盘:FileStream
内存:ArrayStream

public class ByteArray_03 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //数据源
        ByteArrayInputStream bis=new ByteArrayInputStream("ABCDEFG".getBytes());
        
        //数据目的
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        
        int by=0;
        
        while((by=bis.read())!=-1){
            bos.write(by);
        }
        
        System.out.println(bos.size());
        System.out.println(bos.toString());

    }
}

 

可以用于操作基本数据类型

public class DataStreamDemo_02 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        //writeData();
        
        readData();
        
    }
    
    public static void readData() throws IOException{
        
        DataInputStream dis=new DataInputStream(new FileInputStream("d:\\data.txt"));
        
        int num=dis.readInt();
        boolean b=dis.readBoolean();
        double d=dis.readDouble();
        
        System.out.println(num+"..."+b+"..."+d);
        
        dis.close();
    }
    
    
    public static void writeData() throws IOException{
        
        DataOutputStream dos=new DataOutputStream(new FileOutputStream("d:\\data.txt"));
        
        dos.writeInt(234);
        dos.writeBoolean(true);
        dos.writeDouble(98.34);
        
        dos.close();
    }
}

 

对象流

public class ObjecrStreamDemo_01 {

    public static void main(String[] args) throws IOException, Exception {
        // TODO Auto-generated method stub

        //writeObj();
        
        readObj();
    }
    
    public static void writeObj() throws IOException{
        
        //将对象数据写到文件中(对象数据不是纯文本,字节,纯文本是能够看懂的东西)
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("d:\\obj.txt"));
        
        oos.writeObject(new Person("lisi0",399,"kr")); //kr无法打印,静态不能被序列化,静态在方法区,序列化的是堆里面的数据
        
        oos.close();
    }
    
    public static void readObj() throws IOException, ClassNotFoundException{
        
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("d:\\obj.txt"));
        
        Person p=(Person) ois.readObject();
        
        System.out.println(p);
        
        ois.close();
    }

}
class Person implements Serializable{
    
    static final long serialVersionUID = 42L; //固定标识,新的类还能操作曾经序列化的对象
    
    private String name;
    transient int age; //加了关键字,也不能被序列化
    static String country="cn";
    
    Person(String name,int age,String country){
        this.name=name;
        this.age=age;
        this.country=country;
    }

    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 + ","+country+"]";
    }
    
    
}

 

posted @ 2018-02-01 01:11  一日看尽长安花cxjj  阅读(109)  评论(0)    收藏  举报