public void copy(File src, File dst) throws IOException {
//if the parameters are same,then don't excute anything.or it make original file null.
if(!src.getAbsolutePath().equalsIgnoreCase(dst.getAbsolutePath())){
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
这段Java代码实现了文件复制功能。定义了一个copy方法,接收源文件和目标文件作为参数。若两文件路径不同,通过输入输出流将源文件的字节数据逐块读取并写入目标文件,最后关闭流,避免原文件为空。

被折叠的 条评论
为什么被折叠?



