Skip to main content
gamedev.net
🔒 Locked

[java] How to copy files

Started by Mr Coder Jun 28, 2004 at 8:36 AM 4 replies 1.4k views
Original Post
Mr Coder
Mr Coder
My question is: how to copy files with Java? Also, I would like to know if there is any method to check if a frame is open or not. Thanks.
BobV
BobV
The frame question depends on how you implement your frames. Either you can load them in the background at the start of the application and then manipulate their visible property. in that case you can use .isVisible(). If you are creating on the fly, you could check to see if the frame is null.

As for copying files.

Using java.nio:

try {
// Create channel on the source
FileChannel srcChannel = new FileInputStream("srcFilename").getChannel();

// Create channel on the destination
FileChannel dstChannel = new FileOutputStream("dstFilename").getChannel();

// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

// Close the channels
srcChannel.close();
dstChannel.close();
} catch (IOException e) {
}


You should also get familiar with the file class.
Check out the api at http://java.sun.com/j2se/1.4.2/docs/api/
Mr Coder
Mr Coder
Thanks for the answer. Before I asked about Frames,
but here is my problem:
I have this code:



LoadWindow lw = new LoadWindow();

System.out.println("Hello World!!!");



So, LoadWindow is a class that creates a Window
(through a frame, using buttons, textfields...).
The constructor build and show the window.
The problem is, I would like to execute that
System.out.println() instruction only when I press
the Ok button on the LoadWindow.
Actually, instead, it's executed immediatly
after the Window is shown. I know I could implement
that instrutction inside the ActionPerformed() of
the LoadWindow class, but I need to know if things
could work in this setting (and how ;) .
Koroljov
Koroljov
You can use Window.isShowing() to see if a frame is open or not.
You could do something like this:

while(lw.isShowing()){Thread.sleep(100);}

to wait till the window is closed.
Why do my programs never work on other computers?
BobV
BobV
Yea, what Koro said is pretty much the way to do it. Though, depending on how you are implementing, this could be of poor design. You are locking up a thread to wait for specified user input. That doesn't sit well with an event driven model.

I'm guessing that you are doing this from the main method for something like a user authentication before the application kicks off. If that is the case, you are probably safe.
Mr Coder
Mr Coder
Threads...Ok, now I'll try in this way.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.