码迷,mamicode.com
首页 > 其他好文 > 详细

IO(二)

时间:2014-10-04 02:43:35      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   使用   ar   java   

package com.bjsxt.io.buffered;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * 字节流文件拷贝+缓冲流 ,提高性能
 * 缓冲流(节点流)
 * @author Administrator
 *
 */
public class BufferedByteDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        
    }
    /**
     * 文件的拷贝
     * @param  源文件路径
     * @param  目录文件路径
     * @throws FileNotFoundException,IOException
     * @return 
     */
        public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
            //1、建立联系 源(存在且为文件) +目的地(文件可以不存在)  
            File src =new File(srcPath);
            File dest =new File(destPath);
            if(! src.isFile()){ //不是文件或者为null
                System.out.println("只能拷贝文件");
                throw new IOException("只能拷贝文件");
            }
            //2、选择流
            InputStream is =new BufferedInputStream(new FileInputStream(src));
            OutputStream os =new BufferedOutputStream( new FileOutputStream(dest));
            //3、文件拷贝   循环+读取+写出
            byte[] flush =new byte[1024];
            int len =0;
            //读取
            while(-1!=(len=is.read(flush))){
                //写出
                os.write(flush, 0, len);
            }
            os.flush(); //强制刷出
            
            //关闭流
            os.close();
            is.close();
        }

}

 

 

package com.bjsxt.io.buffered;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
 * 字符缓冲流 +新增方法(不能发生多态)
 * @author Administrator
 *
 */
public class BufferedCharDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //创建源 仅限于 字符的纯文本
        File src =new File("E:/xp/test/Demo03.java");
        File dest =new File("e:/xp/test/char.txt");
        //选择流
        BufferedReader reader =null;        
        BufferedWriter wr =null;
        try {
            reader =new BufferedReader(new FileReader(src));
            wr =new BufferedWriter(new FileWriter(dest));
            //读取操作
            /*
            char[] flush =new char[1024];
            int len =0;
            while(-1!=(len=reader.read(flush))){
                wr.write(flush, 0, len);
            }*/
            //新增方法的操作
            String line =null;
            while(null!=(line=reader.readLine())){
                wr.write(line);
            //wr.append("\r\n");
                wr.newLine(); //换行符号
            }
            wr.flush();//强制刷出
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("源文件不存在");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件读取失败");
        }finally{
            try {
                if (null != wr) {
                    wr.close();
                }
            } catch (Exception e2) {
            }
            try {
                if (null != reader) {
                    reader.close();
                }
            } catch (Exception e2) {
            }
        }
    }

}
package com.bjsxt.io.buffered;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
 * 字符缓冲流 +新增方法(不能发生多态)
 * @author Administrator
 *
 */
public class BufferedCharDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //创建源 仅限于 字符的纯文本
        File src =new File("E:/xp/test/Demo03.java");
        File dest =new File("e:/xp/test/char.txt");
        //选择流
        BufferedReader reader =null;        
        BufferedWriter wr =null;
        try {
            reader =new BufferedReader(new FileReader(src));
            wr =new BufferedWriter(new FileWriter(dest));
            //读取操作
            /*
            char[] flush =new char[1024];
            int len =0;
            while(-1!=(len=reader.read(flush))){
                wr.write(flush, 0, len);
            }*/
            //新增方法的操作
            String line =null;
            while(null!=(line=reader.readLine())){
                wr.write(line);
            //wr.append("\r\n");
                wr.newLine(); //换行符号
            }
            wr.flush();//强制刷出
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("源文件不存在");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件读取失败");
        }finally{
            try {
                if (null != wr) {
                    wr.close();
                }
            } catch (Exception e2) {
            }
            try {
                if (null != reader) {
                    reader.close();
                }
            } catch (Exception e2) {
            }
        }
    }

}

编码:

package com.bjsxt.io.convert;

import java.io.UnsupportedEncodingException;

public class ConverDemo01 {

    /**
     * @param args
     * @throws UnsupportedEncodingException 
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str ="中国";
        byte[] data =str.getBytes();
        //字节数不完整
        System.out.println(new String(data,0,3));
        
        test1();
        
    }
    /**
     * 编码与解码字符集必须相同,否则乱码
     * @throws UnsupportedEncodingException 
     */
    public static void test1() throws UnsupportedEncodingException{
        //解码 byte -->char
        String str ="中国"; //gbk 
        //编码 char -->byte
        byte[] data =str.getBytes();
        //编码与解码字符集同一
        System.out.println(new String(data));
        data =str.getBytes("utf-8"); //设定编码字符集
        //不同一出现乱码
        System.out.println(new String(data));
        
        //编码
        byte[] data2 = "中国".getBytes("utf-8");
        //解码
        str=new String(data2,"utf-8");
        System.out.println(str);
    }

}
package com.bjsxt.io.convert;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * 转换流: 字节转为字符
 * 1、输出流 OutputStreamWriter 编码
 * 2、输入流 InputStreamReader  解码
 * 
 * 确保源不能为乱码
 * @author Administrator
 *
 */
