Original Post
🔒 Locked
[java] How to copy files
Advertisement
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/
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/
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 ;) .
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 ;) .
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.
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement