java问题求帮忙
答案:1 悬赏:40 手机版
解决时间 2021-01-08 16:15
- 提问者网友:那叫心脏的地方装的都是你
- 2021-01-08 12:09
java问题求帮忙
最佳答案
- 五星知识达人网友:蓝房子
- 2021-01-08 12:29
final static void showAllFiles(File dir) throws Exception {
File[] fs = dir.listFiles();
for (int i = 0; i < fs.length; i++) {
System.out.println(fs[i].getAbsolutePath());
if (fs[i].isDirectory()) {
try {
showAllFiles(fs[i]);
} catch (Exception e) {
}
}
}
}
private void copyFolder(File src, File dest) throws IOException {
if (src.isDirectory()) {
if (!dest.exists()) {
dest.mkdir();
}
String files[] = src.list();
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
// 递归复制
copyFolder(srcFile, destFile);
}
} else {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
}
第一个:打印
第二个:拷贝
使用:
File root = new File("E:\\fileName");
showAllFiles(root);//第二个同第一个差不多用法
记得抛出异常(或用try catch)
File[] fs = dir.listFiles();
for (int i = 0; i < fs.length; i++) {
System.out.println(fs[i].getAbsolutePath());
if (fs[i].isDirectory()) {
try {
showAllFiles(fs[i]);
} catch (Exception e) {
}
}
}
}
private void copyFolder(File src, File dest) throws IOException {
if (src.isDirectory()) {
if (!dest.exists()) {
dest.mkdir();
}
String files[] = src.list();
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
// 递归复制
copyFolder(srcFile, destFile);
}
} else {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
}
第一个:打印
第二个:拷贝
使用:
File root = new File("E:\\fileName");
showAllFiles(root);//第二个同第一个差不多用法
记得抛出异常(或用try catch)
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