public class copytest
{
static void copy(File f)throws IOException
{
FileOutputStream fos=null;
FileInputStream fis=null;
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);
byte b[]=new byte[10000];
String list[];
File fe;
//begin
list=f.list();
for(int i=0;i<list.length;i++)
{
if(list[i].indexOf('.')!=-1)
{
System.out.println(f.getPath()+list[i]);
if(list[i].endsWith(".txt"))
{
fe=new File("e:\\newcopy\\"+f.getPath().substring(3));
fe.mkdirs();
fis=new FileInputStream(f.getPath()+"\\"+list[i]);
fos=new FileOutputStream("e:\\newcopy\\"+f.getPath().substring(3)+"\\"+list[i]);
bis=new BufferedInputStream(fis);
bos=new BufferedOutputStream(fos);
while(bis.available()>0)
{
bis.read(b,0,9000);
bos.write(b,0,9000);
}
bos.flush();
bos.close();
bis.close();
}
}
else
{
String name=new String(f.getPath()+"\\"+list[i]);
File f2=new File(name);
copy(f2);
}
}
}
public static void main(String args[])throws IOException
{
File fi=new File("G:\\");
System.out.println(fi.getPath());
boolean test=true;
while(test)
{
if(fi.exists())
{
test=false;
copy(fi);
}
}
}
}这个程序只能复制英文小写或者英文大小写混合的文件夹或者文件,中文和英文大写的都不行。谢谢各位高手
{
static void copy(File f)throws IOException
{
FileOutputStream fos=null;
FileInputStream fis=null;
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);
byte b[]=new byte[10000];
String list[];
File fe;
//begin
list=f.list();
for(int i=0;i<list.length;i++)
{
if(list[i].indexOf('.')!=-1)
{
System.out.println(f.getPath()+list[i]);
if(list[i].endsWith(".txt"))
{
fe=new File("e:\\newcopy\\"+f.getPath().substring(3));
fe.mkdirs();
fis=new FileInputStream(f.getPath()+"\\"+list[i]);
fos=new FileOutputStream("e:\\newcopy\\"+f.getPath().substring(3)+"\\"+list[i]);
bis=new BufferedInputStream(fis);
bos=new BufferedOutputStream(fos);
while(bis.available()>0)
{
bis.read(b,0,9000);
bos.write(b,0,9000);
}
bos.flush();
bos.close();
bis.close();
}
}
else
{
String name=new String(f.getPath()+"\\"+list[i]);
File f2=new File(name);
copy(f2);
}
}
}
public static void main(String args[])throws IOException
{
File fi=new File("G:\\");
System.out.println(fi.getPath());
boolean test=true;
while(test)
{
if(fi.exists())
{
test=false;
copy(fi);
}
}
}
}这个程序只能复制英文小写或者英文大小写混合的文件夹或者文件,中文和英文大写的都不行。谢谢各位高手
FileInputStream fis=null; ////this null
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);
实际上,设计思路应该是这样的:
首先,完成单个文件的复制功能。方法名可以为void copy(File src,File des);
其次,完成单个文件夹中文件的复制功能。实际上,就是遍历文件同时调用上述的方法(copy方法)。
该步骤的方法名,可以为void dirCopy(File srcFolder,File desFolder);
最后,考虑该程序是否要支持递归遍历的情况。
也就是文件夹中仍有文件夹的情况,是否依然将其文件进行复制。
如果需求要求考虑递归遍历,那么,由于上一步骤已经有文件的处理方法了,只需递归处理文件夹就行了。
该步骤的方法名,可以为void xCopy(File srcFolder,File desFolder);
大致写一下方法体的代码,仅供参考。
{
dirCopy(srcFolder,desFolder);
File folders[] = srcFolder.listFiles(new FileFilter(){
public boolean accept(File file){
return file.isDirectory();
}
});
for(File folder: folders){
String name = folder.getName();
String folderName = desFolder.getAbsolutePath()+File.separator+name;
xCopy(folder,new File(folderName));
}
}