1 Reply Latest reply: Oct 18, 2012 4:08 PM by EJP RSS

    PrintWriter doesn't write to File even with flush()

    tmazight
      In my code i have two classes. Class DAO who create the folder in a static clock, and class DAOPersist who extends DAO class,get the the trace folder, create a file to it and begin writing with a printWriter.

      Excuse me , I don't remeber how to use code fragment.

      DAO Class
      { static {
          //Création du répértoire de Trace
          fileTrace = new File(lUriTraceFolder);
          traceDest = fileTrace + "/";
          File lTraceFileDest = new File(traceDest);
          lTraceFileDest.mkdir();
      }
      protected String getTraceDest() {
          return traceDest;
      }
      }
      DAOPersist
      {
      Calendar cal = Calendar.getInstance();
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          String dateStr = sdf.format(cal.getTime());
          FileWriter fLogDAO = null;
          PrintWriter out = null;
      
      try {
                      String fichier=super.getTraceDest()  + "logDAO_" + dateStr
                              + ".log";
                      fLogDAO = new FileWriter(fichier, true);
                      out = new PrintWriter(fLogDAO);
                      out.println("------------- ");
                      out.println("DAOTLE.insert() : before insertion: Application");
                      out.println("TLE number: " + pTLE.getMNumber() + "; date: "
                              + pTLE.getMDate() + " ; Logintude: "
                              + pTLE.getMLongitude());
      
                      out.println("DAOTLE.insert(): before insertion: Database");
                      Statement stmt = null;
                      String sql = "SELECT last_modified,number, date, longitude FROM tle WHERE number ="
                              + pTLE.getMNumber()
                              + " ORDER BY tle_id DESC limit 1";
                      stmt = super.getmConn().createStatement();
                      lRs = stmt.executeQuery(sql);
                      // Extract data from result set
                      while (lRs.next()) {
                          // Retrieve by column name
                          int number = lRs.getInt("number");
                          Date date = lRs.getDate("date");
                          float longitude = lRs.getFloat("longitude");
      
                          // Display values
                          out.println("TLE number: " + number + "| date: " + date
                                  + " | Logintude: " + longitude);
                      }
                  } catch (IOException e) {
                      LOGGER.warn("DAOTLE : insert :  Cannot write to trace file");
                  } finally {
                      System.out.println(traceDest);
                      out.flush();
                      out.close();
                      try {
                          fLogDAO.close();
                      } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  }
      }
      This Code creates the file called "logDAO_18-10-2012....." but doesn't write to it. Any idea!!

      Edited by: tmazight on Oct 18, 2012 5:18 PM

      Edited by: sabre150 on Oct 18, 2012 7:12 PM

      Moderator action : added [code ] tags.