Crear Archivos y Directorios
Hola me gustaria saber como crear un directorio desde java y tb como puedo copiar un archivo en un directorio.
MUCHAS GRACIAS DE ANTEMANO.
MUCHAS GRACIAS DE ANTEMANO.
Vamos a ver, ya he buscaro las clases, con esas clases puedo tener una referencia a un archivo, pero no se como crear un directorio.
Buen dia, eh videado tu problema y desarrolle un programita rapido haber si te sirve:
public class DirCopy {
private InputStream in ; //archivo de entrada
private OutputStream out; //archivo de salida
private int len; //posision
byte[] buf = new byte[1024]; //rango de bytes
/** Creates a new instance of DirCopy */
public DirCopy() {
}
//pedimos direccion del archivo a copiar / archivo / la nueva direccion
public void StringDir(String inPath, String inFile, String newPath)throws IOException {
File nameFile = new File(inPath, inFile);
System.out.println(nameFile.getName());//para ver lo que ya hemos copiado
if(nameFile.isDirectory()){//si es un directorio debemos copiar todo lo que estre dentro :)
new File(newPath,nameFile.getName()).mkdir(); //creamos una nueva carpeta
File[] files = nameFile.listFiles(); //recuperamos su profundidad
for(int i=0;i<files.length; i++)
StringDir(files[i].getParent(),files[i].getName(),newPath+"/"+nameFile.getName()); //esto graficamente nos ayuda diferenciar entre un archivo y otro
}else{
Copy(nameFile,newPath,nameFile.getName());
System.out.println(nameFile.getName()+" Ready....");//para ver lo que ya hemos copiado
}
}
public void Copy(File inFile, String outPath, String outFile) throws FileNotFoundException, IOException{
in = new FileInputStream(inFile);
out = new FileOutputStream(outPath+"/"+outFile);
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
public static void main(String[] args) throws IOException {
DirCopy c = new DirCopy();
c.StringDir("/home/cagide","kgd","/home/cagide/laboratorio");
}
}
public class DirCopy {
private InputStream in ; //archivo de entrada
private OutputStream out; //archivo de salida
private int len; //posision
byte[] buf = new byte[1024]; //rango de bytes
/** Creates a new instance of DirCopy */
public DirCopy() {
}
//pedimos direccion del archivo a copiar / archivo / la nueva direccion
public void StringDir(String inPath, String inFile, String newPath)throws IOException {
File nameFile = new File(inPath, inFile);
System.out.println(nameFile.getName());//para ver lo que ya hemos copiado
if(nameFile.isDirectory()){//si es un directorio debemos copiar todo lo que estre dentro :)
new File(newPath,nameFile.getName()).mkdir(); //creamos una nueva carpeta
File[] files = nameFile.listFiles(); //recuperamos su profundidad
for(int i=0;i<files.length; i++)
StringDir(files[i].getParent(),files[i].getName(),newPath+"/"+nameFile.getName()); //esto graficamente nos ayuda diferenciar entre un archivo y otro
}else{
Copy(nameFile,newPath,nameFile.getName());
System.out.println(nameFile.getName()+" Ready....");//para ver lo que ya hemos copiado
}
}
public void Copy(File inFile, String outPath, String outFile) throws FileNotFoundException, IOException{
in = new FileInputStream(inFile);
out = new FileOutputStream(outPath+"/"+outFile);
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
public static void main(String[] args) throws IOException {
DirCopy c = new DirCopy();
c.StringDir("/home/cagide","kgd","/home/cagide/laboratorio");
}
}