
java问题
编写程序,拷贝一个带内容的文件夹。例如:将c:\programfiles\java文件夹拷贝到d盘根目录求可运行代码...
编写程序,拷贝一个带内容的文件夹。 例如:将c:\program files\java 文件夹拷贝到d盘根目录
求可运行 代码 展开
求可运行 代码 展开
2个回答
展开全部
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Test{
static public boolean copyFile(String src, String dest,StringBuffer msg){
System.out.println(src+" => "+dest);
File src_file=new File(src);
if(!src_file.exists()){
msg.append("原文件夹"+src+"不存在\n");
return false;
}
if(src_file.isDirectory())
{
File dest_file=new File(dest);
if(dest_file.exists()){
if(!dest_file.isDirectory()){
msg.append(dest+"已存在且不是目录,无法开始复制");
return false;
}
}else{
msg.append("目标文件夹"+dest+"不存在");
if(dest_file.mkdirs()){
msg.append(" 已成功创建\n");
}else{
msg.append(" 错误:创建失败");
return false;
}
}
String file_list[]=src_file.list();
for(String fname:file_list){
copyFile(src_file.getAbsolutePath()+File.separatorChar+fname, dest+File.separatorChar+fname,msg);
}
}else{
File dest_file=new File(dest);
if(dest_file.exists()){
msg.append(dest+"已存在,不覆盖,放弃操作");
return false;
}
try {
dest_file.createNewFile();
FileOutputStream fos=new FileOutputStream(dest_file);
FileInputStream fis=new FileInputStream(src_file);
byte buffer[]=new byte[1024*1024]; //1MB的缓冲区
int count;
while((count=fis.read(buffer))!=-1){
fos.write(buffer, 0, count);
}
fis.close();
fos.close();
System.out.println("复制成功");
} catch (Exception e) {
msg.append("复制时出现未知错误");
e.printStackTrace();
}
}
return true;
}
public static void main(String[] args) {
String
//src="F:\\download\\___JAVA_backup\\a",
src="C:\\program files\\java",
//dest="F:\\download\\___JAVA_backup\\b";
dest="D:\\";
StringBuffer msg=new StringBuffer();
if(!copyFile(src, dest, msg)){
System.out.println(msg);
}
}
}
==============
输出
F:\download\___JAVA_backup\a => F:\download\___JAVA_backup\b
F:\download\___JAVA_backup\a\ac.h => F:\download\___JAVA_backup\b\ac.h
复制成功
F:\download\___JAVA_backup\a\acdemo.c => F:\download\___JAVA_backup\b\acdemo.c
复制成功
F:\download\___JAVA_backup\a\c => F:\download\___JAVA_backup\b\c
F:\download\___JAVA_backup\a\c\ac.c => F:\download\___JAVA_backup\b\c\ac.c
复制成功
F:\download\___JAVA_backup\a\c\ac.txt => F:\download\___JAVA_backup\b\c\ac.txt
复制成功
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Test{
static public boolean copyFile(String src, String dest,StringBuffer msg){
System.out.println(src+" => "+dest);
File src_file=new File(src);
if(!src_file.exists()){
msg.append("原文件夹"+src+"不存在\n");
return false;
}
if(src_file.isDirectory())
{
File dest_file=new File(dest);
if(dest_file.exists()){
if(!dest_file.isDirectory()){
msg.append(dest+"已存在且不是目录,无法开始复制");
return false;
}
}else{
msg.append("目标文件夹"+dest+"不存在");
if(dest_file.mkdirs()){
msg.append(" 已成功创建\n");
}else{
msg.append(" 错误:创建失败");
return false;
}
}
String file_list[]=src_file.list();
for(String fname:file_list){
copyFile(src_file.getAbsolutePath()+File.separatorChar+fname, dest+File.separatorChar+fname,msg);
}
}else{
File dest_file=new File(dest);
if(dest_file.exists()){
msg.append(dest+"已存在,不覆盖,放弃操作");
return false;
}
try {
dest_file.createNewFile();
FileOutputStream fos=new FileOutputStream(dest_file);
FileInputStream fis=new FileInputStream(src_file);
byte buffer[]=new byte[1024*1024]; //1MB的缓冲区
int count;
while((count=fis.read(buffer))!=-1){
fos.write(buffer, 0, count);
}
fis.close();
fos.close();
System.out.println("复制成功");
} catch (Exception e) {
msg.append("复制时出现未知错误");
e.printStackTrace();
}
}
return true;
}
public static void main(String[] args) {
String
//src="F:\\download\\___JAVA_backup\\a",
src="C:\\program files\\java",
//dest="F:\\download\\___JAVA_backup\\b";
dest="D:\\";
StringBuffer msg=new StringBuffer();
if(!copyFile(src, dest, msg)){
System.out.println(msg);
}
}
}
==============
输出
F:\download\___JAVA_backup\a => F:\download\___JAVA_backup\b
F:\download\___JAVA_backup\a\ac.h => F:\download\___JAVA_backup\b\ac.h
复制成功
F:\download\___JAVA_backup\a\acdemo.c => F:\download\___JAVA_backup\b\acdemo.c
复制成功
F:\download\___JAVA_backup\a\c => F:\download\___JAVA_backup\b\c
F:\download\___JAVA_backup\a\c\ac.c => F:\download\___JAVA_backup\b\c\ac.c
复制成功
F:\download\___JAVA_backup\a\c\ac.txt => F:\download\___JAVA_backup\b\c\ac.txt
复制成功
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用了apache common io的包
注意导包
public static void main(String[] agrs) {
File file = new File("c:/rogram files/java") ;
File toFile = new File("d:/") ;
try {
FileUtils.copyDirectoryToDirectory(file, toFile) ;
FileUtils.deleteDirectory(file) ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
注意导包
public static void main(String[] agrs) {
File file = new File("c:/rogram files/java") ;
File toFile = new File("d:/") ;
try {
FileUtils.copyDirectoryToDirectory(file, toFile) ;
FileUtils.deleteDirectory(file) ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
追问
FileUtils cannot be resolved 提示这个我该怎么做?谢谢
追答
使用了apache common io的包
注意导包
我在答案的第一句就说明了 大兄弟
你去下载个common-io的jar包导入到工程里
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询