Java 使用JFileChooser处理文件

Java 使用JFileChooser处理文件,java,Java,嘿,伙计们,我的程序遇到了问题。我试图让程序只显示文本文件,一旦用户选择一个,文件信息应该显示在GUI的文本框中。我得到这个错误: FileChooserDemo3.java:66:错误:未报告的异常IOException;必须被抓住或宣布被抛出 而((strLine=br.readLine())!=null){ 为什么会发生这种情况?我有一个捕获声明…谢谢你的帮助 import java.awt.*; import java.awt.event.*; import javax.swing.*;

嘿,伙计们,我的程序遇到了问题。我试图让程序只显示文本文件,一旦用户选择一个,文件信息应该显示在GUI的文本框中。我得到这个错误:

FileChooserDemo3.java:66:错误:未报告的异常IOException;必须被抓住或宣布被抛出
而((strLine=br.readLine())!=null){

为什么会发生这种情况?我有一个捕获声明…谢谢你的帮助

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

您正在捕获FileNotFoundException,但还需要在try{}块之后捕获IOException


从编程角度讲,这是因为readLine声明抛出IOException.Translated,它的意思是,即使在打开文件后,从文件读取时仍可能遇到问题。

发生的正是它所说的发生的。“未报告的异常IOException。”基本上,您的catch语句并没有捕获所有可能抛出的异常,请将其更改为捕获IOException,或者确保它捕获所有可能的异常,无论发生什么,都让它捕获Exception。

如果我将其更改为IOException,它仍然会捕获FileNotFound吗?@still2blue它仍然会捕获FileNotFoundException由于FileNotFoundException扩展了IOException,因此捕获(异常)是非常糟糕的做法,因为这将隐藏该块中发生的运行时异常。谢谢!说明说我只需要担心接受文本文件,然后我必须在GUI的文本框中显示txt文件信息。我已经创建了textarea,如何让该文件显示在textarea中?@still2blue我在您的代码中没有看到除此之外的任何问题您提出的IOException问题。如果您对此有其他问题,我想您最好单击我发布的正确解决方案,然后发布另一个问题和更新的代码