Error while moving File . rar from one folder to another

Asked

Viewed 190 times

1

Guys I have a method that takes the files I want . rar from one folder and saved in another, however 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);
    }
  • What version of Java?

1 answer

0


May I suggest several alternatives to the method you are using.

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

2). If you cannot use this version (or more current) you can still try using the Fileutils class that has been available since apache version 1.2.

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

As a last resort, if you really want to write your version I would advise

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 occurs when copying files greater than 64Mb, usually an exception like "Exception in thread "main" java.io.Ioexception: Insufficient system Resources exist to complete the requested service is thrown." To avoid this, it is possible to copy the file in a cycle by copying only 64Mb at a time.

  • Thank you very much man, I used the Fileutils Class and it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.