Greetings to all, this is a doubt that maybe I can not explain enough.
It turns out that I'm doing as a practice a program that makes backup copies. Then I have the path source
(where the files to save come from) and the path target
(where they are going to stop).
My question is this, I have this:
String source="C:/origen";
String target="C:/destino";
I want to copy the contents of this folder:
C:/origen/carpeta1/
that we imagine has a file named documento.txt
I need to somehow transform C:/origen/carpeta1/documento.txt
into C:/destino/carpeta1/documento.txt
, changing only the source
fragment to the target
and keeping the rest of the route intact.
To keep in mind that in my program I first create the carpeta1
and then look for if it has content. This method has to be recursive, that is, it can be applied in any situation, no matter how many folders the path contains.
Here I leave my method (not a big deal, any advice I could use):
public static void checkNewFiles(){
if(source.listFiles().length!=target.listFiles().length){
//Cantidad de ficheros ha variado
//Si hay mas ficheros en la carpeta de origen copialos a la carpeta target
if(source.listFiles().length>target.listFiles().length){
//Copia los ficheros de más de la carpeta origen a la carpeta destino
for(File a: source.listFiles()){
//Crea una abstraccion del fichero con el path de la carpeta source mas el nombre del fichero
File toCopy= new File(target.getAbsoluteFile()+File.separator+a.getName());
if(a.isDirectory()){
toCopy.mkdir();
//Si la carpeta tiene contenido
/*
* CREAR MÉTODO QUE PUEDA COPIAR EL CONTENIDO DE UNA CARPETA ESPECIFICA
*/
copyFolderContent(a);
}else{
try {
toCopy.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}else{
// Si hay mas ficheros en la carpeta destino haz esto
for(int i=target.listFiles().length;i<source.listFiles().length;i++){
}
}
}
}
The method to create in question would be copyFolderContent(a)
, where a
is the folder to copy.