简体   繁体   中英

How to read line by line from a text area

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM