Java 按下按钮后读取文件

Java 按下按钮后读取文件,java,swing,java-io,jfilechooser,Java,Swing,Java Io,Jfilechooser,我试图创建一个程序,让你选择一个文件,读取它,然后打印出结果。。。这就是我目前所拥有的 public class Main { public static void main(String[] args) { JFrame frame = new JFrame("Bebras braces matcher"); JButton selectf = new JButton("Open file"); selectf.addActionList

我试图创建一个程序,让你选择一个文件,读取它,然后打印出结果。。。这就是我目前所拥有的

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Bebras braces matcher");
        JButton selectf = new JButton("Open file");
        selectf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String cmd = e.getActionCommand();
                if(cmd.equals("Open file")) {
                    JFileChooser chooser =new JFileChooser();
                    FileFilter filefilter = new FileNameExtensionFilter("","txt");
                    chooser.setFileFilter(filefilter);
                    int returnVal = chooser.showOpenDialog(chooser);
                    if(returnVal==JFileChooser.APPROVE_OPTION) {
                        FileName=chooser.getSelectedFile().getName();
                        System.out.println(FileName);       
                    }
                }
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);
        frame.add(selectf);
        frame.setVisible(true);

    }
    public static void countChar() throws FileNotFoundException {
        Scanner scan = null;
        try {
            scan = new Scanner(new File(FileName));
        }
        catch (FileNotFoundException e) {
        }
        while (scan.hasNext()) {
            String character = scan.next();
            int index =0;
            char close = '}';
            char open = '{';
            while(index<character.length()) {

                if(character.charAt(index)==close){
                    CloseCount++;
                }
                else if(character.charAt(index)==open) {
                    OpenCount++;
                }
                index++;
            }
        }
        System.out.println("Opened: "+OpenCount+"Closed: "+CloseCount);
    }
    private static String FileName;
    private static int CloseCount = 0;
    private static int OpenCount = 0;
    private static final long serialVersionUID = 7526472295622776147L;
}
公共类主{
公共静态void main(字符串[]args){
JFrame frame=新JFrame(“Bebras支架匹配器”);
JButton selectf=新JButton(“打开文件”);
选择f.addActionListener(新建ActionListener(){
已执行的公共无效操作(操作事件e){
字符串cmd=e.getActionCommand();
if(cmd.equals(“打开文件”)){
JFileChooser chooser=新的JFileChooser();
FileFilter FileFilter=newfilenameextensionfilter(“,”txt”);
选择器.setFileFilter(filefilter);
int returnVal=chooser.showOpenDialog(选择器);
if(returnVal==JFileChooser.APPROVE_选项){
FileName=chooser.getSelectedFile().getName();
System.out.println(文件名);
}
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。设置尺寸(500500);
frame.add(选择f);
frame.setVisible(true);
}
public static void countChar()引发FileNotFoundException{
扫描仪扫描=空;
试一试{
扫描=新扫描仪(新文件(文件名));
}
catch(filenotfounde异常){
}
while(scan.hasNext()){
String character=scan.next();
int指数=0;
char close='}';
char open='{';

while(index您就快到了!您只是打印出文件名,而不是调用该方法

看到这个了吗

if(returnVal==JFileChooser.APPROVE_OPTION) {
    FileName=chooser.getSelectedFile().getName();
    System.out.println(FileName);       
}

您只需将
countChar();

放入文件名而不是文件路径,而不是(或者在前面或后面,如果您愿意的话)
System.out.println(FileName);
。 所以你用这个

if(returnVal==JFileChooser.APPROVE_OPTION) {
FileName=chooser.getSelectedFile().getAbsolutePath();
countChar();

因为,如果文件位于项目所在的同一目录中,然后工作,但当所选文件位于不同的位置时,则需要使用所选文件的绝对路径。

那么,countChar应该做什么?在哪里调用它?静态字段应该由局部变量和参数替换。当然,还有一些其他变量风格问题。但它们今天超出了我们的范围。这是问题的答案。我忘了说我试过了。我得到一个错误:“未处理的异常类型FileNotFoundException”…就像,我找不到,因为用户指定了它!一个足够狡猾的用户可能会选择一个不存在的文件。但即便如此,
FileNotFoundException
就是所谓的检查异常——这意味着你需要在程序中的某个点捕获并处理异常。它看起来像你如果您在
countChars
中执行此操作,那么您可以从该方法的顶部删除
throws FileNotFoundException
。(您可能希望在捕获该异常时执行某些操作—显示错误消息,或者至少退出程序—但首先要担心如何将其编译。)