简体   繁体   English

使用JFileChooser处理文件

[英]Processing a file with JFileChooser

Hey guys I am running into an issue with my program. 大家好,我的程序遇到了问题。 I am trying to get the program to only show text files, and once the user selects one, the file information should be displayed in a textbox in the GUI. 我试图使该程序仅显示文本文件,并且一旦用户选择了一个文件,文件信息应显示在GUI的文本框中。 I am getting this error: 我收到此错误:

FileChooserDemo3.java:66: error: unreported exception IOException; must be caught or declared to be thrown while ((strLine = br.readLine()) != null) {

Why is this happening? 为什么会这样呢? I have a catch statement.. Thanks for any help! 我有一个声明。.谢谢您的帮助!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
import java.io.*;

class FileChooserDemo3{

   JLabel jlab;
   JButton jbtnShow;
   JFileChooser jfc;
   JTextArea jta;
   JScrollPane scrollPane;

   FileChooserDemo3() {
      //create new JFrame container.
      JFrame jfrm = new JFrame("JFileChooser Demo");

      //Specify FlowLayout for layout manager
      jfrm.setLayout(new FlowLayout());

      //Give the frame initial size
      jfrm.setSize(800,800);

      //End program when user closes application
      jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      //Create a label to show selected file
      jlab=new JLabel();

      //Create button to show dialog
      jbtnShow = new JButton("Show File Chooser");

      //create textarea with ability to textwrap (p889-891) and scroll (hint:  Use JScrollPane)
      JTextArea textInput = new JTextArea(20, 40);
      textInput.setLineWrap(true);
      JScrollPane scrollPane = new JScrollPane(textInput);




      //Create file chooser starting at default directory
      jfc=new JFileChooser();

      //Show file chooser when show file chooser button pressed
      jbtnShow.addActionListener(new ActionListener()  {
         public void actionPerformed(ActionEvent le) {
            //Pass null for the parent.  This centers the dialog on the screen.
            int result = jfc.showOpenDialog(null);

            if(result==JFileChooser.APPROVE_OPTION){
               jlab.setText("Selected file is:  " + jfc.getSelectedFile().getName());

               //Get selected file stored as a file.




               try{
                  //Do file processing here
                 String strLine;
                 File selectedFile = jfc.getSelectedFile();
                 FileInputStream in = new FileInputStream(selectedFile);
                 BufferedReader br = new BufferedReader(new InputStreamReader(in));
                 while ((strLine = br.readLine()) != null) {
                     textInput.append(strLine + "\n");
                 }
               }

               catch(FileNotFoundException e){
                  System.out.println("Exception");
               }



            }
            else{
               jlab.setText("No file selected.");
            }
         }
      });

         //add the show file chooser button and label to content pane
         jfrm.add(jbtnShow);
         jfrm.add(jlab);


         //Display the frame
         jfrm.setVisible(true);
   }

   public static void main(String[] args){
      //Create GUI on the event dispatching thread.
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new FileChooserDemo3();
         }
      });
   }
}

You are catching FileNotFoundException, but you also need to catch IOException after your try {} block. 您正在捕获FileNotFoundException,但是在try {}块之后还需要捕获IOException。

Programmatically, this is because readLine declares throws IOException. 以编程方式,这是因为readLine声明引发IOException。 Translated, what it's saying is that even after the file is opened, it could still encounter a problem reading from the file. 译者的意思是,即使在打开文件后,从文件中读取文件仍然会遇到问题。

Whats happening is exactly what it says is happening. 发生的事情正是它所说的事情。 "unreported exception IOException." “未报告的异常IOException。” Basically your catch statement is not catching all possible exceptions that can be thrown, change it to catch either IOException, or to make sure it catches every possible excepion, no matter what, make it catch Exception. 基本上,您的catch语句不会捕获所有可能引发的异常,请将其更改为捕获IOException,或者确保它捕获所有可能的异常,无论如何,使其捕获Exception。

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

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