I have figured out how to read in line by line and display the contents of a text document line by line into a jtextarea and I have figured out how to write out line by line from an array of strings to the text document. I am just having a hard time getting each line from the textarea, as soon as I can get each line into an array i am good to go. Below is the code I am going to use to write each line to the 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
c
\n is the linefeed character literal (ASCII 10) in a Javascript string. <br/> is a line break in HTML.
Use the value property to get the value of a textarea, e.g. const value = textarea. value . The value property can be used to read and set the value of a textarea element. If the textarea is empty, an empty string is returned.
What you get from TextArea
is just a String. Split it at newline and you've got your String[].
for (String line : textArea.getText().split("\\n")) doStuffWithLine(line);
I tried to use the methods provided by the JTextArea class to answer this question.
Hope this helps someone since I couldn't find the answer when I googled it. All that you need to do now is implement the method processLine(String lineStr)
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 the definition of the class here JTextArea Java 1.7
Happy coding!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With