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 9 of 9

E:\classes\com\javaworld\jpitfalls\article5>java com.javaworld.jpitfalls.article5.GoodFileRename
dummy2.txt exists.
path: E:\classes\com\javaworld\jpitfalls\article5
new name: E:\classes\com\javaworld\jpitfalls\article5\dummy2.tst
Rename to new extension Successful.
Delete Successful.


Thus, you shouldn't use the File class as if it represents a file instead of the filename. With that in mind, once the file is renamed, operations such as delete() work only on the new filename.

Experience helps you escape Java traps

In summary, gaining familiarity in Java programming is key to avoiding Java pitfalls. We fixed the traps presented in this article by simply learning the proper functions of certain classes. First, remember to position and size components when adding them to a layer in a JLayeredPane. Second, remember that enumerating over a Vector does not work in conjunction with the remove() operation, as Iterator does. Finally, do not use the File class for operations after a successful renameTo().

I would like to thank everyone who emailed me about this column over the past year. The feedback and support has been wonderful. This column has been a rewarding experience, and I will return after a break.

Until then ... best wishes.

About the author

Michael C. Daconta is the director of Web and technology services for McDonald Bradley, where he conducts training seminars and develops advanced systems with Java, JavaScript, and XML. Over the past 15 years, Daconta has held every major development position, including chief scientist, technical director, chief developer, team leader, systems analyst, and programmer. He is a Sun-certified Java programmer and coauthor of Java Pitfalls (John Wiley & Sons, 2000; ISBN: 0471361747), Java 2 and JavaScript for C and C++ Programmers (John Wiley & Sons, 1999; ISBN: 0471327190), and XML Development with Java 2 (Sams Publishing, 2000; ISBN: 0672316536). In addition, he is the author of C++ Pointers and Dynamic Memory Management (John Wiley & Sons, 1995; ISBN: 0471049980).
  • Print
  • Feedback

Resources