簡體   English   中英

使用JFileChooser處理文件

[英]Processing a file with JFileChooser

大家好,我的程序遇到了問題。 我試圖使該程序僅顯示文本文件,並且一旦用戶選擇了一個文件,文件信息應顯示在GUI的文本框中。 我收到此錯誤:

FileChooserDemo3.java:66: error: unreported exception IOException; must be caught or declared to be thrown while ((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。 譯者的意思是,即使在打開文件后,從文件中讀取文件仍然會遇到問題。

發生的事情正是它所說的事情。 “未報告的異常IOException。” 基本上,您的catch語句不會捕獲所有可能引發的異常,請將其更改為捕獲IOException,或者確保它捕獲所有可能的異常,無論如何,使其捕獲Exception。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM