工具方法
/**
* 拷貝文件,將單個文件拷貝到另一個目錄
*
* @author 靜心事成
* @param source 源文件
* @param target 目標文件
* @param isOverwrite 是否覆蓋目標文件
* @return target.toPath() 目標文件路徑
* @throws IOException
* */
public static Path copy(File source, File target, boolean isOverride) throws IOException {
// 使用java 7的新特性方法
return Files.copy(source.toPath(), target.toPath(), isOverride ? StandardCopyOption.REPLACE_EXISTING : StandardCopyOption.COPY_ATTRIBUTES);
}
/**
* 拷貝文件,將單個文件拷貝到另一個目錄
* Path(File, File, boolean)的重載方法
*
* @param source 源文件路徑
* @param target 目標文件路徑
* @param isOverwrite 是否覆蓋目標文件
* @return target.toPath() 目標文件路徑
* @throws IOException
* */
public static Path copy(String source, String target, boolean isOverride) throws IOException {
return copy(new File(source), new File(target), isOverride);
}
引用的類
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
示例
public static void main(String[] args) {
String source = "D:\\imgPath\\ewm.jpg";
String target = "D:\\imgPath\\ewm2.jpg";
try {
copy(source, target, true);
} catch (IOException e) {
e.printStackTrace();
}
}
捐贈
若你感覺讀到這篇文章對你有啓發,能引起你的思考。請不要吝嗇你的錢包,你的任何打賞或者捐贈都是對我莫大的鼓勵。