简体   繁体   中英

How i can write the results in a file

I am trying to write the results of my program in a file but i do not know why its does not write nothing.

I have done a program and it creates a file but when I open the file, it is empty. What i have done wrong?

MongoClient mongoClient;
DB db;

mongoClient = new MongoClient("localhost", 27017);
db = mongoClient.getDB("behaviourDB_areas");  

DBCollection cEvent = db.getCollection("events_Searching");                                     
File file = new File ("C:\\Users\\Nikos\\Documents\\Apotelesmata\\file1.txt");
file.getParentFile().mkdirs();

PrintWriter writer = new PrintWriter (file);    

 BasicDBObject orderBy = new BasicDBObject();
 orderBy.put("timeStamp",1);

 DBCursor cursorEvents = null;

 BasicDBObject searchQuery = new BasicDBObject();
 searchQuery.put("user_id", "55b20db905f333defea9827f");

 cursorEvents = cEvent.find(searchQuery).sort(orderBy);

 int count = 1;
 int start = 1;
 String timeStartOld = null;

 while (cursorEvents.hasNext()) {

    DBObject documentInEventCollection = cursorEvents.next();

    if("pageLoad".equals(documentInEventCollection.get("type"))){ 

        writer.println("URL(" + count + "): " + documentInEventCollection.get("url").toString());
        //System.out.println("time-start(" + start + "): " + documentInEventCollection.get("timeStamp").toString());
        count++;
        start++;

        try {
           String timeStart = (documentInEventCollection.get("timeStamp").toString());

           if(timeStartOld==null){
               timeStartOld = timeStart;
               continue;
           }

           SimpleDateFormat format = new SimpleDateFormat("yyyy-dd-MM;HH:mm:ss");

           Date d1 = null;
           Date d2 = null;

           d1 = format.parse(timeStartOld);
           d2 = format.parse(timeStart);

           //in milliseconds
           long diff = d2.getTime() - d1.getTime();

           long diffSeconds = diff / 1000 % 60;
           long diffMinutes = diff / (60 * 1000) % 60;
           long diffHours = diff / (60 * 60 * 1000) % 24;
           long diffDays = diff / (24 * 60 * 60 * 1000);

           writer.println(diffDays + " days, ");
           writer.println(diffHours + " hours, ");
           writer.println(diffMinutes + " minutes, ");
           writer.println(diffSeconds + " seconds.");

           timeStartOld = timeStart;

      } catch (Exception e) {
           e.printStackTrace();
      }
  }
  writer.close();
}   
mongoClient.close();

Thank you for your answer.

Try if the below statement is getting printed in console.

System.out.println("time-start(" + start + "): " + documentInEventCollection.get("timeStamp").toString());

If not, execution is not entering the below if statement. Try some conditions where the if gets true .

 if("pageLoad".equals(documentInEventCollection.get("type"))){  ...

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