简体   繁体   中英

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

I am currently working on a project that shows a JList of courses a student may enroll on.

When the list item is clicked, a variable is passed to the setTextArea() method to get the txt files name.

For example if I click on Computer Science, a courseName variable is passed and inserted to the URL to retrieve the txt file.

在此处输入图片说明

This works perfectly fine. Except when I use the JAR File. I know .getClass() and getResource() must be used to access resources when using a JAR, but I can't seem to get mine to work.

Here is the original code I had.

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

I then tried to use the getClass and getResource, but it will not work with a URL and asks me to change the URL type to String . If I do this, then the getClass and getResource wont work.

在此处输入图片说明

I later tried to concatenate the url to string by using String urlString = url + ""; but this also didn't work.

Any ideas on how to go about this?

I guess that you FileReader does not work, when it is in a JAR file, as it tries to get the file in the current working directory, and not in the JAR file.

You must use this line:

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

I think that this should solve your problem.


Since you are working with Scanner I suggest you to remove your FileReader completely, and instead use this line to initialize your Scanner object:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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