使用JFileChooser选择srt文件并读取它

时间:2014-01-14 21:48:10

标签: java swing file-io jfilechooser

我正在尝试在JFileChooser中工作要从另一个类中选择文件我不关心按钮的外观,但是当有人点击它时。它应该显示JFileChooser选择srt文件(如文本文件,但另一种类型),它应该读取它。

这是我的第一堂课

    package AnimeAid;
import java.io.*;
import javax.swing.*;

public class ReadFile {
    private File ourFile= null;
    private static final JFileChooser selectSrtFile = new JFileChooser();
    String filePath = "";

       public ReadFile(){

       }

    public File getSelectFile(){ 
            selectSrtFile.setFileSelectionMode(JFileChooser.FILES_ONLY);
            selectSrtFile.showSaveDialog(null);
            ourFile = selectSrtFile.getSelectedFile();
            filePath = ourFile.getAbsolutePath();
            return ourFile;
    }

        public String readFileInput(){
            try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(getSelectFile()), "UTF-8"));
              String line;
                while ((line = reader.readLine()) != null)
                {
                    System.out.println(line);
                }
            }catch(IOException ex){
            return "there is wrong";
            }
            return "file is added";

        }

}

扫描的课程

    package AnimeAid;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/**
 *
 * @author isslam
 */
public class GuiInterface extends JFrame {

    JTable table;
    JButton is;
    ReadFile t;

    public GuiInterface(String title){
    setSize(900, 700);
    setTitle(title);
    setDefaultCloseOperation(GuiInterface.EXIT_ON_CLOSE);
    is = new JButton();
    t = new ReadFile();
    Container cp = getContentPane();
    cp.add(is);

    is.addActionListener(new addButtonWatcher());

    }



    private class addButtonWatcher implements ActionListener{

         @Override
         public void actionPerformed(ActionEvent a){
             Object buttonPressed=a.getSource();
             if(buttonPressed.equals(is))
             {
              t.getSelectFile(); 
             }
}
    }
}

错误消息

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: animeaid.GuiInterface
    at animeaid.main.main(main.java:15)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

1 个答案:

答案 0 :(得分:1)

看起来你的两个类有不同的包:

package animeFactor;
             ^

package animefactor;
             ^

我相信Java包名称区分大小写,所以尽管您(可能)将这两个文件放在同一个文件夹中,GuiInterface类不能使用ReadFile类而不导入它

将包定义更改为相同,或将导入语句添加到GuiInterface

import animeFactor.ReadFile;