posts - 20,  comments - 2,  trackbacks - 0

          作者: tianshi0253  鏈接:http://tianshi0253.javaeye.com/blog/204572  發表時間: 2008年06月17日

          聲明:本文系JavaEye網站發布的原創博客文章,未經作者書面許可,嚴禁任何網站轉載本文,否則必將追究法律責任!

          package com.syj.util;   
            
            
            
          import java.io.BufferedReader;   
            
          import java.io.BufferedWriter;   
            
          import java.io.ByteArrayInputStream;   
            
          import java.io.ByteArrayOutputStream;   
            
          import java.io.File;   
            
          import java.io.FileInputStream;   
            
          import java.io.FileOutputStream;   
            
          import java.io.FileReader;   
            
          import java.io.FileWriter;   
            
          import java.io.IOException;   
            
          import java.io.InputStream;   
            
          import java.io.InputStreamReader;   
            
          import java.io.ObjectInputStream;   
            
          import java.io.ObjectOutputStream;   
            
          import java.io.OutputStream;   
            
          import java.io.PrintWriter;   
            
          import java.io.StringReader;   
            
          import java.util.Arrays;   
            
            
            
          /**  
           
           * <p>  
           
           * Title:IO工具類  
           
           * </p>  
           
           *   
           
           * <p>  
           
           * Description:常用的IO操作封裝  
           
           * </p>  
           
           *   
           
           * <p>  
           
           * Copyright: 轉載請注明出處http://blog.csdn.net/sunyujia/  
           
           * </p>  
           
           *   
           
           * @author 孫鈺佳  
           
           * @main sunyujia@yahoo.cn  
           
           * @date Jun 15, 2008 4:37:58 PM  
           
           */  
            
          public class IOUtil {   
            
              /**  
           
               * 緩沖區大小 1MB  
           
               */  
            
              private static final int BUFFER_SIZE = 1024 * 1024;   
            
            
            
              /**  
           
               *   
           
               * Description: 將輸入流輸出到輸出流  
           
               *   
           
               * @param in  
           
               *            輸入流  
           
               * @param out  
           
               *            輸出流  
           
               * @param bufferSize  
           
               *            緩沖區大小  
           
               * @throws IOException  
           
               * @mail sunyujia@yahoo.cn  
           
               * @since:Jun 15, 2008 5:57:24 PM  
           
               */  
            
              public static void in2OutStream(InputStream in, OutputStream out,   
            
                      int bufferSize) throws IOException {   
            
                  byte[] buffer = new byte[bufferSize];// 緩沖區   
            
                  for (int bytesRead = 0; (bytesRead = in.read(buffer)) != -1;) {   
            
                      out.write(buffer, 0, bytesRead);   
            
                      Arrays.fill(buffer, (byte) 0);   
            
                  }   
            
              }   
            
            
            
              /**  
           
               *   
           
               * Description: 讀取文件返回字節數組流  
           
               *   
           
               * @param file  
           
               *            文件  
           
               * @return 字節數組流  
           
               * @mail sunyujia@yahoo.cn  
           
               * @since:Jun 15, 2008 4:52:41 PM  
           
               */  
            
              public static ByteArrayOutputStream readFileToByteStream(File file)   
            
                      throws IOException {   
            
                  FileInputStream fis = null;   
            
                  ByteArrayOutputStream bos = null;   
            
                  try {   
            
                      fis = new FileInputStream(file);   
            
                      bos = new ByteArrayOutputStream();   
            
                      in2OutStream(fis, bos, BUFFER_SIZE);   
            
                  } finally {   
            
                      if (fis != null)   
            
                          fis.close();   
            
                  }   
            
                  return bos;   
            
              }   
            
            
            
              /**  
           
               *   
           
               * Description:讀取文件返回字節數組  
           
               *   
           
               * @param file  
           
               *            文件  
           
               * @return 字節數組  
           
               * @throws IOException  
           
               * @mail sunyujia@yahoo.cn  
           
               * @since:Jun 15, 2008 5:38:50 PM  
           
               */  
            
              public static byte[] readFileToByteArray(File file) throws IOException {   
            
                  ByteArrayOutputStream bos = null;   
            
                  try {   
            
                      bos = readFileToByteStream(file);   
            
                  } finally {   
            
                      if (bos != null)   
            
                          bos.close();   
            
                  }   
            
                  return bos.toByteArray();   
            
              }   
            
            
            
              /**  
           
               *   
           
               * Description:讀取文件內容  
           
               *   
           
               * @param file  
           
               *            文件  
           
               * @return String內容  
           
               * @throws IOException  
           
               * @mail sunyujia@yahoo.cn  
           
               * @since:Jun 15, 2008 5:46:32 PM  
           
               */  
            
              public static String readFileToString(File file) throws IOException {   
            
                  StringBuffer sb = null;   
            
                  BufferedReader in = null;   
            
                  try {   
            
                      in = new BufferedReader(new FileReader(file));   
            
                      sb = new StringBuffer();   
            
                      for (String line; (line = in.readLine()) != null;) {   
            
                          sb.append(line + "\r\n");   
            
                      }   
            
                  } finally {   
            
                      if (in != null)   
            
                          in.close();   
            
                  }   
            
                  return sb.toString();   
            
              }   
            
            
            
              /**  
           
               *   
           
               * Description:復制文件  
           
               *   
           
               * @param src  
           
               *            源文件  
           
               * @param dest  
           
               *            目標文件  
           
               * @param cover  
           
               *            是否覆蓋  
           
               * @throws IOException  
           
               * @mail sunyujia@yahoo.cn  
           
               * @since:Jun 15, 2008 6:08:28 PM  
           
               */  
            
              public static void copyFile(File src, File dest, boolean cover)   
            
                      throws IOException {   
            
                  FileInputStream in = null;   
            
                  FileOutputStream out = null;   
            
                  try {   
            
                      if (!dest.exists()) {   
            
                          dest.createNewFile();   
            
                      } else if (dest.exists() && cover) {   
            
                          dest.delete();   
            
                          dest.createNewFile();   
            
                      } else {   
            
                          return;   
            
                      }   
            
                      in = new FileInputStream(src);   
            
                      out = new FileOutputStream(dest);   
            
                      in2OutStream(in, out, BUFFER_SIZE);   
            
                  } finally {   
            
                      try {   
            
                          if (in != null)   
            
                              in.close();   
            
                      } finally {   
            
                          if (out != null)   
            
                              out.close();   
            
                      }   
            
                  }   
            
              }   
            
            
            
              /**  
           
               *   
           
               * Description:寫文件  
           
               *   
           
               * @param file  
           
               *            文件  
           
               * @param str  
           
               *            內容  
           
               * @throws IOException  
           
               * @mail sunyujia@yahoo.cn  
           
               * @since:Jun 15, 2008 6:17:24 PM  
           
               */  
            
              public static void writeFile(File file, String str) throws IOException {   
            
                  PrintWriter out = null;   
            
                  BufferedReader in = null;   
            
                  try {   
            
                      if (!file.exists())   
            
                          file.createNewFile();   
            
                      in = new BufferedReader(new StringReader(str));   
            
                      out = new PrintWriter(new BufferedWriter(new FileWriter(file)));   
            
                      for (String line; (line = in.readLine()) != null;) {   
            
                          out.println(line);   
            
                      }   
            
                  } finally {   
            
                      try {   
            
                          if (in != null)   
            
                              in.close();   
            
                      } finally {   
            
                          if (out != null)   
            
                              out.close();   
            
                      }   
            
                  }   
            
              }   
            
            
            
              /**  
           
               *   
           
               * Description:從控制臺讀取一串字符串  
           
               *   
           
               * @return 讀取的字符串  
           
               * @throws IOException  
           
               * @mail sunyujia@yahoo.cn  
           
               * @since:Jun 15, 2008 6:42:29 PM  
           
               */  
            
              public static String readStringFromSystemIn() throws IOException {   
            
                  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
            
                  try {   
            
                      return br.readLine();   
            
                  } finally {   
            
                      if (br != null)   
            
                          br.close();   
            
                  }   
            
              }   
            
            
            
              /**  
           
               *   
           
               * Description:當ObjectInputStream對象調用  
           
               * readObject();時,會從ByteArrayInputStream流中反序列化出的對象  
           
               *   
           
               *   
           
               * @param bi  
           
               * @return  
           
               * @throws IOException  
           
               * @mail sunyujia@yahoo.cn  
           
               * @since:Jun 15, 2008 7:07:53 PM  
           
               */  
            
              public static ObjectInputStream buildObjectInputStream(   
            
                      ByteArrayInputStream bi) throws IOException {   
            
                  return new ObjectInputStream(bi);   
            
              }   
            
            
            
              /**  
           
               *   
           
               * Description:當ObjectOutputStream對象調用  
           
               * writeObject(o);時,o對象會序列化到ByteArrayOutputStream流中去  
           
               *   
           
               * @param bos  
           
               *            字節數組流  
           
               * @return 對象輸出流  
           
               * @throws IOException  
           
               * @mail sunyujia@yahoo.cn  
           
               * @since:Jun 15, 2008 7:06:00 PM  
           
               */  
            
              public static ObjectOutputStream buildObjectOutputStream(   
            
                      ByteArrayOutputStream bos) throws IOException {   
            
                  return new ObjectOutputStream(bos);   
            
              }   
            
            
            
              public static BufferedReader buildBufferedReader(String str) {   
            
                  return new BufferedReader(new StringReader(str));   
            
              }   
            
            
            
              public static ByteArrayInputStream buildByteArrayInputStream(String str) {   
            
                  return new ByteArrayInputStream(str.getBytes());   
            
              }   
            
            
            
              public static ByteArrayInputStream buildByteArrayInputStream(byte[] bt) {   
            
                  return new ByteArrayInputStream(bt);   
            
              }   
            
            
            
              public static void main(String[] args) throws Exception {   
            
                  byte[] bootFileBytes = IOUtil.readFileToByteArray(new File(   
            
                          "C:\\boot.ini"));   
            
                  System.out.println(new String(bootFileBytes));   
            
                  String bootFileStr = readFileToString(new File("C:\\boot.ini"));   
            
                  System.out.println(bootFileStr);   
            
                  System.out.println(new String(bootFileBytes).equals(bootFileStr));   
            
                  IOUtil.copyFile(new File("C:\\boot.ini"), new File("C:\\boot1.ini"),   
            
                          true);   
            
                  IOUtil.writeFile(new File("C:\\boot2.ini"), bootFileStr);   
            
                  ByteArrayOutputStream bos = new ByteArrayOutputStream();   
            
                  ObjectOutputStream oos = IOUtil.buildObjectOutputStream(bos);   
            
                  oos.writeObject(new String("abcd"));   
            
                  ObjectInputStream ois = IOUtil.buildObjectInputStream(IOUtil   
            
                          .buildByteArrayInputStream(bos.toByteArray()));   
            
                  System.out.println(ois.readObject());   
            
                  System.out.println(IOUtil.readStringFromSystemIn());   
            
              }   
            
          } 

           


          本文的討論也很精彩,瀏覽討論>>


          JavaEye推薦




          文章來源:http://tianshi0253.javaeye.com/blog/204572
          posted on 2008-06-17 09:40 姚文超 閱讀(117) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 伊春市| 海兴县| 尚义县| 兴国县| 寿光市| 高清| 定西市| 博野县| 无为县| 贵阳市| 精河县| 罗田县| 蛟河市| 湘阴县| 开阳县| 交城县| 和静县| 松江区| 内丘县| 恭城| 伊宁县| 永登县| 思南县| 台州市| 葫芦岛市| 岑溪市| 三河市| 彝良县| 肇东市| 石首市| 喀喇| 从化市| 元江| 池州市| 天门市| 那曲县| 乳源| 白城市| 怀化市| 永福县| 江陵县|