Dúvida de como transferir aquivos

2 respostas
Robson_j_s_Diniz

Olá pessoal, será que alguém poderia me ajudar explicando como faço para transferir arquivos de um diretório a outro. O tipo de arquivo não importa pode ser qualquer arquivo que desejar. Tipo, ‘pego’ um arquivo em um diretório e transfiro ao outro. Obrigado.

2 Respostas

Felagund

Cara não sei se é a melhor maneira, mas você pode abrir o arquivo e gravar ele no novo lugar e excluir do antigo (caso queira exclui-lo). Pois nunca vi nada de copiar (recortar) e colar no java. posso estar enganado.

dedspr

Tente este exemplo aqui…

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

/**
 *
 * @author Douglas Emanoel
 */
public class CopiarArquivo {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            String pathArquivo = "c:\\cursor.png";
            String novoPathArquivo = "c:\\cursor-copy.png";
            File arquivoFile = new File(pathArquivo);
            bis = new BufferedInputStream(new FileInputStream(pathArquivo));
            bos = new BufferedOutputStream(new FileOutputStream(novoPathArquivo));
            
            //TAMANHO DO BUFFER PARA LER O ARQUIVO POR VEZ
            byte[] buffer = new byte[30000];
            int nBytesLidos;
            
            while (true) {
                nBytesLidos = bis.read(buffer, 0, buffer.length);
                
                if (nBytesLidos <= 0) {
                    break;
                    //this.destroy();
                }
                
                System.out.println("Copiando "+nBytesLidos+" bytes de "+(int) arquivoFile.length()+" bytes...");
                bos.write(buffer, 0, nBytesLidos);
            }
        } catch (IOException ex) {
            //
        } finally {
            if (bis != null) try { bis.close(); } catch (IOException ex) {}
            if (bos != null) try { bos.close(); } catch (IOException ex) {}
        }
    }
    
}
Criado 10 de setembro de 2008
Ultima resposta 11 de set. de 2008
Respostas 2
Participantes 3