Domanda

I have a file of words in text.I want to read the file

FileInputStream fstream = new FileInputStream(s);               
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

MaxentTagger tagger = new MaxentTagger("tag/wsj-0-18-bidirectional-distsim.tagger");    
String tagged = tagger.tagString(br);

My problem is it should read the file and give line by line of the file as string to the tagger and print in a output file.

È stato utile?

Soluzione

As both input and output are going to be text, I'd use Reader and Writer rather than streams. Something like:

try (
  BufferedReader in = new BufferedReader(new FileReader("inputFile.txt"));
  PrintWriter out = new PrinterWriter(new FileWriter("outputFile.txt"));
) {

  MaxentTagger tagger = new MaxentTagger("tag/wsj-0-18-bidirectional-distsim.tagger");    
  String line;
  while ((line = in.readLine()) != null) {
    String tagged = tagger.tagString(line);
    out.println(tagged);
  }
}

Note, this code uses Java 7 resource handling, so the in and out are closed automatically.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top