My problem is that when I use this code I only get the last line of text containing my requested word, how can I get all the lines which includes the specific word I asked and later store them into jTextArea
properly?
try {
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String line;
while((line = br.readLine()) !=null) {
if(line.contains("Win")){
jTextArea1.setText(line);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
jTextArea1.setText("sup");
}
One approach would be to save the String to a List
List<String> words = new ArrayList<>();
...
if (line.contains("Win")) {
words.add(line);
}
...
String output = StringUtils.join(words, "\n");
jTextArea1.setText(output);
The final few lines will join each word in the ArrayList using the new line token to join the words.
The problem is that you are using the method setText (). Every time you call setText() it replaces your Text Area. What you want to do is append so you need to call: jTextArea.append(word)
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.