关闭流:
普通io方式的拷贝:
nio方式的拷贝:
普通io方式读取文本:
nio读取文本:
普通io方式写出文本:
nio写出文本:
public static void closeSilently(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ex) {
}
}
}
普通io方式的拷贝:
/***
* <p> 使用字节流进行拷贝. 拷贝完成后不会关闭流, 流的位置将保持在最后一次读取和写入的位置. </p>
*/
public static void copy(InputStream src, OutputStream dst, int bufferSize) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(dst);
byte[] buffer = new byte[bufferSize];
int len;
while ((len = src.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.flush();
}
/***
* <p> 使用字符流进行拷贝. 拷贝完成后不会关闭流, 流的位置将保持在最后一次读取和写入的位置. </p>
*/
public static void copy(Reader src, Writer dst, int bufferSize) throws IOException {
BufferedWriter bw = new BufferedWriter(dst);
char[] buffer = new char[bufferSize];
int len;
while ((len = src.read(buffer)) != -1) {
bw.write(buffer, 0, len);
}
bw.flush();
}
/***
* <p> 使用随机访问文件进行拷贝. 拷贝完成后不会关闭文件, 位置将保持在最后一次读取和写入的位置. </p>
*/
public static void copy(RandomAccessFile src, RandomAccessFile dst, int bufferSize) throws IOException {
byte[] buffer = new byte[bufferSize];
int len;
while ((len = src.read(buffer)) != -1) {
dst.write(buffer, 0, len);
}
}
nio方式的拷贝:
/***
* <p> 使用FileChannel的transferFrom进行拷贝. transferFrom最大支持的文件在2GB左右, 不同的jvm可能会有不同的限制. </p>
*/
public static void copy(String srcFile, String dstFile) throws IOException {
FileChannel srcChannel = null;
FileChannel dstChannel = null;
try {
srcChannel = new FileInputStream(srcFile).getChannel();
dstChannel = new RandomAccessFile(dstFile, "rw").getChannel();
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
// 或者
// srcChannel.transferTo(0, dstChannel.size(), dstChannel);
} finally {
closeSilently(srcChannel);
closeSilently(dstChannel);
}
}
/***
* <p> 使用Channel进行拷贝. 拷贝完成后不会关Channel, Channel将位置保持在最后一次读取和写入的位置.</p>
*/
public static void copy(ReadableByteChannel src, WritableByteChannel dst, int bufferSize) throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
while (src.read(buffer) != -1) {
buffer.flip();
dst.write(buffer);
buffer.clear();
}
}
普通io方式读取文本:
/***
* <p> 从指定的流读取文本. 读取结束后流不会关闭. </p>
* @param is 要读取文本的流
* @param enc 流中文本所使用的编码
* @return
* @throws IOException
*/
public static String readString(InputStream is, String enc) throws IOException {
try {
return readString(new InputStreamReader(is, enc));
} catch (UnsupportedEncodingException ex) {
throw new IllegalArgumentException(ex);
}
}
private static String readString(Reader r) throws IOException {
BufferedReader reader = new BufferedReader(r);
StringBuilder buffer = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();
}
/***
* <p> 从指定的文件读取文本. </p>
* @param filepath 要读取文本的文件
* @return
* @throws IOException
*/
public static String readString(String filepath) throws IOException {
// 不像InputStreamReader, FileReader可以自动识别文件的编码
FileReader reader = null;
try {
reader = new FileReader(filepath);
return readString(reader);
} finally {
closeSilently(reader);
}
}
public static String scanString(String filepath) throws IOException {
Scanner sc = null;
try {
File file = new File(filepath);
sc = new Scanner(file);
StringBuilder buffer = new StringBuilder((int) (file.length()/3));
while (sc.hasNextLine()) {
buffer.append(sc.nextLine());
}
return buffer.toString();
} finally {
if (sc != null) {
sc.close();
}
}
}
nio读取文本:
/***
* <p> 以nio的方式从指定Chanle读取文本. 读取完成后不会关Channel, Channel将位置保持在最后一次读取的位置. </p>
*/
public static void readString(ReadableByteChannel src, int bufferSize, String enc) throws IOException {
try {
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
StringBuilder str = new StringBuilder();
Charset cs = Charset.forName(enc);
while (src.read(buffer) != -1) {
buffer.flip();
CharBuffer cbuffer = cs.decode(buffer);
str.append(cbuffer.toString());
buffer.clear();
// 也可以使用
/*
int length = buffer.remaining();
if (bytes == null || length > bytes.length) {
bytes = new byte[length];
}
buffer.get(tmp, 0, length);
String text = new String(tmp, enc);
str.append(cbuffer.toString());
*/
}
} catch (UnsupportedEncodingException ex) {
throw new IllegalArgumentException(ex);
}
}
普通io方式写出文本:
/***
* <p> 将文本写入到指定的输出流中. 写入完毕后流不会关闭. </p>
* @param text
* @param os
* @param enc
* @throws IOException
*/
public static void writeString(String text, OutputStream os, String enc) throws IOException {
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, enc));
bw.write(text);
bw.flush();
} catch (UnsupportedEncodingException ex) {
throw new IllegalArgumentException(ex);
}
}
public static void writeString(String text, Writer writer) throws IOException {
BufferedWriter bw = new BufferedWriter(writer);
bw.write(text);
}
/***
* <p> 将文本写入到指定的文件中. </p>
* @param text
* @param filepath
* @throws IOException
*/
public static void writeString(String text, String filepath) throws IOException {
FileWriter fw = null;
try {
fw = new FileWriter(filepath);
writeString(text, fw);
} finally {
closeSilently(fw);
}
}
public static void printString(String text, String filepath) throws IOException {
PrintWriter pw = null;
try {
pw = new PrintWriter(new File(filepath));
pw.print(text);
} finally {
closeSilently(pw);
}
}
nio写出文本:
/***
* <p> 以nio的方式将文本写入Chanle. 写入完成后不会关Channel, Channel将位置保持在最后一次写入的位置. </p>
* @param dst 要写入的channel
* @param text 要写出的文本
* @param enc 文本转换为字节时的编码
* @param bufferSize ByteBuffer的大小
*/
public static void writeString(WritableByteChannel dst, String text, String enc, int bufferSize) throws IOException {
byte[] textBytes = text.getBytes(enc);
writeString(dst, textBytes, bufferSize);
}
/***
* <p> 以nio的方式将文本写入Chanle. 写入完成后不会关Channel, Channel将位置保持在最后一次写入的位置. </p>
* @param dst 要写入的channel
* @param textBytes 文本经过编码后的字节
* @param bufferSize ByteBuffer的大小
*/
public static void writeString(WritableByteChannel dst, byte[] textBytes, int bufferSize) throws IOException {
int i = 0;
int strBytesLength = textBytes.length;
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
for (; i < strBytesLength && i + bufferSize < strBytesLength; i += bufferSize) {
buffer.put(textBytes, i, bufferSize);
buffer.flip();
dst.write(buffer);
buffer.clear();
}
int remind = textBytes.length - i;
buffer.put(textBytes, i, remind);
buffer.flip();
dst.write(buffer);
}