private static void copy(String source, String target) { // 检查source是否存在 Path sourcePath =Paths.get(source).normalize().toAbsolutePath(); if (!sourcePath.toFile().isFile()){ throw new IllegalArgumentException(source); } // 检查target是否存在,不存在创建 Path targetPath =Paths.get(target).normalize().toAbsolutePath(); Path targetParentPath =targetPath.getParent(); if (!targetParentPath.toFile().exists()){ targetParentPath.toFile().mkdirs(); } // 复制 try (InputStream input= new FileInputStream(sourcePath.toString()); OutputStream output = new FileOutputStream(targetPath.toString());){ byte[] buffer = new byte[1024]; int len; while ((len =input.read(buffer)) != -1) { output.write(buffer, 0, len); } output.flush(); } catch (IOException e) { e.printStackTrace(); } }
Sign in to make a reply
迦梨罩我来学习
private static void copy(String source, String target) { // 检查source是否存在 Path sourcePath =Paths.get(source).normalize().toAbsolutePath(); if (!sourcePath.toFile().isFile()){ throw new IllegalArgumentException(source);
} // 检查target是否存在,不存在创建 Path targetPath =Paths.get(target).normalize().toAbsolutePath();
Path targetParentPath =targetPath.getParent(); if (!targetParentPath.toFile().exists()){
targetParentPath.toFile().mkdirs();
} // 复制 try (InputStream input= new FileInputStream(sourcePath.toString());
OutputStream output = new FileOutputStream(targetPath.toString());){ byte[] buffer = new byte[1024]; int len; while ((len =input.read(buffer)) != -1) {
output.write(buffer, 0, len);
}
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}