/**
* Delete the files in the specified folder.
* @param strPath
*/
public static boolean delFileInFolder(String strPath)
{
File f = new File(strPath);
if((f.exists()) && ( f.isDirectory()))
{
Vector v = new Vector();
getFilesRecursive(f,v);
for(int i=0;i<v.size();i++)
{
String strDelPath = (String)v.elementAt(i);
System.out.println("Delete file:" + strDelPath);
try
{
File fDel = new File(strDelPath);
if( fDel.exists() && fDel.isFile() )
fDel.delete();
}
catch(Exception e)
{
System.out.println("Can not delete file:" + strDelPath);
return false;
}
}
return true;
}
else
{
System.out.println("Can not delete files in " + strPath);
return false;
}
}
* Delete the files in the specified folder.
* @param strPath
*/
public static boolean delFileInFolder(String strPath)
{
File f = new File(strPath);
if((f.exists()) && ( f.isDirectory()))
{
Vector v = new Vector();
getFilesRecursive(f,v);
for(int i=0;i<v.size();i++)
{
String strDelPath = (String)v.elementAt(i);
System.out.println("Delete file:" + strDelPath);
try
{
File fDel = new File(strDelPath);
if( fDel.exists() && fDel.isFile() )
fDel.delete();
}
catch(Exception e)
{
System.out.println("Can not delete file:" + strDelPath);
return false;
}
}
return true;
}
else
{
System.out.println("Can not delete files in " + strPath);
return false;
}
}
* Get the all the filenames with absolute path
* All the files fullpath will be store in vector
* @return the path name
*/
public static void getFilesRecursive(File dir,Vector v)
{
if(dir.isDirectory())
{
String path = dir.getAbsolutePath();
String sub[] = dir.list();
for(int i = 0;i<sub.length;i++)
{
File f = new File(path + File.separator + sub[i]);
getFilesRecursive(f,v);
}
}
else
{ String tmp = dir.getAbsolutePath();
v.add(tmp);
//System.out.println(tmp);
} }
你的方法可以列出所有的文件,但这不是问题的所在了,我想要得是一个copy的方法。
难道我只有每个文件都打开和写入吗?
public void copyallfile(String path1,String path2) throws IOException
{
File f=new File(path1);
if(f.isDirectory() ){
File files[]=f.listFiles() ;
for (int i = 0; i < files.length; i++) {
FileInputStream fi = new FileInputStream(files[i].toString());
FileOutputStream fo = new FileOutputStream(path2+"\\"+files[i].getName());
byte date[] = new byte[fi.available()];
fi.read(date);
fo.write(date);
fi.close();
fo.close();
}
} 俺测试过的!
public static void copyallfile(String path1, String path2)
throws IOException {
File f = new File(path1);
if (f.isDirectory()) {
File files[] = f.listFiles();
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()) {
FileInputStream fi =
new FileInputStream(files[i].toString());
FileOutputStream fo =
new FileOutputStream(path2 + "\\" + files[i].getName());
byte date[] = new byte[fi.available()];
fi.read(date);
fo.write(date);
fi.close();
fo.close();
} else {
String str = path2 + "\\" + files[i].getName();
File diro = new File(str);
if (!diro.exists()) {
diro.mkdir();
}
copyallfile(files[i].getPath(), str);
}
} }
}