public class ConverDemo02 {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        //指定解码字符集
        BufferedReader br =new BufferedReader(
                new InputStreamReader(
                    new BufferedInputStream(
                            new FileInputStream( 
                                    new File("E:/xp/test/Demo03.java"))),"UTF-8")
                );
        //写出文件 编码
        BufferedWriter bw =new BufferedWriter(
                new OutputStreamWriter(
                    new BufferedOutputStream(    
                    new FileOutputStream(new File("E:/xp/test/encode.java")))));
                
        String info =null;
        while(null!=(info=br.readLine())){
            //System.out.println(info);
            bw.write(info);
            bw.newLine();
        }
        bw.flush();
        bw.close();
        br.close();
    }

}

其它流:

package com.bjsxt.io.others;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 字节数组 节点流
 * 数组的长度有限 ,数据量不会很大
 * 
 * 文件内容不用太大
 * 1、文件  --程序->字节数组
 * 2、字节数组  --程序->文件
 * @author Administrator
 *
 */
public class ByteArrayDemo01 {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        read(write());        
    }
    /**
     * 输出流  操作与文件输出流 有些不同, 有新增方法,不能使用多态
     * @throws IOException 
     */
    public static byte[] write() throws IOException{
        //目的地
        byte[] dest;
        //选择流   不同点
        ByteArrayOutputStream bos =new ByteArrayOutputStream();
        //操作 写出
        String msg ="操作与 文件输入流操作一致";
        byte[] info =msg.getBytes();
        bos.write(info, 0, info.length);
        //获取数据
        dest =bos.toByteArray();
        //释放资源
        bos.close();
        return dest;        
    }
    /**
     * 输入流  操作与 文件输入流操作一致
     * 读取字节数组
     * @throws IOException 
     */
    public static void read(byte[] src) throws IOException{
        //数据源传入        
        
        //选择流
        InputStream is =new BufferedInputStream(
                    new ByteArrayInputStream(
                            src
                        )
                );
        //操作
        byte[] flush =new byte[1024];
        int len =0;
        while(-1!=(len=is.read(flush))){
            System.out.println(new String(flush,0,len));
        }
        //释放资源
        is.close();
    }

}

 

 

package com.bjsxt.io.others;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *1、文件  --程序->字节数组
 *1)、文件输入流     
 *        字节数组输出流
 *
 *
 * 2、字节数组  --程序->文件
 * 1)、字节数组输入流
 *         文件输出流
 * @author Administrator
 *
 */
public class ByteArrayDemo02 {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        byte[] data =getBytesFromFile("e:/xp/test/1.jpg");
        toFileFromByteArray(data,"e:/xp/test/arr.jpg");
    }
    /**
     * 2、字节数组  --程序->文件
     */
    public static void toFileFromByteArray(byte[] src,String destPath) throws IOException{
        //创建源
        //目的地
        File dest=new File(destPath);
        
        //选择流
        //字节数组输入流
        InputStream is =new BufferedInputStream(new ByteArrayInputStream(src));        
        //文件输出流
        OutputStream os =new BufferedOutputStream(new FileOutputStream(dest));
        
        //操作 不断读取字节数组
        byte[] flush =new byte[1];
        int len =0;
        while(-1!=(len =is.read(flush))){
            //写出到文件中
            os.write(flush, 0, len);
        }
        os.flush();
        
        //释放资源
        os.close();
        is.close();
        
        
    }
    
    
    
    /**
     * 1、文件  --程序->字节数组
     * @return
     * @throws IOException 
     */
    public static byte[] getBytesFromFile(String srcPath) throws IOException{
        //创建文件源
        File src =new File(srcPath);
        //创建字节数组目的地 
        byte[] dest =null;
        
        //选择流
        //文件输入流     
        InputStream is =new BufferedInputStream(new FileInputStream(src));
        //字节数组输出流 不能使用多态
        ByteArrayOutputStream bos =new ByteArrayOutputStream();
        
        
        //操作   不断读取文件 写出到字节数组流中
        byte[] flush =new byte[1024];
        int len =0;
        while(-1!=(len =is.read(flush))){
            //写出到字节数组流中
            bos.write(flush, 0, len);
        }
        bos.flush();
        
        //获取数据
        dest =bos.toByteArray();
        
        bos.close();
        is.close();        
        return dest;
    }

}

 

 

