做项目导入导出用到,开始用的BufferedReader可是压缩文件会损坏,在网上找的例子,整理了下
/**
* 导入
* @param urlPath 附件相对路径(xml存储路径)
* @param path 项目绝对路径
* @param keyID 要导出信息的keyID
* @param filepath 导入后路径
*/
private static void importCopy(String urlPath, String path, String keyID, String filepath) {
// 生成目录
File f = new File(path + filepath);
if (!f.exists()) {
f.mkdirs();
}
String filename = urlPath.substring(urlPath.lastIndexOf("/") + 1);
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try {
fi = new FileInputStream(path + "/imp/" + keyID + "/" + filename);
fo = new FileOutputStream(path + filepath + filename);
in = fi.getChannel();// 得到对应的文件通道
out = fo.getChannel();// 得到对应的文件通道
in.transferTo(0, in.size(), out);// 连接两个通道,并且从in通道读取,然后写入out通道
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}