package com
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
/**
* <p>
* Title:IO工具類
* </p>
*
* <p>
* Description:常用的IO操作封裝
* </p>
*
* <p>
* Copyright: 轉載請注明出處<A >
</A> * </p>
*
* @author 孫钰佳
* @main <A
</A> * @date Jun
*/
public class IOUtil {
/**
* 緩沖區大小
*/
private static final int BUFFER_SIZE =
/**
*
* Description: 將輸入流輸出到輸出流
*
* @param in
* 輸入流
* @param out
* 輸出流
* @param bufferSize
* 緩沖區大小
* @throws IOException
* @mail <A
</A> * @since
*/
public static void in
int bufferSize) throws IOException {
byte[] buffer = new byte[bufferSize];// 緩沖區
for (int bytesRead =
out
Arrays
}
}
package com
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
/**
* <p>
* Title:IO工具類
* </p>
*
* <p>
* Description:常用的IO操作封裝
* </p>
*
* <p>
* Copyright: 轉載請注明出處
* </p>
*
* @author 孫钰佳
* @main
* @date Jun
*/
public class IOUtil {
/**
* 緩沖區大小
*/
private static final int BUFFER_SIZE =
/**
*
* Description: 將輸入流輸出到輸出流
*
* @param in
* 輸入流
* @param out
* 輸出流
* @param bufferSize
* 緩沖區大小
* @throws IOException
* @mail
* @since
*/
public static void in
int bufferSize) throws IOException {
byte[] buffer = new byte[bufferSize];// 緩沖區
for (int bytesRead =
out
Arrays
}
}
view plaincopy to clipboardprint?
/**
*
* Description: 讀取文件返回字節數組流
*
* @param file
* 文件
* @return 字節數組流
* @mail <A
</A> * @since
*/
public static ByteArrayOutputStream readFileToByteStream(File file)
throws IOException {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
in
} finally {
if (fis != null)
fis
}
return bos;
}
/**
*
* Description:讀取文件返回字節數組
*
* @param file
* 文件
* @return 字節數組
* @throws IOException
* @mail <A
</A> * @since
*/
public static byte[] readFileToByteArray(File file) throws IOException {
ByteArrayOutputStream bos = null;
try {
bos = readFileToByteStream(file);
} finally {
if (bos != null)
bos
}
return bos
}
/**
*
* Description:讀取文件內容
*
* @param file
* 文件
* @return String內容
* @throws IOException
* @mail <A
</A> * @since
*/
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
sb
}
} finally {
if (in != null)
in
}
return sb
}
/**
*
* Description:復制文件
*
* @param src
* 源文件
* @param dest
* 目標文件
* @param cover
* 是否覆蓋
* @throws IOException
* @mail <A
</A> * @since
*/
public static void copyFile(File src
throws IOException {
/**
*
* Description: 讀取文件返回字節數組流
*
* @param file
* 文件
* @return 字節數組流
* @mail
* @since
*/
public static ByteArrayOutputStream readFileToByteStream(File file)
throws IOException {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
in
} finally {
if (fis != null)
fis
}
return bos;
}
/**
*
* Description:讀取文件返回字節數組
*
* @param file
* 文件
* @return 字節數組
* @throws IOException
* @mail
* @since
*/
public static byte[] readFileToByteArray(File file) throws IOException {
ByteArrayOutputStream bos = null;
try {
bos = readFileToByteStream(file);
} finally {
if (bos != null)
bos
}
return bos
}
/**
*
* Description:讀取文件內容
*
* @param file
* 文件
* @return String內容
* @throws IOException
* @mail
* @since
*/
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
sb
}
} finally {
if (in != null)
in
}
return sb
}
/**
*
* Description:復制文件
*
* @param src
* 源文件
* @param dest
* 目標文件
* @param cover
* 是否覆蓋
* @throws IOException
* @mail
* @since
*/
public static void copyFile(File src
throws IOException {
view plaincopy to clipboardprint?
FileInputStream in = null;
FileOutputStream out = null;
try {
if (!dest
dest
} else if (dest
dest
dest
} else {
return;
}
in = new FileInputStream(src);
out = new FileOutputStream(dest);
in
} finally {
try {
if (in != null)
in
} finally {
if (out != null)
out
}
}
}
/**
*
* Description:寫文件
*
* @param file
* 文件
* @param str
* 內容
* @throws IOException
* @mail <A
</A> * @since
*/
public static void writeFile(File file
PrintWriter out = null;
BufferedReader in = null;
try {
if (!file
file
in = new BufferedReader(new StringReader(str));
out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
for (String line; (line = in
out
}
} finally {
try {
if (in != null)
in
} finally {
if (out != null)
out
}
}
}
/**
*
* Description:從控制台讀取一串字符串
*
* @return 讀取的字符串
* @throws IOException
* @mail <A
</A> * @since
*/
public static String readStringFromSystemIn() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System
try {
return br
} finally {
if (br != null)
br
}
}
/**
*
* Description:當ObjectInputStream對象調用
* readObject();時
*
*
* @param bi
* @return
* @throws IOException
* @mail <A
</A> * @since
*/
public static ObjectInputStream buildObjectInputStream(
ByteArrayInputStream bi) throws IOException {
return new ObjectInputStream(bi);
}
/**
*
* Description:當ObjectOutputStream對象調用
* writeObject(o);時
*
* @param bos
* 字節數組流
* @return 對象輸出流
* @throws IOException
* @mail <A
</A> * @since
*/
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
}
public static ByteArrayInputStream buildByteArrayInputStream(byte[] bt) {
return new ByteArrayInputStream(bt);
}
public static void main(String[] args) throws Exception {
byte[] bootFileBytes = IOUtil
System
String bootFileStr = readFileToString(new File(
System
System
pyFile(new File(
true);
IOUtil
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = IOUtil
oos
ObjectInputStream ois = IOUtil
System
System
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25681.html