0

How can I index first line from files in a field and other lines in a different field?

My code is:

  FileInputStream fis;
  try {
    fis = new FileInputStream(file);
  } catch (FileNotFoundException fnfe) {
    return;
  }

  try {

    Document doc = new Document();

    doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8))));
  } finally {
    fis.close();
  }

Please help me!

  • 1
    Have you checked [this](http://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java) answer? It shows how to read file line by line. – mindas Jun 25 '14 at 13:30
  • Yes, but I need a way to treat differently the lines. That because I need to add some lines in a field and some lines in other field – user3733768 Jun 25 '14 at 14:08
  • 1
    OK, so just add another `TextField` with a different field name. Should be trivial, no? – mindas Jun 25 '14 at 14:10

1 Answers1

0

I did it!

FileInputStream fis;
try {
  fis = new FileInputStream(file);
} catch (FileNotFoundException fnfe) {
  return;
}

try {

  Document doc = new Document();

  String line = null;
  try (BufferedReader reader = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8))) {
    line = reader.readLine();
    Field headerField = new TextField("header", line, Field.Store.YES);
    headerField.setBoost(2.0F);
    doc.add(headerField);
    while ((line = reader.readLine()) != null ) {
        doc.add(new TextField("contents", line, Field.Store.YES));
        }
    } catch (IOException e) {
        System.err.println(e);
    }

} finally {
  fis.close();
}