分享

实用的IO工具类

 fenyu8 2011-07-24

package day09;

 

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.OutputStream;

import java.io.RandomAccessFile;

import java.io.Serializable;

 

public class IOUtils {

    // byte数组转成十六进制的字符串

    public static String hex(byte[] ary){

       if(ary==null||ary.length==0){

           return "";

       }

       StringBuilder buf = new StringBuilder();

       for(byte b:ary){

           int i = b & 0xff;//去除高24位的1

           if(i<=0xf){//0xf=15,小于或等于15的数转为16进制只有一位数

              buf.append("0");//如果是一位数就补一个0

           }

           buf.append(Integer.toHexString(i)).append(" ");

       }

       return buf.toString();

    }

    //把一个文件读取出来保存到一个byte[]

    public static byte[] reaAll(String file) throws IOException{

       RandomAccessFile raf = new RandomAccessFile(file, "r");

       byte[] buf = new byte[(int)raf.length()];

       raf.read(buf);

       raf.close();

       return buf;

    }

    //以十六进制方式读取一个文件转换成字符串

    public static String readAsHex(String file) throws IOException{

       return hex(reaAll(file));

    }

    //自己管理一个缓冲区复制文件,速度快

    public static void cp(String file1,String file2) throws IOException{

       InputStream in = new FileInputStream(file1);

       OutputStream out = new FileOutputStream(file2);

       byte[] buf = new byte[1024*500];

       int c;

       while((c=in.read(buf))!=-1){

           out.write(buf,0,c);

       }

       in.close();

       out.close();

    }

    //用系统提供的缓冲区复制文件,速度比较快

    public static void cp1(String file1,String file2) throws IOException{

       InputStream in = new BufferedInputStream(new FileInputStream(file1));

       OutputStream out = new BufferedOutputStream(new FileOutputStream(file2));

       int b;

       while((b=in.read())!=-1){

           System.out.print(b);

           out.write(b);

       }

       in.close();

       out.close();

    }

    //不使用缓冲区一个byte一个byte的复制文件,速度最慢

    public static void cp2(String file1,String file2) throws IOException{

       InputStream in = new FileInputStream(file1);

       OutputStream out = new FileOutputStream(file2);

       int b;

       while((b=in.read())!=-1){

           System.out.print(b);

           out.write(b);

       }

       in.close();

       out.close();

    }

    /*Java对象转换为字节序列的过程称为对象的序列化。

    java.io.ObjectOutputStream代表对象输出流,它的writeObject(Object obj)方法

    可对参数指定的obj对象进行序列化,把得到的字节序列写到一个目标输出流中。

    */

    public static byte[] serialize(Serializable obj) throws IOException{

       ByteArrayOutputStream buf = new ByteArrayOutputStream();

       ObjectOutputStream out = new ObjectOutputStream(buf);

       out.writeObject(obj);

       out.close();

       return buf.toByteArray();      

    }

    /*把字节序列恢复为Java对象的过程称为对象的反序列化。

     * java.io.ObjectInputStream代表对象输入流,它的readObject()方法

     * 从一个源输入流中读取字节序列,再把它们反序列化为一个对象,并将其返回。

    */

    public static Serializable unserialize(byte[] ary) throws IOException, ClassNotFoundException{

       ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(ary));

       Serializable obj = (Serializable)in.readObject();

       in.close();

       return obj;      

    }

    //深层复制对象

    public static Serializable deepCopy(Serializable obj){

       try {

           return unserialize(serialize(obj));

       } catch (Exception e) {

           e.printStackTrace();

           throw new RuntimeException();

       }

    }

}

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多