package com.bjsxt.io.others;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 数据类型(基本+String)处理流
 * 1、输入流 DataInputStream  readXxx()
 * 2、输出流 DataOutputStream writeXxx()
 * 新增方法不能使用多态
 * 
 * java.io.EOFException :没有读取到相关的内容
 * @author Administrator
 *
 */
public class DataDemo01 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            //write("e:/xp/test/data.txt");
            //read("e:/xp/test/arr.txt"); //非法内容
            read("e:/xp/test/data.txt");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
    /**
     * 从文件读取数据+类型
     * @throws IOException 
     */
    public static void read(String destPath) throws IOException{
        //创建源
        File src =new File(destPath);
        //选择流
        DataInputStream dis =new DataInputStream(
                    new BufferedInputStream(
                                new FileInputStream(src)
                            )
                );
        
        //操作 读取的顺序与写出一致   必须存在才能读取
        //不一致,数据存在问题
        long num2 =dis.readLong();
        double num1 =dis.readDouble();
        String str =dis.readUTF();
        
        dis.close();
        System.out.println(num2+"-->"+str);
        
    }
    /**
     * 数据+类型输出到文件
     * @throws IOException 
     */
    public static void write(String destPath) throws IOException{
        double point =2.5;
        long num=100L;
        String str ="数据类型";
        
        //创建源
        File dest =new File(destPath);
        //选择流  DataOutputStream
        DataOutputStream dos =new DataOutputStream(
                    new BufferedOutputStream(
                                new FileOutputStream(dest)
                            )
                );
        //操作 写出的顺序 为读取准备
        dos.writeDouble(point);
        dos.writeLong(num);
        dos.writeUTF(str);        
        dos.flush();
        
        //释放资源
        dos.close();       
    }

}

 

package com.bjsxt.io.others;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * 数据类型(基本+String)处理流
 * 1、输入流 DataInputStream  readXxx()
 * 2、输出流 DataOutputStream writeXxx()
 * 新增方法不能使用多态
 * java.io.EOFException :没有读取到相关的内容
 * @author Administrator
 *
 */
public class DataDemo02 {
    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            byte[] data=write();
            read(data);
            System.out.println(data.length);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 从字节数组读取数据+类型
     * @throws IOException 
     */
    public static void read(byte[] src) throws IOException{
        //选择流
        DataInputStream dis =new DataInputStream(
                    new BufferedInputStream(
                                new ByteArrayInputStream(src)
                            )
                );
        //操作 读取的顺序与写出一致   必须存在才能读取
        double num1 =dis.readDouble();
        long num2 =dis.readLong();
        String str =dis.readUTF();
        dis.close();
        System.out.println(num1+"-->"+num2+"-->"+str);
    }
    /**
     * 数据+类型输出到字节数组中
     * @throws IOException 
     */
    public static byte[] write() throws IOException{
        //目标数组
        byte[] dest =null;
        double point =2.5;
        long num=100L;
        String str ="数据类型";
        //选择流 ByteArrayOutputStream  DataOutputStream
        ByteArrayOutputStream bos =new ByteArrayOutputStream();
        DataOutputStream dos =new DataOutputStream(
                    new BufferedOutputStream(
                            bos
                            )
                );
        //操作 写出的顺序 为读取准备
        dos.writeDouble(point);
        dos.writeLong(num);
        dos.writeUTF(str);        
        dos.flush();
        dest =bos.toByteArray();
        //释放资源
        dos.close();
        return dest;
    }

}

 

package com.bjsxt.io.others;
/**
 * 空接口只是标识
 * @author Administrator
 *
 */
public class Employee implements java.io.Serializable {
    //不需要序列化
    private transient String name;
    private double salary;
    public Employee() {
    }
    public Employee(String name, double salary) {
        super();
        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;
    }
    
    
}
package com.bjsxt.io.others;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
/**
 * 不是所有的對象都可以序列化  java.io.NotSerializableException
 * 不是所有的屬性都需要序列化  transient
 * @author Administrator
 *
 */
public class ObjectDemo01 {

