装饰流

一、缓冲流:BufferedInputStream和BufferedOutputStream

1、字节缓冲流

直接套上BufferedInputStream、BufferedOutputStream即可

2、字符缓冲流:BufferedReader和BufferedWriter

为了应用新增方法  readLine读一行和newLine换行符,不能使用多态,

应使用BufferedReader br=new BufferedReader(new FileReader);

BufferedWriter br=new BufferedWriter(new FileWriter).

二、转换流:InputStreamReader和OutputStreamWriter

package com.sxt.io;

import java.io.*;

public class ConvertTest {
    public static void main(String[] args) {
        try(BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(System.out));){
            String  msg="";
            while(!msg.equals("exit")){
                msg=reader.readLine();
                writer.write(msg);
                writer.newLine();
                writer.flush();
            }
        }catch (IOException e){
            System.out.println("操作异常");
        }
    }
}

三、数据流:DataOutputStream和DataInputStream

package com.sxt.io;


import java.io.*;

public class DateTest {
    public static void main(String[] args) throws IOException {
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(baos));
        dos.writeInt(2);
        dos.writeChar('a');
        dos.writeUTF("编码辛酸泪");
        dos.flush();
        byte [] datas=baos.toByteArray();
        InputStream is=new ByteArrayInputStream(datas);
        DataInputStream dis=new DataInputStream(new BufferedInputStream(is));
        int a=dis.readInt();
        char b=dis.readChar();
        String c=dis.readUTF();
        System.out.println(a+"-->"+b+"-->"+c);

    }
}

问题总结:

1、先写出再读取

2、读取顺序必须与写出顺序相同

四、对象流:ObjectOutputStream

package com.sxt.io;
//加入BufferedOutputStream和new BufferedInputStream,记得flush
import java.io.*;
import java.util.Date;

public class ObjectText {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //写出-->序列化
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        ObjectOutputStream oos =new ObjectOutputStream(new BufferedOutputStream(baos));
        //操作数据类型+数据
        oos.writeUTF("编码辛酸泪");
        oos.writeBoolean(true);
        oos.writeChar('c');
        oos.writeInt(30);
        oos.writeObject("谁解其中味");
        oos.writeObject(new Date());
        Employee emp=new Employee("马云",400);
        oos.writeObject(emp);
        oos.flush();
        byte[] datas=baos.toByteArray();
        //读取-->反序列化
        ObjectInputStream ois =new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
        //顺序与写入一致
        String s= ois.readUTF();
        Boolean b= ois.readBoolean();
        char c= ois.readChar();
        int i= ois.readInt();
        //对象的数据还原
        Object str= ois.readObject();
        Object date= ois.readObject();
        Object obj= ois.readObject();
        if(str instanceof String){
            String strObj =(String)str;
            System.out.println(strObj);
        }
        if(date instanceof Date){
            Date dateObj =(Date)date;
            System.out.println(dateObj);
        }
        if(obj instanceof Employee){
            Employee empObj=(Employee)obj;
            System.out.println(empObj.getName()+"-->"+empObj.getSalary());
        }

    }
}
//自定义对象
class Employee implements  java.io.Serializable{
    private  transient String name;//该数据不需要序列化
    private double salary;
    public Employee(){

    }

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

要点总结:

1、先写出再读取

2、读取顺序必须与写出顺序相同

3、只有支持java.io.Serializable接口的对象才能写入流中

五、打印流:PrintStream和PrintWriter

package com.sxt.io;

import java.io.*;

public class PrintTest01 {
    public static void main(String[] args) throws FileNotFoundException {
        PrintStream ps=System.out;
        ps.println("aaaaa");
        ps=new PrintStream(new BufferedOutputStream(new FileOutputStream("print.txt")),true);
        ps.println("打印流");
        ps.println(true);


        //重定向输出端
        System.setOut(ps);
        System.out.println("change");
        //重定向回控制台
        System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)) ,true));
        System.out.println("back");

        //PrintWriter和PrintStream用法相同
        PrintWriter pw=new PrintWriter(new BufferedOutputStream(new FileOutputStream("print.txt")),true);;
        pw.println("abc");

    }

}

六、随机流:RandomAccessFile

