问题
Is there a way to read the contents of a JTextPane
line by line? Much like the BufferedReader?
回答1:
Element root = textPane.getDocument().getDefaultRootElement();
Once you get the root Element you can check to see how many child elements (ie. lines) exist. Then you can get each child Element and use the start/end offset methods to get the text for that particular line.
This would be more efficient than getting all the text in one big string and then splitting it again.
回答2:
The way that I've done this in the past is to use the getText method over my pane, then parse the string that is returned looking for the newline character '\n'.
回答3:
Can you explain what you're trying to do? From the top of the head I can't say if it is possible actually to read it line by line. Of course you could just split the text by the newline character and then you would get an array of strings, each line as its own element. Is this a problem in your case?
来源:https://stackoverflow.com/questions/3550656/read-jtextpane-line-by-line