    /**
     * @param args
     * @throws ClassNotFoundException 
     */
    public static void main(String[] args) throws ClassNotFoundException {
        try {
            seri("e:/xp/test/ser.txt");
            read("e:/xp/test/ser.txt");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //反序列化
    public static void read(String destPath) throws IOException, ClassNotFoundException{
        //创建源
        File src =new File(destPath);
        //选择流
        ObjectInputStream dis =new ObjectInputStream(
                    new BufferedInputStream(
                                new FileInputStream(src)
                            )
                );
        
        //操作 读取的顺序与写出一致   必须存在才能读取
        //不一致,数据存在问题
        Object obj =dis.readObject();
        if(obj instanceof Employee){
            Employee emp=(Employee)obj;
            System.out.println(emp.getName());
            System.out.println(emp.getSalary());
        }
        
        obj =dis.readObject();
        int[] arr=(int[])obj;
        System.out.println(Arrays.toString(arr));
        dis.close();
    }
    
    //序列化
    public static void seri(String destPath) throws IOException{
        Employee emp =new Employee("bjsxt",1000000);
        int[] arr ={1,2,3,45};
        //创建源
        File dest =new File(destPath);
        //选择流  ObjectOutputStream
        ObjectOutputStream dos =new ObjectOutputStream(
                    new BufferedOutputStream(
                                new FileOutputStream(dest)
                            )
                );
        //操作 写出的顺序 为读取准备
        dos.writeObject(emp);
        dos.writeObject(arr);
        //释放资源
        dos.close();
    }
}
package com.bjsxt.io.util;

import java.io.Closeable;

public class FileUtil {

    /**
     * 工具类关闭流
     * 可变参数: ...  只能形参最后一个位置,处理方式与数组一致
     */
    public static void close(Closeable ... io){
        for(Closeable temp:io){
            try {
                if (null != temp) {
                    temp.close();
                }
            } catch (Exception e) {
            }
        }
        
    }
    
    /**
     * 使用泛型方法
     */
    public static <T extends Closeable> void closeAll(T ... io){
        for(Closeable temp:io){
            try {
                if (null != temp) {
                    temp.close();
                }
            } catch (Exception e) {
            }
        }
    }

}

 

 

package com.bjsxt.io.others;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

/**
 * PrintStream 打印流 -->处理流
 * @author Administrator
 *
 */
public class PrintStreamDemo01 {

    /**
     * @param args
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {
        System.out.println("test");
        PrintStream ps =System.out;
        ps.println(false);
        //输出到文件
        File src = new File("e:/xp/test/print.txt");
        ps = new PrintStream(new BufferedOutputStream(new FileOutputStream(src)));
        ps.println("io is so easy....");
        ps.close();
    }

}
package com.bjsxt.io.others;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 封装输入
 * @author Administrator
 *
 */
public class BuffereIn {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        InputStream is =System.in;
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        System.out.println("请输入。。。。");
        String msg =br.readLine();
        System.out.println(msg);
    }

}

 

package com.bjsxt.io.others;

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

import com.bjsxt.io.util.FileUtil;
/**
 * 文件的分割思路
 * 1、分割的块数 size   n块
 * 2、每一块的大小 blockSize
 *   最后:总的文件大小 -(n-1)*blockSize
 * 
 * @author Administrator
 *
 */
public class RndDemo01 {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        RandomAccessFile rnd =new RandomAccessFile(new File("E:/xp/20130502/test/test.java"),"r");
        rnd.seek(40);
        //定义缓冲大小
        byte[] flush =new byte[1024];
        //接收长度
        int len =0;         
        
        while(-1!=(len=rnd.read(flush))){
            if(len>=20){
                System.out.println(new String(flush,0,20));
                break;
            }else{
                System.out.println(new String(flush,0,len));
            }
            
        }
        FileUtil.close(rnd);
    }

}
package com.bjsxt.io.others;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.bjsxt.io.util.FileUtil;
public class SplitFile {
    //文件的路径
    private String filePath;
    //文件名
    private String fileName;
    //文件大小
    private long length;
    //块数
    private int size;
    //每块的大小
    private long blockSize;
    //分割后的存放目录
    private String destBlockPath;
    //每块的名称
    private List<String> blockPath;
    
    public SplitFile(){
        blockPath = new ArrayList<String>();
    }
    public SplitFile(String filePath,String destBlockPath){
        this(filePath,destBlockPath,1024);        
    }
    public SplitFile(String filePath,String destBlockPath,long blockSize){
        this();
        this.filePath= filePath;
        this.destBlockPath =destBlockPath;
        this.blockSize=blockSize;
        init();
    }
    
