熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java 常用IO操作

2022-06-13   來源: Java核心技術 

  package comsyjutil;

  import javaioBufferedReader;
        import javaioBufferedWriter;
        import javaioByteArrayInputStream;
        import javaioByteArrayOutputStream;
        import javaioFile;
        import javaioFileInputStream;
        import javaioFileOutputStream;
        import javaioFileReader;
        import javaioFileWriter;
        import javaioIOException;
        import javaioInputStream;
        import javaioInputStreamReader;
        import javaioObjectInputStream;
        import javaioObjectOutputStream;
        import javaioOutputStream;
        import javaioPrintWriter;
        import javaioStringReader;
        import javautilArrays;

  /**
         * <p>
         * Title:IO工具類
         * </p>
         *
         * <p>
         * Description:常用的IO操作封裝
         * </p>
         *
         * <p>
         * Copyright: 轉載請注明出處<A >
    </A>     * </p>
         *
         * @author 孫钰佳
         * @main <A >mailto:>
    </A>     * @date Jun :: PM
         */

  public class IOUtil {
         /**
          * 緩沖區大小 MB
          */
         private static final int BUFFER_SIZE = * ;

  /**
          *
          * Description: 將輸入流輸出到輸出流
          *
         * @param in
          *            輸入流
          * @param out
          *            輸出流
          * @param bufferSize
          *            緩沖區大小
          * @throws IOException
          * @mail <A >mailto:>
    </A>      * @sinceJun :: PM
          */
         public static void inOutStream(InputStream in OutputStream out

  int bufferSize) throws IOException {
          byte[] buffer = new byte[bufferSize];// 緩沖區
          for (int bytesRead = ; (bytesRead = inread(buffer)) != ;) {
           outwrite(buffer bytesRead);
           Arraysfill(buffer (byte) );
          }
         }

  package comsyjutil;

  import javaioBufferedReader;
        import javaioBufferedWriter;
        import javaioByteArrayInputStream;
        import javaioByteArrayOutputStream;
        import javaioFile;
        import javaioFileInputStream;
        import javaioFileOutputStream;
        import javaioFileReader;
        import javaioFileWriter;
        import javaioIOException;
        import javaioInputStream;
        import javaioInputStreamReader;
        import javaioObjectInputStream;
        import javaioObjectOutputStream;
        import javaioOutputStream;
        import javaioPrintWriter;
        import javaioStringReader;
        import javautilArrays;

  /**
         * <p>
         * Title:IO工具類
         * </p>
         *
         * <p>
         * Description:常用的IO操作封裝
         * </p>
         *
         * <p>
         * Copyright: 轉載請注明出處
         * </p>
         *
         * @author 孫钰佳
         * @main
         * @date Jun :: PM
         */

  public class IOUtil {
         /**
          * 緩沖區大小 MB
          */
         private static final int BUFFER_SIZE = * ;

  /**
          *
          * Description: 將輸入流輸出到輸出流
          *
         * @param in
          *            輸入流
          * @param out
          *            輸出流
          * @param bufferSize
          *            緩沖區大小
          * @throws IOException
          * @mail
          * @sinceJun :: PM
          */
         public static void inOutStream(InputStream in OutputStream out

  int bufferSize) throws IOException {
          byte[] buffer = new byte[bufferSize];// 緩沖區
          for (int bytesRead = ; (bytesRead = inread(buffer)) != ;) {
           outwrite(buffer bytesRead);
           Arraysfill(buffer (byte) );
          }
         }
    view plaincopy to clipboardprint?
         /**
          *
          * Description: 讀取文件返回字節數組流
          *
          * @param file
          *            文件
          * @return 字節數組流
          * @mail <A >mailto:>
    </A>      * @sinceJun :: PM
          */
         public static ByteArrayOutputStream readFileToByteStream(File file)
           throws IOException {

  FileInputStream fis = null;
          ByteArrayOutputStream bos = null;
          try {
           fis = new FileInputStream(file);
           bos = new ByteArrayOutputStream();
           inOutStream(fis bos BUFFER_SIZE);
          } finally {
           if (fis != null)
            fisclose();
          }

  return bos;

  }

  /**
          *
          * Description:讀取文件返回字節數組
          *
          * @param file
          *            文件
          * @return 字節數組
          * @throws IOException
          * @mail <A >mailto:>
    </A>      * @sinceJun :: PM
          */
         public static byte[] readFileToByteArray(File file) throws IOException {

  ByteArrayOutputStream bos = null;
          try {
           bos = readFileToByteStream(file);
          } finally {
           if (bos != null)
            bosclose();
          }

  return bostoByteArray();

  }

  /**
          *
          * Description:讀取文件內容
          *
          * @param file
          *            文件
          * @return String內容
          * @throws IOException
          * @mail <A >mailto:>
    </A>      * @sinceJun :: 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 = inreadLine()) != null;) {
            sbappend(line + \r\n);
           }
          } finally {
           if (in != null)
            inclose();
          }

  return sbtoString();

  }

  /**
          *
          * Description:復制文件
          *
          * @param src
          *            源文件
          * @param dest
          *            目標文件
          * @param cover
          *            是否覆蓋
          * @throws IOException
          * @mail <A >mailto:>
    </A>      * @sinceJun :: PM
          */
         public static void copyFile(File src File dest boolean cover)
           throws IOException {

  /**
          *
          * Description: 讀取文件返回字節數組流
          *
          * @param file
          *            文件
          * @return 字節數組流
          * @mail
          * @sinceJun :: PM
          */
         public static ByteArrayOutputStream readFileToByteStream(File file)
           throws IOException {

  FileInputStream fis = null;
          ByteArrayOutputStream bos = null;
          try {
           fis = new FileInputStream(file);
           bos = new ByteArrayOutputStream();
           inOutStream(fis bos BUFFER_SIZE);
          } finally {
           if (fis != null)
            fisclose();
          }

  return bos;

  }

  /**
          *
          * Description:讀取文件返回字節數組
          *
          * @param file
          *            文件
          * @return 字節數組
          * @throws IOException
          * @mail
          * @sinceJun :: PM
          */
         public static byte[] readFileToByteArray(File file) throws IOException {

  ByteArrayOutputStream bos = null;
          try {
           bos = readFileToByteStream(file);
          } finally {
           if (bos != null)
            bosclose();
          }

  return bostoByteArray();

  }

  /**
          *
          * Description:讀取文件內容
          *
          * @param file
          *            文件
          * @return String內容
          * @throws IOException
          * @mail
          * @sinceJun :: 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 = inreadLine()) != null;) {
            sbappend(line + \r\n);
           }
          } finally {
           if (in != null)
            inclose();
          }

  return sbtoString();

  }

  /**
          *
          * Description:復制文件
          *
          * @param src
          *            源文件
          * @param dest
          *            目標文件
          * @param cover
          *            是否覆蓋
          * @throws IOException
          * @mail
          * @sinceJun :: PM
          */
         public static void copyFile(File src File dest boolean cover)
           throws IOException {

  view plaincopy to clipboardprint?
          FileInputStream in = null;
          FileOutputStream out = null;
          try {
           if (!destexists()) {
            destcreateNewFile();
           } else if (destexists() && cover) {
            destdelete();
            destcreateNewFile();
           } else {

  return;

  }

  in = new FileInputStream(src);
           out = new FileOutputStream(dest);
           inOutStream(in out BUFFER_SIZE);

  } finally {
           try {
            if (in != null)
             inclose();
           } finally {
            if (out != null)
             outclose();
           }
          }
         }

  /**
          *
          * Description:寫文件
          *
          * @param file
          *            文件
          * @param str
          *            內容
          * @throws IOException
          * @mail <A >mailto:>
    </A>      * @sinceJun :: PM
          */
         public static void writeFile(File file String str) throws IOException {

  PrintWriter out = null;
          BufferedReader in = null;
          try {
           if (!fileexists())
            filecreateNewFile();
           in = new BufferedReader(new StringReader(str));
           out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
           for (String line; (line = inreadLine()) != null;) {
            outprintln(line);
           }
          } finally {

  try {
            if (in != null)
             inclose();
           } finally {
            if (out != null)
             outclose();
           }
          }
         }

  /**
          *
          * Description:從控制台讀取一串字符串
          *
          * @return 讀取的字符串
          * @throws IOException
          * @mail <A >mailto:>
    </A>      * @sinceJun :: PM
          */
         public static String readStringFromSystemIn() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(Systemin));
          try {
           return brreadLine();
          } finally {
           if (br != null)
            brclose();
          }
         }

  /**
          *
          * Description:當ObjectInputStream對象調用
          * readObject();時會從ByteArrayInputStream流中反序列化出的對象
          *
          *
          * @param bi
          * @return
          * @throws IOException
          * @mail <A >mailto:>
    </A>      * @sinceJun :: 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 <A >mailto:>
    </A>      * @sinceJun :: 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(strgetBytes());

  }

  public static ByteArrayInputStream buildByteArrayInputStream(byte[] bt) {

  return new ByteArrayInputStream(bt);

  }

  public static void main(String[] args) throws Exception {
          byte[] bootFileBytes = IOUtilreadFileToByteArray(new File(
            C:\\bootini));
          Systemoutprintln(new String(bootFileBytes));
          String bootFileStr = readFileToString(new File(C:\\bootini));
          Systemoutprintln(bootFileStr);
          Systemoutprintln(new String(bootFileBytes)equals(bootFileStr));

  pyFile(new File(C:\\bootini) new File(C:\\bootini)
            true);

  IOUtilwriteFile(new File(C:\\bootini) bootFileStr);
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          ObjectOutputStream oos = IOUtilbuildObjectOutputStream(bos);
          ooswriteObject(new String(abcd));

  ObjectInputStream ois = IOUtilbuildObjectInputStream(IOUtil
            buildByteArrayInputStream(bostoByteArray()));

  Systemoutprintln(oisreadObject());
          Systemoutprintln(IOUtilreadStringFromSystemIn());
         }
        }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25681.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.