乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      實用的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數(shù)組轉(zhuǎn)成十六進制的字符串

          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的數(shù)轉(zhuǎn)為16進制只有一位數(shù)

                    buf.append("0");//如果是一位數(shù)就補一個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;

          }

          //以十六進制方式讀取一個文件轉(zhuǎn)換成字符串

          public static String readAsHex(String file) throws IOException{

             return hex(reaAll(file));

          }

          //自己管理一個緩沖區(qū)復(fù)制文件,速度快

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

          }

          //用系統(tǒng)提供的緩沖區(qū)復(fù)制文件,速度比較快

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

          }

          //不使用緩沖區(qū)一個byte一個byte的復(fù)制文件,速度最慢

          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對象轉(zhuǎn)換為字節(jié)序列的過程稱為對象的序列化。

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

          可對參數(shù)指定的obj對象進行序列化,把得到的字節(jié)序列寫到一個目標輸出流中。

          */

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

          }

          /*把字節(jié)序列恢復(fù)為Java對象的過程稱為對象的反序列化。

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

           * 從一個源輸入流中讀取字節(jié)序列,再把它們反序列化為一個對象,并將其返回。

          */

          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;      

          }

          //深層復(fù)制對象

          public static Serializable deepCopy(Serializable obj){

             try {

                 return unserialize(serialize(obj));

             } catch (Exception e) {

                 e.printStackTrace();

                 throw new RuntimeException();

             }

          }

      }

       

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多