简体   繁体   中英

How to process a file returned from JFileChooser in Java

I have an application to create graphic representation of data. I use this method on JFreeCharts methods to create the charts using the lists parameters. I need to process the file returned from JFileChooser. Here is my class:

public class ReadGCFile {
static File theFile;
static JFileChooser fileChooser = new JFileChooser();
static JButton theButton = new JButton("Choose the file to represent");

public static void readGCList(List<String> gcArrayList,
        List<String> gcStringList, List<String> gcDateList)
        throws NumberFormatException, IOException, ParseException {
    String line = "";
    String[] tokens = null;
    FileReader fr = null;
    BufferedReader bufReader = null;

    theButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {

            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                theFile = fileChooser.getSelectedFile();

            }
        }
    });

    try {

        fr = new FileReader(theFile);

        bufReader = new BufferedReader(fr);
        while ((line = bufReader.readLine()) != null) {
            line = line.replace(",", ".");
            tokens = line.split(";");

            gcDateList.add(tokens[0]);
            gcStringList.add(tokens[1]);
            gcArrayList.add(tokens[2]);
            gcArrayList.add(tokens[3]);
            gcArrayList.add(tokens[4]);

        }

    } catch (FileNotFoundException es) {
        System.out.println("The file was not found.");

    } catch (NullPointerException e) {
        System.out.println("No files were chosen !");
    }

    catch (IOException e) {

        bufReader.close();

    }

    for (int i = 0; i < gcDateList.size(); i++) {
        SimpleDateFormat currentFormat = new SimpleDateFormat(
                "yyyy-MM-dd hh:mm:ss");
        SimpleDateFormat convertedFormat = new SimpleDateFormat(
                "dd MMM hh:mm:ss");

        gcDateList.set(i, convertedFormat.format(currentFormat
                .parse(gcDateList.get(i))));

    }

}

}

How can I keep the value of "theFile" from the button's actionlistener. In the "try" block, "theFile" is null.

Process the file only when you have one, that is, in the actionPerformed method.

Example :

Put all your processing logic inside one method :

private static void processFile(File theFile, List<String> gcArrayList,
        List<String> gcStringList, List<String> gcDateList){

    String line = "";
    String[] tokens = null;
    FileReader fr = null;
    BufferedReader bufReader = null;

   try {

        fr = new FileReader(theFile);

        bufReader = new BufferedReader(fr);
        while ((line = bufReader.readLine()) != null) {
            line = line.replace(",", ".");
            tokens = line.split(";");

            gcDateList.add(tokens[0]);
            gcStringList.add(tokens[1]);
            gcArrayList.add(tokens[2]);
            gcArrayList.add(tokens[3]);
            gcArrayList.add(tokens[4]);

        }

    } catch (FileNotFoundException es) {
        System.out.println("The file was not found.");

    } catch (NullPointerException e) {
        System.out.println("No files were chosen !");
    }

    catch (IOException e) {

        bufReader.close();

    }

    for (int i = 0; i < gcDateList.size(); i++) {
        SimpleDateFormat currentFormat = new SimpleDateFormat(
                "yyyy-MM-dd hh:mm:ss");
        SimpleDateFormat convertedFormat = new SimpleDateFormat(
                "dd MMM hh:mm:ss");

        gcDateList.set(i, convertedFormat.format(currentFormat
                .parse(gcDateList.get(i))));

    }

}

Then, call it from the actionPerformed method :

theButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {

            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                theFile = fileChooser.getSelectedFile();
                processFile(theFile,gcArrayList,gcStringList,gcDateList);

            }
        }
    });

Also, depending on your needs, you may consider resetting the lists between two files processing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM