How to read line by line from a text area

I figured out how to read line by line and display the contents of a text document line by line in jtextarea, and I figured out how to write line by line from an array of lines to a text document. It’s just hard for me to get every row from the text field, as soon as I get each row into an array, I have to go Below is the code I'm going to use to write each line to a file ...

public class FileWrite { public static void FileClear(String FileName) throws IOException{ FileWriter fstream = new FileWriter(FileName,true); BufferedWriter out = new BufferedWriter(fstream); out.write(""); } public static void FileWriters(String FileName, String Content) throws IOException { FileWriter fstream = new FileWriter(FileName,true); BufferedWriter out = new BufferedWriter(fstream); out.append(Content); out.newLine(); } } 

thanks

from

+6
source share
2 answers

What you get from TextArea is just a string. Divide it on a new line, and you have your line [].

 for (String line : textArea.getText().split("\\n")) doStuffWithLine(line); 
+20
source

I tried using the methods provided by the JTextArea class to answer this question.

Hope this helps someone, as I could not find the answer when I was looking for it. All you have to do is implement the processLine (String lineStr) method

  int lines = textArea.getLineCount(); try{// Traverse the text in the JTextArea line by line for(int i = 0; i < lines; i ++){ int start = textArea.getLineStartOffset(i); int end = texttArea.getLineEndOffset(i); // Implement method processLine processLine(textArea.getText(start, end-start)); } }catch(BadLocationException e){ // Handle exception as you see fit } 

See class definition here JTextArea Java 1.7

Happy coding !!!

0
source

Source: https://habr.com/ru/post/913751/


All Articles