1. 最快速度的文件拷贝,管道对管道。
/**
* Create report file.
* @param srcFile
* @param dstFile
*/
private void CreateReportFile(String srcFile, String dstFile) {
int length = 1048891;
FileChannel inC = null;
FileChannel outC = null;
try {
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(dstFile);
inC = in.getChannel();
outC = out.getChannel();
ByteBuffer b = null;
while (inC.position() < inC.size()) {
if ((inC.size() - inC.position()) < length) {
length = (int) (inC.size() - inC.position());
} else
length = 1048891;
b = ByteBuffer.allocateDirect(length);
inC.read(b);
b.flip();
outC.write(b);
outC.force(false);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inC != null && inC.isOpen()) {
inC.close();
}
if (outC != null && outC.isOpen()) {
outC.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. 文件压缩, zip
/**
*
* @param file
* @param zipfile
*/
private void zipFile(String file, String zipfile) {
try {
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(zipfile);
ZipOutputStream zipOut = new ZipOutputStream(out);
ZipEntry entry = new ZipEntry(file);
zipOut.putNextEntry(entry);
int nNumber;
byte[] buffer = new byte[512];
while ((nNumber = in.read(buffer)) != -1)
zipOut.write(buffer, 0, nNumber);
zipOut.close();
out.close();
in.close();
} catch (IOException e) {
System.out.println(e);
}
}
3. 获得系统时间。
/**
* @param sPath
* @return
*/
private void deleteFile(String sPath) {
File file = new File(sPath);
if (file.isFile() && file.exists()) {
file.delete();
}
}