0

Hi below is my code to extract particular metadata tags and write those tags to a json file. And i imported json.lib.jar and tika-app.jar into my build path.

File dir = new File("C:/pdffiles");
File listDir[] = dir.listFiles();
for (int i = 0; i < listDir.length; i++) 
{
    System.out.println("files"+listDir.length);     
    String file=listDir[i].toString();
    File file1 = new File(file);

    InputStream input = new FileInputStream(file1);           
    Metadata metadata = new Metadata();
    BodyContentHandler handler = new BodyContentHandler(10*1024*1024);
    AutoDetectParser parser = new AutoDetectParser();       
    parser.parse(input, handler, metadata);

    Map<String, String> map = new HashMap<String, String>();
    map.put("File name: ", listDir[i].getName());
    map.put("Title: " , metadata.get("title"));
    map.put("Author: " , metadata.get("Author"));
    map.put("Content type: " , metadata.get("Content-Type"));


    JSONObject json = new JSONObject();
    json.accumulateAll(map);

    FileWriter file2;
    file2 = new FileWriter("C:\\test.json");
    file2.write(json.toString());
    file2.flush();
}

But it is writing only single file metadata to the json file. Is there any problem with my code, please suggest me.

yeaaaahhhh..hamf hamf
  • 746
  • 2
  • 13
  • 34
user2353439
  • 489
  • 2
  • 7
  • 18

1 Answers1

0

may be you should use- file2.write(json.toJSONString()); instead of this line - file2.write(json.toString());

alena_fox_spb
  • 697
  • 1
  • 8
  • 24