简体   繁体   中英

How to fetch strings from a text document in java?

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?

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();
}

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