日期:2014-05-20  浏览次数:20904 次

java如何批量读取文件
如何实现把一个目录的文件全读出来,然后把所有文件移到另外一个目录。

------解决方案--------------------
帮顶
------解决方案--------------------
嵌入批处理命令吧,要是你只想移动文件的话
------解决方案--------------------
关注
------解决方案--------------------
/**
* Copy directories or files or together with files to another directory.
* @param from it 's a file name or directory name.
* @param to it 's a directory name.
* @throws IOException the IOException.
*/
public void copy(File from, File to) throws IOException {
if (from.isDirectory()) {
to.mkdir();
File[] flist = from.listFiles();
for (int i = 0; i < flist.length; i++) {
if (flist[i].isFile())
copy(flist[i], to);
else
copy(flist[i], new File(to.getPath() + File.separator
+ flist[i].getName()));
}
} else {
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(from);
FileOutputStream fos = new FileOutputStream(to.getPath()
+ File.separator + from.getName());
int length = 0;
while ((length = fis.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
fis.close();
fos.close();
}
}