繁体   English   中英

使用getClass()。getResource将.txt文件检索到JTextArea

[英]Using getClass().getResource to retrieve a .txt file to a JTextArea

我目前正在从事一个项目,该项目显示学生可以注册的课程的JList。

单击列表项后,会将变量传递到setTextArea()方法以获取txt文件名。

例如,如果我单击“计算机科学”,则会传递一个courseName变量并将其插入URL以检索txt文件。

在此处输入图片说明

这工作得很好。 除了我使用JAR文件时。 我知道使用JAR时必须使用.getClass()和getResource()来访问资源,但是我似乎无法正常工作。

这是我原来的代码

public void valueChanged(ListSelectionEvent e)
{
    int selectionNum = courseList.getSelectedIndex();
    String courseName;

    //Switch statement to pass strings to method
    switch(selectionNum)
    {
        case 0: courseName = "computer_science";
                setTextArea(courseName);
                currentCourseClicked ="0";
                break;

        case 1: courseName = "business";
                setTextArea(courseName);
                currentCourseClicked ="1";
                break;

        case 2: courseName = "business2";
                setTextArea(courseName);
                currentCourseClicked ="2";
                break;

        case 3: courseName = "engineering";
                setTextArea(courseName);
                currentCourseClicked ="3";
                break;

        case 4: courseName = "sport";
                setTextArea(courseName);
                currentCourseClicked ="4";
                break;

        case 5: courseName = "early";
                setTextArea(courseName);
                currentCourseClicked ="5";
                break;  

        case 6: courseName = "social";
                setTextArea(courseName);
                currentCourseClicked ="6";
                break;              
    }

}   //End of valuesChanges Method



/**
 * This method is used to read the file, with the course name passed
 * from the valueChanged method.
 * @param courseName
 */
@SuppressWarnings("resource")
public void setTextArea(String courseName)
{   
    descriptionPanel.setVisible(true);
    enrol.setVisible(true);
    enrol.setFont(new Font("", Font.BOLD, 20));

    try
    {
        //This retrieves the information from a text file
        FileReader file = new FileReader("./res/courseInfo/" +courseName + ".txt");
        Scanner fileReaderScan = new Scanner(file);

        String storeAll = "";

        //while loop to read trough lines of the text file
        while(fileReaderScan.hasNextLine())
        {   
            String temp = fileReaderScan.nextLine() + "\n";
            storeAll += temp;
        }
        descriptionTextArea.setText(storeAll);
    }
    catch (FileNotFoundException e1)
    {
        e1.printStackTrace();
    }
} //end of setTextArea method

然后,我尝试使用getClass和getResource,但它不适用于URL,并要求我将URL类型更改为String 如果我这样做,则getClass和getResource将无法工作。

在此处输入图片说明

我后来尝试通过使用String urlString = url + "";将url连接到字符串String urlString = url + ""; 但这也不起作用。

关于如何解决这个问题的任何想法?

我想当FileReader处于JAR文件中时,它将无法正常工作,因为它试图在当前工作目录而不是JAR文件中获取文件。

您必须使用以下行:

FileReader f = new FileReader(new File(ClassA.class.getResource("/res/courseInfo/" +courseName + ".txt").toURI()));

我认为这应该可以解决您的问题。


由于您正在使用Scanner ,因此建议您完全删除FileReader,而改用此行初始化Scanner对象:

 Scanner fileReaderScan = new Scanner(getClass().getResourceAsStream("/res/courseInfo/" +courseName + ".txt")); 

暂无
暂无

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

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