Buenas,
Lo mejor es que utilices un SwingWorker para ir actualizando segun copias el fichero. Te pongo un ejemplo para que puedas ejecutarlo y copiar lo que necesites. Esta sacado de aqui:
http://stackoverflow.com/questions/1...ories-and-file
Código Java:
Ver original{
private static final long serialVersionUID = 1L;
private CopyTask task;
public FileCopierUtility()
{
buildGUI();
}
private void buildGUI()
{
setTitle("File Copier Utility");
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
{
@Override
{
if(task != null) task.cancel(true);
dispose();
}
});
progressAll.setStringPainted(true);
progressCurrent.setStringPainted(true);
txtDetails.setEditable(false);
btnCopy.setFocusPainted(false);
btnCopy.setEnabled(false);
btnCopy.addActionListener(this);
{
@Override
{
boolean bEnabled = txtSource.getText().length() > 0 && txtTarget.getText().length() > 0;
btnCopy.setEnabled(bEnabled);
}
@Override
{
boolean bEnabled = txtSource.getText().length() > 0 && txtTarget.getText().length() > 0;
btnCopy.setEnabled(bEnabled);
}
@Override
};
txtSource.getDocument().addDocumentListener(listener);
txtTarget.getDocument().addDocumentListener(listener);
contentPane.
setBorder(BorderFactory.
createEmptyBorder(5,
5,
5,
5));
panProgressLabels.
add(lblProgressCurrent,
BorderLayout.
CENTER);
panControls.
setBorder(BorderFactory.
createEmptyBorder(5,
0,
5,
0));
panProgress.
add(panProgressLabels,
BorderLayout.
LINE_START);
pack();
setLocationRelativeTo(null);
}
@Override
{
if("Copy".equals(btnCopy.getText()))
{
File source
= new File(txtSource.
getText()); File target
= new File(txtTarget.
getText());
if(!source.exists())
{
JOptionPane.
showMessageDialog(this,
"The source file/directory does not exist!",
"ERROR",
JOptionPane.
ERROR_MESSAGE); return;
}
if(!target.exists() && source.isDirectory()) target.mkdirs();
else
{
int option
= JOptionPane.
showConfirmDialog(this,
"The target file/directory already exists, do you want to overwrite it?",
"Overwrite the target",
JOptionPane.
YES_NO_OPTION); }
task = this.new CopyTask(source, target);
task.addPropertyChangeListener(this);
task.execute();
btnCopy.setText("Cancel");
}
else if("Cancel".equals(btnCopy.getText()))
{
task.cancel(true);
btnCopy.setText("Copy");
}
}
@Override
{
if("progress".equals(evt.getPropertyName()))
{
int progress
= (Integer) evt.
getNewValue(); progressAll.setValue(progress);
}
}
public static void main
(String[] args
) {
{
@Override
public void run()
{
new FileCopierUtility().setVisible(true);
}
});
}
class CopyTask
extends SwingWorker
<Void, Integer
> {
private long totalBytes = 0L;
private long copiedBytes = 0L;
public CopyTask
(File source,
File target
) {
this.source = source;
this.target = target;
progressAll.setValue(0);
progressCurrent.setValue(0);
}
@Override
{
txtDetails.append("Retrieving some info ... ");
retrieveTotalBytes(source);
txtDetails.append("Done!\n");
copyFiles(source, target);
return null;
}
@Override
public void process(List<Integer> chunks)
{
for(int i : chunks)
{
progressCurrent.setValue(i);
}
}
@Override
public void done()
{
setProgress(100);
btnCopy.setText("Copy");
}
private void retrieveTotalBytes
(File sourceFile
) {
File[] files
= sourceFile.
listFiles(); {
if(file.isDirectory()) retrieveTotalBytes(file);
else totalBytes += file.length();
}
}
{
if(sourceFile.isDirectory())
{
if(!targetFile.exists()) targetFile.mkdirs();
String[] filePaths
= sourceFile.
list();
for(String filePath
: filePaths
) {
File srcFile
= new File(sourceFile, filePath
); File destFile
= new File(targetFile, filePath
);
copyFiles(srcFile, destFile);
}
}
else
{
txtDetails.append("Copying " + sourceFile.getAbsolutePath() + " ... ");
long fileBytes = sourceFile.length();
long soFar = 0L;
int theByte;
while((theByte = bis.read()) != -1)
{
bos.write(theByte);
setProgress((int) (copiedBytes++ * 100 / totalBytes));
publish((int) (soFar++ * 100 / fileBytes));
}
bis.close();
bos.close();
publish(100);
txtDetails.append("Done!\n");
}
}
}
}
Un saludo