9

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

Christian Kuetbach
  • 15,171
  • 4
  • 37
  • 74
Charlie
  • 1,198
  • 5
  • 13
  • 24

2 Answers2

29

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);
Marko Topolnik
  • 179,046
  • 25
  • 276
  • 399
  • so if i have a file with 10 lines of text that should give me lines[1-9]? – Charlie Apr 20 '12 at 21:48
  • The array indices are zero-based. [0-9]. – Marko Topolnik Apr 20 '12 at 21:51
  • I meant 0 whoops haha, I have one question, when I run my for loop it gives me an arrayindexoutofbounds error on the last line, is that because there is not another line after it so it can't split it? how do i fix this? Thanks – Charlie Apr 20 '12 at 22:32
  • See updated answer for the proper idiom to iterate over the array. – Marko Topolnik Apr 20 '12 at 22:34
  • Nice. So rework it to what I showed you. Your example blows because you allow i to get equal to line count, if that's important to you. If you used the style I showed, you wouldn't have to worry about index at all. – Marko Topolnik Apr 20 '12 at 22:40
  • ahhh wait I only glanced at the reworked edition and didnt notice it was a for loop! Thanks! – Charlie Apr 20 '12 at 22:46
  • Thanks man! That worked incredibly well! I didnt even know that could be done with a for loop! – Charlie Apr 20 '12 at 22:53
1

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!!!