List of Topics:
Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Copy a File from One Location to Another in Java?

Copy File in Java

Condition for Copying a File from One Location to Another in Java

  • Description: Copying a file from one location to another in Java involves using classes like `FileInputStream` and `FileOutputStream` or utilizing the `Files` class from the `java.nio.file` package. With `FileInputStream`, the file is read byte by byte, and with `FileOutputStream`, the data is written to the destination file. Alternatively, `Files.copy()` from `java.nio.file.Files` provides a simpler and more efficient way to copy files, allowing for options such as replacing the destination file or copying attributes. The `InputStream` and `OutputStream` approach requires handling resources carefully using try-with-resources to ensure the streams are closed after the operation. Error handling is essential to catch exceptions such as file not found or access issues, and it is important to ensure that the target directory exists before performing the copy operation.
Sample Source Code
  • # CopyFile.java
    package SocketConnection;

    import java.awt.HeadlessException;
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.StandardCopyOption;
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;

    public class CopyFile extends javax.swing.JFrame {

    public CopyFile() {
    initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jTextField2 = new javax.swing.JTextField();
    jButton3 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Copy a File One Location to Another Location");

    jButton1.setText("Choose File");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    jButton2.setText("Copy");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton2ActionPerformed(evt);
    }
    });

    jButton3.setText("Choose copy location");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton3ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(26, 26, 26)
    .addComponent(jLabel1))
    .addGroup(layout.createSequentialGroup()
    .addGap(138, 138, 138)
    .addComponent(jButton2))
    .addGroup(layout.createSequentialGroup()
    .addGap(57, 57, 57)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
    .addComponent(jTextField2)))
    .addGroup(layout.createSequentialGroup()
    .addGap(117, 117, 117)
    .addComponent(jButton1))
    .addGroup(layout.createSequentialGroup()
    .addGap(112, 112, 112)
    .addComponent(jButton3)))
    .addContainerGap(28, Short.MAX_VALUE))
Screenshots
  • STEP 1: The user selects a text document from local files.
  • The user selects a text document from local files.

  • STEP 2: To click the choose file button, it navigates the local device to choose a file.
  • To click the choose file button, it navigates the local device to choose a file.

  • STEP 3: The file path will be displayed in the text field.
  • The file path will be displayed in the text field..

  • STEP 4: The user selects another text document from local files for storing purposes.
  • The user selects another text document from local files for storing purposes.

  • STEP 5: The file path will be displayed in the text field.
  • The file path will be displayed in the text field.

Related Links