Home > Software design >  Java split method on new line
Java split method on new line

Time:01-17

I'm currently learning java application for desktop, specifically in Apache NetBeans IDE. I'm working on extract information from file to the application. Here what the file is about:enter image description here I want to split the end of the first sentence (6.0) from the beginning of the next sentence (TLA002). This is the requirement's example for the task I'm working on:enter image description here I was trying to use split("\n") but it turned out not working (It became like this when I tried: enter image description here). Can someone help me find the solution to this problems? It'd be a huge help. Thanks a lot! P/s: Sorry for the language difference, I'm not studying in an English course, hope you can forgive.

CodePudding user response:

Have you tried reading the file line by line?

Here is an example on how to do it (https://www.digitalocean.com/community/tutorials/java-read-file-line-by-line):

public class ReadFileLineByLineUsingBufferedReader {

    public static void main(String[] args) {
        BufferedReader reader;

        try {
            reader = new BufferedReader(new FileReader("sample.txt"));
            String line = reader.readLine();

            while (line != null) {
                System.out.println(line);
                // This is where you would split your individual line
                // read next line
                line = reader.readLine();
            }

            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
  • Related