• Revista PROGRAMAR: Já está disponível a edição #53 da revista programar. Faz já o download aqui!

Sign in to follow this  
Followers 0

[Java] Copiar Ficheiros

1 post in this topic

Um simples método que permite copiar um ficheiro de forma binária.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;

public void Copiar(String src, String dst){

try {
   BufferedInputStream in = new BufferedInputStream( new FileInputStream( new File(src) ) );
   BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream( new File(dst) ) );

   byte[] buf = new byte[1024];
   int lidos = 0;

   while( ( lidos = in.read(buf) ) !=-1 ){
       out.write(buf, 0, numRead);


   catch(FileNotFoundException fnf){
       System.out.println("Ficheiro não encontrado : "+ fnf.getMessage());
   catch(IOException io){
       System.out.println("Erro ao ler/escrever no ficheiro : "+io.getMessage());
   catch(Exception e){


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0