Copiar un fichero en otro
Hola, quiero copiar un fichero en otro. El problema es que cuando llamo a ese método siempre se lanza la FileNotFoundException y los ficheros están creados y en su lugar adecuado, asà que no entiendo que es lo que pasa. A continuación está el código. Gracias y un saludo
public void Copia( String origen, String destino ) throws IOException
{
FileReader fr=null;
FileWriter fw=null;
try
{
fr=new FileReader( origen );
fw=new FileWriter( destino );
}
catch ( FileNotFoundException fnfe )
{
System.err.println( " No se ha encontrado el fichero " );
}
catch ( SecurityException se )
{
System.err.println( " No tiene permiso de lectura y/o escritura " );
}
BufferedReader br=new BufferedReader( fr );
BufferedWriter bw=new BufferedWriter( fw );
String cadena;
while ( br.readLine()!=null )
{
cadena=br.readLine();
bw.write(cadena);
}
br.close();
bw.close();
}
public void Copia( String origen, String destino ) throws IOException
{
FileReader fr=null;
FileWriter fw=null;
try
{
fr=new FileReader( origen );
fw=new FileWriter( destino );
}
catch ( FileNotFoundException fnfe )
{
System.err.println( " No se ha encontrado el fichero " );
}
catch ( SecurityException se )
{
System.err.println( " No tiene permiso de lectura y/o escritura " );
}
BufferedReader br=new BufferedReader( fr );
BufferedWriter bw=new BufferedWriter( fw );
String cadena;
while ( br.readLine()!=null )
{
cadena=br.readLine();
bw.write(cadena);
}
br.close();
bw.close();
}
Prueba con este codigo
void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
