代码后续上来,就是我用递归扫了一边个人U盘,然后打算把扫到的东西放进一个新建的文件夹,不知道为什么老报错。请大佬指教!代码:
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
public class File1 {
public static void main(String[] args) throws Exception {
//创建File对象。传入路径
File f = new File("G:\\");
try {
showfile(f);//调用静态showfile方法
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("长度为:" + f.length());
// System.out.println("路径:"+f.getPath());
// System.out.println("父路径:"+f.getParentFile());
}
public static void showfile(File f) throws Exception {
//判断目录是否为空
if (f != null) {
//打印目录名
System.out.println(f.getName());
//判断是否为目录
if (f.isDirectory()) {
File fs1;//定义fs1变量
File[] fs = f.listFiles();//fs数组接收
if (fs != null) {//判断目录是否为空
for (int i = 0; i < fs.length; i++) {
fs1 = fs[i];//接收递归后数据
showfile(fs1);//传入showfile方法
//定义集合
ArrayList<File> al = new ArrayList<File>();
//把递归扫到的数据丢进集合
al.add(fs1);
//创建新建目录路径
File ar = new File("G:\\TestAR");
//创建目录
ar.mkdir();
//创建输出流
FileOutputStream out = null;
try {
//创建输出路径
out = new FileOutputStream("G:\\TestAR");
//创建准备输出的代码
File fs2 = fs1;
byte bs[] = fs2.listFiles();
out.write(bs, 0, 60000);
System.out.println("Test");
} catch (Exception e) finally {
if (ar != null) {
out.close();
}
}
}
}
}
}
}
}
3 回答
风洛洛
TA贡献11条经验 获得超2个赞
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
public class File1 {
public static void main(String[] args) throws Exception {
//创建File对象。传入路径
File f = new File("E:\\test");
try {
showfile(f);//调用静态showfile方法
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("长度为:" + f.length());
// System.out.println("路径:"+f.getPath());
// System.out.println("父路径:"+f.getParentFile());
}
public static void showfile(File f) throws Exception {
//判断目录是否为空
//你要想连目录都要遍历,那必须得去除掉之前的根路径,再在新的路径下创建 , 我简单做处理
if (f != null) {
//打印目录名
System.out.println(f.getAbsolutePath());
//判断是否为目录
if (f.isDirectory()) {
File fs1;//定义fs1变量
File[] fs = f.listFiles();//fs数组接收
if (fs != null) {//判断目录是否为空
for (int i = 0; i < fs.length; i++) {
fs1 = fs[i];//接收递归后数据
showfile(fs1);//传入showfile方法
//定义集合
//你的这个集合完全没有意义
ArrayList<File> al = new ArrayList<File>();
//把递归扫到的数据丢进集合
al.add(fs1);
//创建新建目录路径
File ar = new File("E:\\TestAR");
//这里你应该先判断下,目录是否存在
if(!ar.exists()){
//不存在 创建目录
ar.mkdir();
}
//得先判断 文件是否是目录,是目录直接在新的目录下创建目录就行
if(fs1.isDirectory()){
File newDir = new File("E:\\TestAR\\"+fs1.getAbsolutePath().substring("E:\\test".length()));
if(!newDir.exists()){
newDir.mkdir();
}
}else{
//如果他的根路径不存在 , 先创建目录
String dirStr = "E:\\TestAR\\"+fs1.getAbsolutePath().substring("E:\\test".length());
dirStr = dirStr.substring(0,dirStr.length() - fs1.getName().length());
File dir = new File(dirStr);
if(!dir.exists()){
dir.mkdir();
}
//先创建文件
File file = new File("E:\\TestAR\\"+fs1.getAbsolutePath().substring("E:\\test".length()));
if(!file.exists()){
file.createNewFile();
}
//创建输出流
FileOutputStream out = null;
FileInputStream in = null;
try {
//创建输出路径
out = new FileOutputStream(file);
//创建准备输出的代码
File fs2 = fs1;
//这里的
in = new FileInputStream(fs2);
//没理解你这里要干嘛。。。
// File bs[] = fs2.listFiles();
byte[] bs = new byte[2048];
while(in.read(bs)!=-1){
out.write(bs);
bs = new byte[2048];
}
// out.write(bs, 0, 60000);
// System.out.println("Test");
} catch (Exception e){
//这里之前少这块 , 先直接堆栈打出来吧
e.printStackTrace();
}finally {
// 这里应该判断流是否存在 并关闭
// if (ar != null) {
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
}
}
}
}
}
}
}这里 多说两句。。。 我这是在你那原基础上改的。。。所以有点乱,怕重新设计了你又看不懂了。
这里给点建议:
名字起得有意义些,我改的时候感觉根本不知道命名是什么意思
写代码最起码编译得过,而且,设计时考虑全面些= =你之前代码没有考虑文件夹里面有文件夹怎么办
建议你再看下io那里
添加回答
举报
0/150
提交
取消
