繁体   English   中英

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

[英]How to process a file returned from JFileChooser in Java

我有一个应用程序来创建数据的图形表示。 我在JFreeCharts方法上使用此方法,以使用列表参数创建图表。 我需要处理从JFileChooser返回的文件。 这是我的课:

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

            }
        }
    });

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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