package com.sxt.io;


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

public class RandTest01 {
    public static void main(String[] args) throws IOException {
        File src=new File("D:/IO_study/src/com/sxt/io/DecorateTest01.java");
        //总长度
        long len=src.length();
        //每块大小
        int blockSize=100;
        //多少块
        int size=(int) Math.ceil(len*1.0/blockSize);
        int beginPos=0;
        int actualSize=(int)(blockSize>len?len:blockSize);
        for(int i=0;i<size;i++){
            beginPos=i*blockSize;
            if(i==size-1){
                actualSize=(int)len;

            }else{
                actualSize=blockSize;
                len-=blockSize;

            }
            System.out.println(i+"-->"+beginPos+"-->"+actualSize);
            split( i, beginPos, actualSize);
        }


    }
    public  static void split(int i,int beginPos,int actualSize)throws IOException {
        RandomAccessFile raf=new RandomAccessFile("D:/IO_study/src/com/sxt/io/DecorateTest01.java","r");

        //实际大小

        //随机读取
        raf.seek(beginPos);

        //分段读出
        byte[] flush=new byte[1024];
        int len=-1;
        while((len=raf.read(flush))!=-1){

            if(actualSize>len){
                System.out.println(new String(flush,0,len));
                actualSize-=len;
            }else{

                System.out.println(new String(flush,0,actualSize));
                break;
            }
        }


    }
}


七、合并流:SequenceInputStream

package com.sxt.io;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class SplitFile{
    private File src;//起始文件
    private String dir;//文件夹
    private List<String> destPaths;//子文件
    private int blockSize;//分割的长度
    private int size;//分割的个数
    public SplitFile(String srcPath , String dest, int blockSize){
        this.src =new File(srcPath);
        this.dir = dest;
        this.blockSize =blockSize;
        this.destPaths =new ArrayList<String>();
        init();
    }
    //初始化
    private void init(){
        long len=this.src.length();//长度
        this.size=(int)Math.ceil(len*1.0/ blockSize);//块数
        //路径名称
        for(int i=0;i<size;i++){
            this.destPaths.add(this.dir +"/"+i+"-"+this.src.getName());
        }
    }

    //起始位置和实际大小
    // 分割
    public  void split() throws IOException {
    long len=this.src.length();
    int beginPos=0;
    int actualSize=(int)(len>blockSize?blockSize:len);
    for(int i=0;i<size;i++){
        beginPos=i*blockSize;
        if(i==size-1){
            actualSize=(int)len;
            splitDetail(i,beginPos,actualSize);
        }else{actualSize=blockSize;
                len-=actualSize;
                splitDetail(i,beginPos,actualSize);
        }

    }
    }

    private void splitDetail(int i,int beginPos,int actualSize) throws IOException {
        RandomAccessFile raf=new RandomAccessFile(this.src,"r");
        RandomAccessFile raf2=new RandomAccessFile(this.destPaths.get(i),"rw");
        raf.seek(beginPos);
        byte[] flush=new byte[1024];
        int len=-1;//接受长度
        while((len=raf.read(flush))!=-1){
            if(len<actualSize){
                raf2.write(flush,0,len);
                actualSize-=len;
            }else{
                raf2.write(flush,0,actualSize);
                break;
            }
        }
        raf2.close();
        raf.close();
    }
    public void emerge(String destPath) throws IOException {
        OutputStream os=new BufferedOutputStream(new FileOutputStream(destPath,true));
        Vector<InputStream> vi=new Vector<InputStream>();
        SequenceInputStream sis=null;
        for(int i=0;i<destPaths.size();i++){
            vi.add(new BufferedInputStream(new FileInputStream(destPaths.get(i))));}
        sis =new SequenceInputStream(vi.elements());
            //拷贝
            byte[] flush=new byte[1024];
            int len=-1;
            while((len=sis.read(flush))!=-1){
                os.write(flush,0,len);
            }
            os.flush();
            sis.close();
            os.close();
    }


    public static void main(String[] args) throws IOException {
        SplitFile sf=new SplitFile("d.txt" , "dest" , 1024 );
        sf.split();
        sf.emerge("bbb.java");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值