    /**
     * 初始化操作 计算 块数、确定文件名
     */
    public void init(){
        File src =null;
        //健壮性
        if(null==filePath ||!(((src=new File(filePath)).exists()))){
            return;
        }
        if(src.isDirectory()){
            return ;
        }
        //文件名
        this.fileName =src.getName();
        
        //计算块数 实际大小 与每块大小
        this.length = src.length();
        //修正 每块大小
        if(this.blockSize>length){
            this.blockSize =length;
        }
        //确定块数        
        size= (int)(Math.ceil(length*1.0/this.blockSize));
        //确定文件的路径
        initPathName();
    }
    
    private void initPathName(){
        for(int i=0;i<size;i++){
            this.blockPath.add(destBlockPath+"/"+this.fileName+".part"+i);
        }
    }
    
    /**
     * 文件的分割
     * 0)、第几块
     * 1、起始位置
     * 2、实际大小
     * @param destPath 分割文件存放目录
     */
    public void split(){    
        long beginPos =0;  //起始点
        long actualBlockSize =blockSize; //实际大小        
        //计算所有块的大小、位置、索引
        for(int i=0;i<size;i++){
            if(i==size-1){ //最后一块
                actualBlockSize =this.length-beginPos;
            }            
            spiltDetail(i,beginPos,actualBlockSize);
            beginPos+=actualBlockSize; //本次的终点,下一次的起点
        }
        
    }
    /**
     * 文件的分割 输入 输出
     * 文件拷贝
     * @param idx 第几块
     * @param beginPos 起始点
     * @param actualBlockSize 实际大小
     */
    private void spiltDetail(int idx,long beginPos,long actualBlockSize){
        //1、创建源
        File src = new File(this.filePath);  //源文件
        File dest = new File(this.blockPath.get(idx)); //目标文件
        //2、选择流
        RandomAccessFile raf = null;  //输入流
        BufferedOutputStream bos=null; //输出流
        try {
            raf=new RandomAccessFile(src,"r");
            bos =new BufferedOutputStream(new FileOutputStream(dest));
            
            //读取文件
            raf.seek(beginPos);
            //缓冲区
            byte[] flush = new byte[1024];
            //接收长度
            int len =0;
            while(-1!=(len=raf.read(flush))){                
                if(actualBlockSize-len>=0){ //查看是否足够
                    //写出
                    bos.write(flush, 0, len);
                    actualBlockSize-=len; //剩余量
                }else{ //写出最后一次的剩余量
                    bos.write(flush, 0, (int)actualBlockSize);
                    break;
                }
            }
            
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            FileUtil.close(bos,raf);
        }
        
    }
    /**
     * 文件的合并
     */
    public void merge(String destPath){
        //创建源
        File dest =new File(destPath);
        //选择流
        BufferedOutputStream bos=null; //输出流
        SequenceInputStream sis =null ;//输入流
        //创建一个容器
        Vector<InputStream> vi = new Vector<InputStream>();        
        try {
            for (int i = 0; i < this.blockPath.size(); i++) {
                vi.add(new BufferedInputStream(new FileInputStream(new File(this.blockPath.get(i)))));
            }    
            bos =new BufferedOutputStream(new FileOutputStream(dest,true)); //追加
            sis=new SequenceInputStream(vi.elements());            
                
            //缓冲区
            byte[] flush = new byte[1024];
            //接收长度
            int len =0;
            while(-1!=(len=sis.read(flush))){                        
                bos.write(flush, 0, len);
            }
            bos.flush();
            FileUtil.close(sis);
        } catch (Exception e) {
        }finally{
            FileUtil.close(bos);
        }        
        
    }
    /**
     * 文件的合并
     */
    public void merge1(String destPath){
        //创建源
        File dest =new File(destPath);
        //选择流
        BufferedOutputStream bos=null; //输出流
        try {
            bos =new BufferedOutputStream(new FileOutputStream(dest,true)); //追加
            BufferedInputStream bis = null;
            for (int i = 0; i < this.blockPath.size(); i++) {
                bis = new BufferedInputStream(new FileInputStream(new File(this.blockPath.get(i))));
                
                //缓冲区
                byte[] flush = new byte[1024];
                //接收长度
                int len =0;
                while(-1!=(len=bis.read(flush))){                        
                    bos.write(flush, 0, len);
                }
                bos.flush();
                FileUtil.close(bis);
            }
        } catch (Exception e) {
        }finally{
            FileUtil.close(bos);
        }        
        
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        SplitFile split = new SplitFile("E:/xp/20130502/test/学员设置(20130502).xls","E:/xp/20130502",51);
        
        //System.out.println(split.size);
        
        //split.split();
        
        split.merge("E:/xp/20130502/test1.xls");
    }

}

IO(二)

标签:des   style   blog   color   io   os   使用   ar   java   

原文地址:http://www.cnblogs.com/sunhan/p/4005497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!