如何用JAVA实现根据文件后缀名分类文件,并且将文件复制到不同的文件夹 如何用JAVA实现根据文件后缀名分类文件,并且将文件复制到不同的文件夹 求完整代码!!谢谢大家了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 判断文件后缀名可以用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 MYSQL不定时出现的问题 字符串怎么实现类似这样的乘法? 如何通过java来实现用word读取html文件? 请教applet与servlet通信的问题 【求一个简单事件例子】(马上给分) 为什么不允许创建监听者界面? 搞java安全者请进或搞安全者 用JMF来播放avi文件,图象不连续,效果很差,这是什么原因??? 人分数全给第十个回复的人 提问:用Corba作开发都需要准备什么。 我现在用J2SE做一个Windows系统运行的软件,该软件显示大照片有时会内存溢出。哪位高手知道如何处理? arraylist泛型声明括号中数字的含义
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