0

I am looking to convert a large text document into individual strings based on what line the sentences are found on. Is there a way to access the text document in java and designate each line as a separate string? Or at least quickly convert all of it by importing it to a java file?

  • 2
    Read the file into an ArrayList. Iterate through the the ArrayList and the index into the ArrayList becomes your line number. – camickr Oct 14 '20 at 03:00
  • 2
    have a look at this https://stackoverflow.com/questions/25553673/split-very-large-text-file-by-max-rows – DarrenChand Oct 14 '20 at 03:01

1 Answers1

0

Probably the easiest way would be reading all lines in the file (when the file is small) into an ArrayList by using the readAllLines() method available in Files class. Then you can pull the line you are interested in by using the line number:

public static String getLine(String filename, int lineNum) throws IOException {
  List<String> lines = Files.readAllLines(Paths.get(filename));
  return (lines.size() >= lineNum) ? lines.get(lineNum - 1) : null;
}

This method can also take a charset parameter, to read as per a specific character encoding:

public static String getLine(String filename, int lineNum) throws IOException {
  Charset charset = Charset.forName("ISO-8859-1");
  List<String> lines = Files.readAllLines(Paths.get(filename), charset);
  return (lines.size() >= lineNum) ? lines.get(lineNum - 1) : null;
}

But for larger files, you can use Stream APIs (Java 8):

public static String getLine(String filename, int lineNum) throws IOException {
  try (Stream<String> lines = Files.lines(Paths.get(filename))) {
    Optional<String> line = lines.skip(lineNum - 1).findFirst();
    return line.isPresent() ? line.get() : null;
  }
}

Using BufferedReader:

public static String getLine(String filename, int lineNum) throws IOException {
  FileInputStream fis = new FileInputStream(filename);
  BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
    for (int i = 0; i < lineNum - 1; ++i) {
        reader.readLine();
    }
  return reader.readLine();
}
1218985
  • 7,531
  • 2
  • 25
  • 31