从jfilechooser获取文件的值

时间:2015-12-05 06:38:42

标签: java jbutton actionlistener jfilechooser

我有一个带有Jbutton的Java应用程序,用于选择本地文件,然后用不同类型的图表表示该文件。 我想获取JFileChooser返回的文件并对其进行处理(拆分,在某些列表中添加值...)

这是JButton actionlistener:

JButton theButton = new JButton("Choose the file to represent");
theButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent event) {
        int returnValue = fileChooser.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            theFile = fileChooser.getSelectedFile();
            ReadGCFile.getValue(theFile);
        }       
}

});         
 desktopPane.add(theButton);
 theButton.setVisible(true);
 theButton.setBounds(20, 100, 250, 20);
 getContentPane().add(theButton);

这里我要处理文件:

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

        fr = new FileReader(GDRT.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();
    }

我制作了一个getValue方法,其中我不知道该放什么:

 public static void getValue(File myFile) {
 System.out.println(myFile.getName());
 }

1 个答案:

答案 0 :(得分:0)

考虑这样的方法:

<script>

 $( document ).ready(function() {
    $("#switch").bootstrapSwitch();
    $( "#set_on" ).click(function() {
        $("#switch").bootstrapSwitch('setState', true);
    });
    $( "#set_off" ).click(function() {
        $("#switch").bootstrapSwitch('setState', false);
    });
});

</script>

这可以在你的ActionListener中调用:

public void processFile(File newFile) {
    try {

        getValue(newFile);
        FileReader fr = new FileReader(newFile);   
        BufferedReader bufReader = new BufferedReader(fr);
        Object line;
        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]);

        }
}