Enjoy
the file copier.
Mystifier.
/** Created on Mar 26, 2005
*
*/
package mystifier.examples.filecopy;import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.*;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* @author mystifier
*
*/
public class FileCopyExample extends JPanel implements ActionListener
{
private JTextField srcTextField;
private JButton srcBrowseButton;
private JFileChooser fileChooser;
private JTextField destTextField;
private JButton destBrowseButton;
private JButton copyButton;
private File srcFile;
private File destFile;
private JLabel messageLabel;public FileCopyExample() {
super(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(10,5,10,5);
c.weighty = 0.5;
c.weightx = 1.0;
add(new JLabel("Source file"),c);
srcTextField = new JTextField(40);
srcTextField.setEditable(false);
c.weightx = 0.0;
c.fill = GridBagConstraints.HORIZONTAL;
add(srcTextField,c);
srcBrowseButton = new JButton("Browse");
srcBrowseButton.addActionListener(this);
c.fill = GridBagConstraints.NONE;
c.gridwidth = GridBagConstraints.REMAINDER;
add(srcBrowseButton,c);
c.weightx = 1.0;
c.gridwidth = 1;
add(new JLabel("Destination file"),c);
destTextField = new JTextField(40);
destTextField.setEditable(false);
c.fill = GridBagConstraints.HORIZONTAL;
add(destTextField,c);
destBrowseButton = new JButton("Browse");
destBrowseButton.addActionListener(this);
c.fill = GridBagConstraints.NONE;
c.gridwidth = GridBagConstraints.REMAINDER;
add(destBrowseButton,c);copyButton = new JButton("Copy");
copyButton.addActionListener(this);
add(copyButton,c);
messageLabel = new JLabel(" ");
messageLabel.setForeground(Color.RED);
c.fill = GridBagConstraints.HORIZONTAL;
add(messageLabel,c);
fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
}/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if(e.getSource() == srcBrowseButton)
{
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setSelectedFile(null);
fileChooser.showOpenDialog(this);
srcFile = fileChooser.getSelectedFile();
srcTextField.setText(srcFile.getAbsolutePath());
return;
}
if(e.getSource() == destBrowseButton)
{
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setSelectedFile(null);
fileChooser.showSaveDialog(this);
destFile = fileChooser.getSelectedFile();
destTextField.setText(destFile.getAbsolutePath());
return;
}
if(e.getSource() == copyButton)
{
copy();
return;
}
}
public static void copyFile(File src,File dest) throws IOException
{
FileChannel in = new FileInputStream(src).getChannel();
FileChannel out = new FileOutputStream(dest).getChannel();
long transBytes = 0;
while(transBytes < in.size())
{
transBytes += out.transferFrom(in,0,in.size() - transBytes);
}
in.close();
out.close();
}
public static void copyLargeFile(File src,File dest) throws IOException
{
FileChannel in = new FileInputStream(src).getChannel();
FileChannel out = new FileOutputStream(dest).getChannel();
MappedByteBuffer map = in.map(FileChannel.MapMode.READ_ONLY,0,in.size());
out.write(map);
in.close();
out.close();
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);//Create and set up the window.
JFrame frame = new JFrame("FileCopyExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Create and set up the content pane.
JComponent newContentPane = new FileCopyExample();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
/**
* @param args
* @throws IOException
*/
public void copy() {
if(srcFile == null || destFile == null)
{
messageLabel.setText("Source or Destination is empty");
}
if(!srcFile.exists())
{
messageLabel.setText("source file("+srcFile.getAbsolutePath()+") does not exist.");
return;
}
if(destFile.isDirectory())
destFile = new File(destFile.getAbsolutePath()+ File.separator + srcFile.getName());
if(destFile.exists())
{
messageLabel.setText("destination file("+destFile.getAbsolutePath()+") exists.");
return;
}
try {
if(srcFile.length() > 200000)
{
copyLargeFile(srcFile,destFile);
}
else
{
copyFile(srcFile,destFile);
}
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
}}
_______________________________________________ Java mailing list [email protected] http://mail.jug-delhi.org/mailman/listinfo/java_jug-delhi.org
