如何适应我的文件所需的JTextArea?

时间:2014-10-11 16:54:35

标签: java swing file-io jtextarea

我希望我的JTextArea在txt文件中显示文本。但它只显示整个文本。

http://pastebin.com/Y8vWUvtg

package jBoxThreadTry;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class GuestFinal extends JFrame implements Runnable {

    private JLabel test;
    private JTextArea txtArea;
    private String titleBar;
    private static String fileName;

    private String[] CSEterms = {"CSE11.txt", "CSE12.txt", "CSE21.txt",
        "CSE22.txt", "CSE31.txt", "CSE32.txt", "CSE41.txt", "CSE42.txt"};

    private boolean threadAliveFlag;

    public GuestFinal(boolean threadAliveFlag) {
                    // TODO Auto-generated constructor stub
        // super()
        this.threadAliveFlag = threadAliveFlag;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            while (threadAliveFlag) {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setLayout(new FlowLayout());
                setVisible(true);
                setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

                /*test = new JLabel("yes");
                 add(test);
                 */
                setTitle(titleBar);

                threadAliveFlag = false;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    public void setBool(boolean b) {
        // TODO Auto-generated method stub
        threadAliveFlag = b;
    }

    public void setTitleBar(String string) {
        // TODO Auto-generated method stub
        titleBar = "Syllabus for " + string;
    }

    public void setFileToShow(int selectedIndex) {
        // TODO Auto-generated method stub
        fileName = CSEterms[selectedIndex];
        showFile(fileName);
    }

    private void showFile(String fName) {
        // TODO Auto-generated method stub
        try {
            FileInputStream fstream = new FileInputStream("syllabusDir\\"
                    + fName);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            String line = br.readLine();
            StringBuilder strBuilder = new StringBuilder();

            while (line != null) {
                // Print the content on the console
                System.out.println(line);
                strBuilder.append(line);
                line = br.readLine();
            }

            String everything = strBuilder.toString();

            txtArea = new JTextArea(everything);
            add(txtArea);

            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}

3 个答案:

答案 0 :(得分:3)

  

"我希望我的JTextArea在txt文件中显示文本。但它只显示整个文本"

while (line != null) {
     // Print the content on the console
     System.out.println(line);
     strBuilder.append(line);
     line = br.readLine();
}

字符串只是一个很长的字符序列。所以你正在做的只是附加相同的字符序列。分隔行的方法是使用行分隔符\n字符。所以你想在每一行之后追加它。

strBuilder.append(line);
strBuilder.append("\n");

替代方案,在这种情况下没有多大区别,但JTextArea也有append方法。


<强>更新

最合理的方法是使用JTextArea.read()方法,您可以将BufferedReader传递给该方法,这将读取整个文件的文本区域。无需循环和追加。

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
JTextArea area = new JTextArea(10, 50);
area.read(reader, null);

简单就是那个

答案 1 :(得分:1)

这段代码:

while (line != null) {
     // Print the content on the console
     System.out.println(line);
     strBuilder.append(line);
     line = br.readLine();
}

strBuilder.append(line);更改为strBuilder.append(line+"\n");

追加不会像你想要的那样添加换行符。

另外,如果您正在寻找该功能,请使用txtArea.setLineWrap(true);确保始终包装行。

答案 2 :(得分:0)

添加一种方便的方法来使用流和NIO

来读取文件
 public String readFile(String fName) throws IOException {
    List<String> contents = Files.readAllLines(FileSystems.getDefault().getPath("syllabusDir\\"
            + fName));
    return contents
            .stream()
            .collect(Collectors.joining("\n"));
}

另见java.nio.file.Files#readAllLines(java.nio.file.Path, java.nio.charset.Charset)