Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read line by line from a text area

Tags:

java

file-io

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

like image 794
Charlie Avatar asked Apr 20 '12 20:04

Charlie


People also ask

How do you go to the next line in text area?

\n is the linefeed character literal (ASCII 10) in a Javascript string. <br/> is a line break in HTML.

How do I get content of text area?

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.


2 Answers

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);
like image 153
Marko Topolnik Avatar answered Nov 01 '22 12:11

Marko Topolnik


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

like image 28
DanG_3280651 Avatar answered Nov 01 '22 14:11

DanG_3280651