Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用getClass().getResource将.txt文件检索到JTextArea_Java_File_User Interface_Jar - Fatal编程技术网

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

Java 使用getClass().getResource将.txt文件检索到JTextArea,java,file,user-interface,jar,Java,File,User Interface,Jar,我目前正在进行一个项目,该项目显示了学生可以注册的课程列表 单击列表项时,将向setTextArea()方法传递一个变量以获取txt文件名 例如,如果单击“计算机科学”,将传递一个courseName变量并将其插入URL以检索txt文件 这个很好用。除非我使用JAR文件。我知道,在使用JAR时,必须使用.getClass()和getResource()来访问资源,但我似乎无法让我的工作正常 这是我的原始代码 public void valueChanged(ListSelectionEvent

我目前正在进行一个项目,该项目显示了学生可以注册的课程列表

单击列表项时,将向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连接到字符串但这也不起作用


有什么想法吗?

我想当FileReader在JAR文件中时,它不工作,因为它试图在当前工作目录中获取文件,而不是在JAR文件中

您必须使用此行:

FileReader f = new FileReader(new File(ClassA.class.getResource("/res/courseInfo/" +courseName + ".txt").toURI()));
我认为这应该解决你的问题。
由于您使用的是
扫描仪
,我建议您完全删除文件读取器,并使用此行初始化扫描仪对象:

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

你的资源到底在哪里?在你的jar文件中?如果是,具体在哪里?因为url在FileReader中不起作用。它要求将
url
转换为字符串类型。但是当我这样做的时候,getClass().getResource不起作用。你说的“除了使用JAR文件之外”是什么意思?@aribeiro当我使用eclipse时,我可以在JTextArea中查看txt文件的内容。但是如果我使用jar文件,它将无法工作,显然是因为它正在寻找一个只适用于eclipse的路径。因此,jar文件无法访问资源文件夹中的txt文件。只需按照@Hackerdarshi的答案操作即可。它仍然只在eclipse中工作。无法在jar中工作,JTextField仍然是empty@Aaronward您确定文本文件位于正确的目录中吗?@aaroward另外,请尝试从控制台运行jar文件,这样您就可以看到堆栈跟踪。是的,我的所有目录都正确。我在CMD中尝试了一下,当我点击JList中的课程时,我得到了错误代码
java.lang.IllegalArguementException:URI不是分层的
,它非常简单,就在我眼前。太好了,谢谢你。