1. 程式人生 > >封裝了集中常用的文件讀的方法

封裝了集中常用的文件讀的方法

實現 locate ack ear 讀取 exception @param pack flip

package com.opslab.util.algorithmImpl;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
* 封裝了集中常用的文件讀的方法
*/
public class FileReadImpl {

/**
* 利用FileChannel直接實現文件的對拷,對於大文件速度特別明顯
*
* @param source
* @param target
*/
public static void copyFileWithChannel(File source, File target) {
try (
FileInputStream inStream = new FileInputStream(source);
FileOutputStream outStream = new FileOutputStream(target);
FileChannel in = inStream.getChannel();
FileChannel out = outStream.getChannel();
) {
in.transferTo(0, in.size(), out);
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 使用FileChannel+Buffer實現文件的讀取拷貝是一種極佳的方案
*
* @param source
* @param target
*/
public static void copyFileWithBuffer(File source, File target) {
try (
FileInputStream inStream = new FileInputStream(source);
FileOutputStream outStream = new FileOutputStream(target);
FileChannel in = inStream.getChannel();
FileChannel out = outStream.getChannel()
) {
ByteBuffer buffer = ByteBuffer.allocate(4096);
while (in.read(buffer) != -1) {
buffer.flip();
out.write(buffer);
buffer.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 利用Buffer實現文件的讀取拷貝
*
* @param source
* @param target
*/
public static void customBufferBufferedStreamCopy(File source, File target) {
try (
InputStream fis = new BufferedInputStream(new FileInputStream(source));
OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))
) {
byte[] buf = new byte[4096];
int i;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 利用Buffer實現文件的讀取拷貝
*
* @param source
* @param target
*/
public static void customBufferStreamCopy(File source, File target) {
try (
InputStream fis = new FileInputStream(source);
OutputStream fos = new FileOutputStream(target);
) {
byte[] buf = new byte[4096];
int i;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
} catch (IOException e) {
e.printStackTrace();
}
}

}

封裝了集中常用的文件讀的方法