-4

I am trying to write a program that copies one file and copies it's contents to another. I have to have user chose the files. i am stuck can some one please help me.

import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import javax.swing.JFileChooser;

public class FileCopy {
    public static void main(String[]Args) throws IOException {
        JFileChooser chooser = new JFileChooser("/Users/josealvarado/Desktop/");

        Path FROM = Paths.get(chooser);
        Path TO = Paths.get(chooser);

        //overwrite existing file, if exists
        CopyOption[] options = new CopyOption[]{
            StandardCopyOption.REPLACE_EXISTING,
            StandardCopyOption.COPY_ATTRIBUTES
        };
        Files.copy(FROM, TO, options);
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720

2 Answers2

1

I only can guess, but i think you'll need a JFrame which will contain your JFileChooser, i made a small example without any functionality, only to show, how you could maybe reach your goal.

Please realize for your next question(s) here on SO, post what you tried and POST the errors / exception you get, otherwise it is hard to help or solve your problem!

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package de.professional_webworkx.tutorial.jtable.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
 *
 * @author ottp
 */
public class MainFrame extends JFrame {

    private JFileChooser chooser;

    public MainFrame() {
        initGUI();
    }

    private void initGUI() {

        chooser = new JFileChooser();
        chooser.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(chooser.getSelectedFile().getName());
            }
        });
        this.setTitle("FileChoosing and copy one file to another");
        this.setSize(1024, 768);
        this.getContentPane().add(chooser, BorderLayout.NORTH);
        this.setVisible(true);
    }
}

The Class to start your App

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package de.professional_webworkx.tutorial.jtable;

import de.professional_webworkx.tutorial.jtable.view.MainFrame;
import javax.swing.JFileChooser;

/**
 *
 * @author ottp
 */
public class JTableDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new MainFrame();

    }

}

Hope this helps, Patrick

Patrick
  • 4,532
  • 2
  • 26
  • 32
1
  Path FROM = Paths.get(chooser);
  Path TO = Paths.get(chooser)

You can't pass a JFileChooser to Paths.get(). Here are the overloaded static methods

  • Paths.get(String first, String... more) - Converts a path string, or a sequence of strings that when joined form a path string, to a Path.
  • Paths.get(URI uri) - Converts the given URI to a Path object.

You're probably looking to pass a String. In order to to that, you need to get the String file path from the JFileChooser. To do that, you first need to chooser.showOpenDialog() which returns an int if the OK button is pressed after selecting a file (APPROVE_OPTION), So you want to do something like this

JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(null);
String path;
if (result == JFileChooser.APPROVE_OPTION) {
    path = (chooser.getSelectedFile()).getAbsolutePath();
}

Then you can pass the path to Paths.get(path)

You should really have a look at How to Use File Choosers

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720