11. februar 2002 - 19:42
Der er
2 kommentarer og
1 løsning
Kopiering af filer
Hej
Hvordan kopierer man filer på en lokal harddisk i et javaprogram fra en sti til en anden?
Selv havde jeg forstillet mig at skulle bruge File-klassen fra java.io.*.
File fraPath;
File tilPath;
Hvordan kopierer jeg en fil fra fraPath til tilPath?? Og er dét overhovedet rigtigt at skulle bruge File?
11. februar 2002 - 20:34
#1
Her er en klasse som flytter et helt directory og alt rekursivt derunder, så kan du også se et åar andre finde ting :)
/*
* dirMove.java
*
* Created on 23. november 2000, 11:13
*/
import java.io.*;
import java.lang.*;
import java.util.*;
/**
*
* @author reinke
* @version
*/
public class dirMove extends Object
{
/** The error code
*/
private int error=0;
File sourceDir,destDir;
public dirMove()
{
}
/** Moves directory from source to dest including everything beneath it
* @param source The filepath to the source directory
* @param dest The filepath to the destination
* @return According to result of the moving of the directory
*/
public boolean move(String source, String dest)
{
//check if either source or dest is empty or null
if ( (source.trim().length()==0) || (source==null) || (dest.trim().length()==0) || (dest==null) )
{
error=1;
return false;
}
sourceDir=new File(source);
if(!sourceDir.isDirectory())
{
error=2;
return false;
}
destDir=new File(dest);
if(destDir.exists())
{
error=3;
return false;
}
if(source.endsWith("/"))
{
source=source.substring(0,source.length()-1);
}
if(dest.endsWith("/"))
{
dest=dest.substring(0,dest.length()-1);
}
//some error has happened
if(!moveIt(source,dest))
{
deleteDirs(dest);
}
return true;
}
public void deleteDirs(String dest)
{
File dFile=new File(dest);
File files[]=scanDir(dFile);
if(files.length!=0)
{
for(int x=0;x<files.length;x++)
{
String name=files[x].getName();
if(files[x].isDirectory())
{
deleteDirs(dest+"/"+name);
files[x].delete();
}
else
{
files[x].delete();
}
}
}
else
{
dFile.delete();
}
}
private boolean moveIt(String source, String dest)
{
File sFile=new File(source);
File dFile=new File(dest);
if(!dFile.mkdirs())
{
error=4;
return false;
}
File files[]=scanDir(sFile);
if(files.length!=0)
{
for(int x=0;x<files.length;x++)
{
String name=files[x].getName();
if(files[x].isDirectory())
{
moveIt(source+"/"+name,dest+"/"+name);
}
else
{
if(!copyFile(source+"/"+name,dest+"/"+name))
{
error=5;
return false;
}
}
}
}
return true;
}
/**
* @param dir
* @return */
private File [] scanDir(File dir)
{
return dir.listFiles();
}
private boolean copyFile(String source, String dest)
{
try
{
// FileInputStream in=new FileInputStream(new File(source));
// FileOutputStream out=new FileOutputStream(new File(dest));
BufferedInputStream in=new BufferedInputStream(new FileInputStream(new File(source)));
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(new File(dest)));
int data;
while((data=in.read())!=-1)
{
out.write(data);
}
in.close();
out.close();
}
catch (IOException e)
{
return false;
}
return true;
}
/** Returns the error text according to the value of 'error'
* @return String containing the Error Text according to the value of {@link error}
*/
public String getError()
{
if(error==1) return "source eller dest er tom";
if(error==2) return "source er ikke en directory";
if(error==3) return "destination findes allerede!!!";
if(error==4) return "Could not create Directory";
if(error==5) return "File copy failed";
return "";
}
}