Generic Java sour grapes!

I have had 2 minor issues that are paining me, I could well turn out to be my problem. I decided to throw it out to the blogsphere to someone the oppurtunity to say I screwed up (if I have 😉 )

 private void deleteOlderFiles() {
  /* Delete older files */
  File directory = new File(JetForm.OUTPUT_DIR);

  File[] olderfiles = directory.listFiles();

  long purgeTime = System.currentTimeMillis() – (12 * 60 * 60 * 1000);
  // Current time minus 12 hours

  for (int idx = 0; idx < olderfiles.length; idx++) {

   if (olderfiles[idx].lastModified() < purgeTime) {
    if (!olderfiles[idx].delete()) {
     mLogger.error(“Unable to delete olderfiles”);
    } else {
     mLogger.debug(“Deleted file ” + olderfiles[idx].getName());

file.delete() refuses to work! and there is no api call that I can call to see what went wrong! I need to delete my older files before I generate new ones!

Here’s another

 NumberFormat formatter = new DecimalFormat(“#.0”);
 String number = formatter.format(“182345”); // number is now 182345.0
 NumberFormat nf = NumberFormat.getNumberInstance(Locale.CANADA);
 bucketValue = NumberFormat.getNumberInstance(locale).format(Double.parseDouble(number)); // I expect 182,345.0 & I get 182,345

I need the commas and the format to have one decimal point. Im sure theres a simpler way to do this.


9 thoughts on “Generic Java sour grapes!”

  1. Ooops, posted on the wrong article: Dunno about the first one (I’m guessing either a permissions problem or something has a handle open still), but try this for a format string for the second one: #,###.0

  2. At one point, we too could not delete files. It turned out that we were writing to the files earlier and forgot to close the streams.

  3. Regarding the formatting. You could set the minimal number of fraction digits and the grouping separator by hand:

    NumberFormat formatter = new DecimalFormat(“#.0”);
    String number = formatter.format(182345); // 182,345.0

  4. Regarding the formatting:

    NumberFormat nf1 = new DecimalFormat(“#,##.0”);
    NumberFormat nf2 = new DecimalFormat(“#.0”);

    String n0a = nf1.format(0); // .0
    String n0b = nf2.format(0); // .0
    String n123a = nf1.format(123.5); // 123.5
    String n123b = nf2.format(123.5); // 123.5
    String n1234a = nf1.format(1234.5); // 1,234.5
    String n1234b = nf2.format(1234.4); // 1234.5

    So the single difference between nf1 and nf2 is how they format numbers greater than thousand.

  5. At one point, it’s a strange behaviour. I try to delete some files on my Windows host and all right as expected. Maybe you turn out something switch option for JVM?
    And also you should not use bad practice for implicit call of System.gc(). Garbage collector take actions at indefinetly time and you could not guess when it happened.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s