Error moving .rar file from one folder to another

1

I have a method that takes the files I want to .rar from one folder and saves it to another, but when I open the file it is corrupted.

try {
        //Origem
        File arquivoOrigem = new File(path);
        FileReader fis = new FileReader(arquivoOrigem);
        BufferedReader bufferedReader = new BufferedReader(fis);
        StringBuilder buffer = new StringBuilder();
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            buffer.append(line).append("\n");
        }

        fis.close();
        bufferedReader.close();
        //Destino
        File arquivoDestino = new File(frmMenuInicial.caminhoTemporario+"/" + nomeArquivo + ".zip");
        System.err.println("TESTE: "+frmMenuInicial.caminhoTemporario+"/" + nomeArquivo +  ext);
        FileWriter writer = new FileWriter(arquivoDestino);
        writer.write(buffer.toString());
        writer.flush();
        writer.close();
        JOptionPane.showMessageDialog(null, "Arquivo Salvo com Sucesso!\nPasta Destino: "+frmMenuInicial.caminhoTemporario, "Sucesso", JOptionPane.INFORMATION_MESSAGE);
        //Process p;  
        //p = Runtime.getRuntime().exec(path);
    } catch (Exception e) {
        e.printStackTrace();

        JOptionPane.showMessageDialog(null, "Erro ao tentar salvar Arquivo!\nVerifique se o Arquivo ainda exixte.\n"+e, "ERRO!", JOptionPane.ERROR_MESSAGE);
    }
    
asked by anonymous 28.04.2015 / 15:49

1 answer

0

I can suggest several alternatives to the method you're using.

1). From Java 7 you can use NIO2 to copy files.

2). If you can not use this version (or more current) you can still try using the FileUtils class that has been available since version 1.2 of the apache commons-io library.

File source = new File("C:\tempDir\ficheiro1");
File dest = new File("C:\tempDir\ficheiro2");
try {
    FileUtils.copyDirectory(source, dest);
} catch (IOException e) {
    e.printStackTrace();
}

In the last resort, if you really want to write your version I would advise you

public static void copiarFicheiro(File sourceFile, File destinationFile) throws IOException {
    FileChannel inChannel = new FileInputStream(sourceFile).getChannel();
    FileChannel outChannel = new FileOutputStream(destinationFile).getChannel();
    try {
        // inChannel.transferTo(0, inChannel.size(), outChannel); 
        int maxCount = (64 * 1024 * 1024) - (32 * 1024);  --> Para ficheiros de tamanho superior a 64Mb
        long size = inChannel.size();
        long position = 0;
        while (position < size) {
            position += inChannel.transferTo(position, maxCount, outChannel);
        }
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}

A final comment to explain why maxCount: In Windows, sometimes an error can occur when copying files larger than 64Mb, an exception like this is usually thrown: "Exception in thread" main "java.io.IOException: Insufficient system resources exist to complete the requested service is thrown. " To avoid this, you can copy the file in one cycle by copying only 64Mb at a time.

    
28.04.2015 / 19:00