简体   繁体   English

从资源文件夹中读取文本文件并填充jTextArea(NetBeans Java)

[英]Read text file from resource folder and populate jTextArea (NetBeans Java)

StringBuilder result = new StringBuilder("");   
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("DBase.dat").getFile());
try (Scanner scanner = new Scanner(file)) {
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        result.append(line).append("\n");                        
    }
    jTextArea1.setText(result.toString());
    scanner.close();    
} catch (FileNotFoundException ex) {

}

I am new to JAVA and netBeans I am using this code to read a text file from resources folder and then populate jTextArea from that file. 我是JAVA和netBeans的新手,正在使用此代码从资源文件夹中读取文本文件,然后从该文件填充jTextArea。 But I am getting Errors. 但是我收到错误消息。 I want to read textfile line by line not all the text at once... Please Help me what should I do. 我想逐行读取文本文件,而不是一次读取所有文本...请帮助我该怎么办。 I am pasting a picture too... Project Picture 我也要粘贴图片... 项目图片

尝试这样的事情:

File file = new File(classLoader.getResource("DBase.dat").getPath());

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

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