簡體   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