简体   繁体   中英

How to display a filenames (.txt) in JTextArea?

This method shows only a name of file in console.

JTextArea area = new JTextArea(20,40);
public void readContent(){
    KreatorPytan kp = new KreatorPytan();

    File file = new File("D:\\IT\\JAVA\\zadanie\\Testy");
    File[] files = file.listFiles();

    for(int i = 0; i < files.length; i++){
        try {
            BufferedReader reader = null;
            reader = new BufferedReader(new FileReader(files[i]));
            if(files[i].isFile()){
                System.out.println(files[i].getName());
                area.read(reader, "File");
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(WyborPytan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(WyborPytan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

I don't know why these names of files are not saving to my jtextarea. Can you help me?

This code is reading files to JTextArea and rewriting each previous file contents with the next. So as result you will see the last file contents. If you want to get file names in JTextArea, you don't need to read file, just do smth like

    for (int i = 0; i < files.length; i++) {
        area.append(files[i].getName() + '\n');
    }

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