0

I have a JButton which opens JFileChooser and that in turn selects a file into a variable called "file".

I want to rename the selected file to "Best.html" and then provide it to TableToCSV.java (java TableToCSV.class Best.html), which will convert the selected file to a .csv format.

Here is my code -

final JFileChooser  fileDialog = new JFileChooser();
    JButton btnInputFile = new JButton("Input File");
    btnInputFile.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            int returnVal = fileDialog.showOpenDialog(rootPane);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
               java.io.File file = fileDialog.getSelectedFile();

            }

        }
    });

Note - I want to rename because the TableToCSV.java file only inputs file with a .html extension.

Note - TableToCSV.java lies in the same folder as my java program.

0

3 Answers 3

2

You have two options...

You Could...

Use TableToCSV main method...

TableToCSV.main(new String[]{file.getAbsolutePath()});

Which is essentially the same as calling it from the command line

Or You Could...

Use the TableToCSV constructor...

TableToCSV tableToCSV = new TableToCSV( file, ',', '\"', '#', CSV.UTF8Charset );

The problem with this is, TableToCSV is expecting a file name with a 4 char extension...So if you were to pass it a File with an extension of .txt, the resulting file won't appear as you expect it and could actually lead to some danger.

In this case you could use

String name = file.getName();
name = name.subString(0, name.lastIndexOf("."));
name += ".html";
File newFile = new File(file.getParentFile(), name);
if (file.renameTo(newFile)) {
    TableToCSV tableToCSV = new TableToCSV( newFile, ',', '\"', '#', CSV.UTF8Charset );
}

But I do hate renaming files...

Sign up to request clarification or add additional context in comments.

Comments

0

Why not call the constructor of TableToCSV? Like in line 192 of TableToCSV.java?

 new TableToCSV( file, ',', '\"', '#', CSV.UTF8Charset );

Comments

0

Maybe something like this?

final JFileChooser  fileDialog = new JFileChooser();
        JButton btnInputFile = new JButton("Input File");
        btnInputFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                int returnVal = fileDialog.showOpenDialog(rootPane);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                   java.io.File file = fileDialog.getSelectedFile();
                   File newFile = new File("Best.html")
                   com.mindprod.TableToCSV(file.renameTo(newfile), ... );
                }

            }
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.