Reputation: 1348
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
Upvotes: 9
Views: 40859
Reputation: 63
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!!!
Upvotes: 1
Reputation: 200266
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);
Upvotes: 28