哪位大侠帮忙看看下面的Java代码问题出在哪里?谢谢

2024-11-05 21:36:23
推荐回答(2个)
回答(1):

public void copyFloderFile(String frompath, String topath) {
// TODO Auto-generated method stub
File fromFile=new File(frompath);
File toFile=new File(topath);
InputStream to=null;
OutputStream fo=null;
if (fromFile.isDirectory()){
try
{
(new File(topath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
File a=new File(frompath);
String[] file=a.list();
File temp=null;
for (int i = 0; i < file.length; i++)
{
if(frompath.endsWith(File.separator))
{
temp=new File(frompath+file[i]);
}
else
{
temp=new File(frompath+File.separator+file[i]);
}
if(temp.isFile())
{
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(topath + "/" +
(temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
try {
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
output.flush();
output.close();
input.close();
}
}
if(temp.isDirectory())
{
//如果是子文件夹
copyFloderFile(frompath+"/"+file[i],topath+"/"+file[i]);
}
}
System.out.println("复制文件夹操作 成功执行");
}
catch (Exception e)
{
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}

}else{
try {
to=new FileInputStream(fromFile);
fo=new FileOutputStream(toFile);
int readByte=0;
while ((readByte=to.read())!=-1) {
fo.write(readByte);
fo.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(to!=null){
to.close();
}
if(fo!=null){
fo.close();
}
System.out.println("复制成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

回答(2):

不会