如何用JAVA实现根据文件后缀名分类文件,并且将文件复制到不同的文件夹
如何用JAVA实现根据文件后缀名分类文件,并且将文件复制到不同的文件夹 求完整代码!!谢谢大家了 --------------------编程问答-------------------- 判断文件后缀名可以用
File file = new File("路径");
if (file.getName().toLowerCase().endsWith(".txt")) {
//复制操作
}
文件复制操作如下
--------------------编程问答--------------------
public static boolean copy(File fileFrom, File fileTo) {
try {
FileInputStream in = new java.io.FileInputStream(fileFrom);
FileOutputStream out = new FileOutputStream(fileTo);
byte[] bt = new byte[1024];
int count;
while ((count = in.read(bt)) > 0) {
out.write(bt, 0, count);
}
in.close();
out.close();
return true;
} catch (IOException ex) {
return false;
}
}
++ --------------------编程问答--------------------
Path dir = ...;
Path target = ...;
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{c,h,cpp,hpp,java}")) {
for (Path entry: stream) {
Files.copy(entry,target.resolve(entry.getFileName()));
}
} catch (DirectoryIteratorException ex) {
// I/O error encounted during the iteration, the cause is an IOException
throw ex.getCause();
}
需要 jdk7 --------------------编程问答-------------------- 如果是word、excel,拷贝是不是会复杂些? --------------------编程问答--------------------
不会,都按二进制文件读写
补充:Java , Java SE