Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Practice makes perfect

Experience is often your best defense against Java pitfalls

  • Print
  • Feedback

Page 6 of 9

E:\classes\com\javaworld\jpitfalls\article5>java com.javaworld.jpitfalls.article5.GoodVisitor
one
three
four
What's really there...
one
three
four


Notice that the Iterator implementation class, not the Vector class, calls the remove() method.

Pitfall 10 is caused by the expectation that a class will do more than it does. Specifically, the Vector implementation of the Enumeration interface is assumed to work in conjunction with the underlying collection's modification. Implementations of Iterator fixed the pitfall by including a remove() method in the Iterator interface.

Pitfall 11: When File.renameTo() doesn't

Craig Minton, another reader, suggested that the File.renameTo() method suffered pitfalls in both design and implementation. Listing 11.1 demonstrates that method's behavior and pitfalls:

Listing 11.1. BadFileRename.java

package com.javaworld.jpitfalls.article5;
import java.io.*;
public class BadFileRename
{
    public static void main(String args[])
    {       
        try
        {
            // Check if test file in current dir
            File f = new File("dummy.txt");
            String name = f.getName();
            if (f.exists())
                System.out.println(f.getName() + " exists.");
            else
                System.out.println(f.getName() + " does not exist.");
            // Attempt to rename to an existing file
            File f2 = new File("dummy.bin");
            // Issue 1: boolean status return instead of Exceptions
            if (f.renameTo(f2))
                System.out.println("Rename to existing File Successful.");
            else
                System.out.println("Rename to existing File Failed.");
            // Attempt to rename with a different extension
            int dotIdx = name.indexOf('.');
            if (dotIdx >= 0)
                name = name.substring(0, dotIdx);
            name = name + ".tst";
            String path = f.getAbsolutePath();
            int lastSep = path.lastIndexOf(File.separator);
            if (lastSep > 0)
                path = path.substring(0,lastSep);
            System.out.println("path: " + path);
            File f3 = new File(path + File.separator + name);
            System.out.println("new name: " + f3.getPath());
            if (f.renameTo(f3))
                System.out.println("Rename to new extension Successful.");
            else
                System.out.println("Rename to new extension failed.");
            
            // Delete the file
            // Issue 2: Is the File class a file?
            if (f.delete())
                System.out.println("Delete Successful.");
            else
                System.out.println("Delete Failed.");
            // Assumes program not run from c drive
            // Issue 3: Behavior across operating systems?
            File f4 = new File("c:\\" + f3.getName());
            if (f3.renameTo(f4))
                System.out.println("Rename to new Drive Successful.");
            else
                System.out.println("Rename to new Drive failed.");        
        } catch (Throwable t)
         {
            t.printStackTrace();   
         }       
    }        
}


When Listing 11.1 runs from a drive other than C and with the file dummy.txt in the current directory, it produces the following output:

  • Print
  • Feedback

Resources