I have a class that copies one file from one folder to another :
public class Foo extends JFrame{
Timer t;
FileChannel inp = null,
outp= null;
File sourceFile = new File("C:/movies/movie.mkv"),
destFile = new File("C:/test/movie.mkv");
long rec = 0;
long size;
LayoutManager manager = new MigLayout();
public void createUI(){
JPanel panel = new JPanel();
JButton copyFile = new JButton("Copy file");
JButton btn = new JButton("Start function");
JButton stop = new JButton("Stop function");
panel.setLayout(manager);
panel.add(btn);
panel.add(copyFile,"wrap");
panel.add(stop);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setContentPane(panel);
this.setVisible(true);
this.setSize(400,400);
this.pack();
btn.addActionListener((e)->{
try {
inp = new FileInputStream(sourceFile).getChannel();
outp = new FileOutputStream(destFile).getChannel();
size = inp.size();
outp.transferFrom(inp,0,size);
} catch (Exception e2) {
// TODO Auto-generated catch block
System.out.println("FIle not found");
return;
}
t = new Timer(100,i->{
try {
rec = outp.position();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
rec = 0;
}
finally{
System.out.println("Position in file:"+rec);
}
});
t.start();
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(()->new Foo().createUI());
}
}
Now I have a Swing timer that every 100ms outputs the position in File.
What I am actually trying to do is to let the user know how much of the file has been copied.The problem is that every 100ms the number that is outputed in my console is "Position in file:126089692".
I would like an explanation if possible.
Thanks for your time