如何处理从 Java 中的 JFileChooser 返回的文件

How to process a file returned from JFileChooser in Java

我有一个应用程序可以创建数据的图形表示。 我在 JFreeCharts 方法上使用此方法,以使用列表参数创建图表。 我需要处理从 JFileChooser 返回的文件。 这是我的 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))));

    }

}

}

如何从按钮的动作侦听器中保留 "theFile" 的值。 在 "try" 块中,"theFile" 为空。

只有当你有一个文件时才处理该文件,即在 actionPerformed 方法中。

示例:

将所有处理逻辑放在一个方法中:

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))));

    }

}

然后,从 actionPerformed 方法调用它:

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);

            }
        }
    });

此外,根据您的需要,您可以考虑重置两个文件处理之间的列表。