0

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

3 Answers3

1

I found in https://docs.oracle.com/javase/8/docs/api/:

public abstract long transferFrom(ReadableByteChannel src,
                              long position,
                              long count)
                       throws IOException- 

"...This method does not modify this channel's position..."

Maybe that is why you see the same value (I didn't check it).

Try to replace:

 rec = outp.position();

by

rec = inp.position();
olsli
  • 831
  • 6
  • 8
0

It is because you first copy the whole file(outp.transferFrom(inp,0,size);), and then starts timer - while work is done already. Try to start timer prior actual data transfer.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

The transferFrom method is not executed asynchronly. It is already finished when your timer starts.

Consider to move the try..catch part (file transfer) to an own thread, start first the timer and then the transfer thread.

And BTW: a 100 ms interval is much too fast for a GUI. Incrementing it all seconds or half seconds is more than enough.

Heri
  • 4,368
  • 1
  • 31
  • 51