简体   繁体   中英

Read text and input file text to JTextArea

What's wrong with my code? I'm trying to read text file and then put the text to JTextArea , but its input only consists of the last line of text. What's wrong?

Code :

public void read() {
    int returnVal = fc.showOpenDialog(null);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        pavadinimas = file.getName();
        try {
            FileInputStream fstream = new FileInputStream(fc.getCurrentDirectory() + "/" + pavadinimas);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null) {
                tekstas.setText(strLine);
            }
            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}

使用append函数代替settext

Don't reinvent the wheel. There is no need to write looping code or append you own end-of-line string.

Use the JTextArea.read(...) method.

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