General Techniques for Cross-Platform File Access Code

It's hard to write truly cross-platform file manipulation code. The NIO working group was supposed to fix this years ago. However, they spent so much time on channels and buffers that they never fulfilled their mandate to design a decent, platform-independent filesystem API. Now it looks like it's not going to make it into Java 6 either. Maybe we'll finally get one in Java 7. In the meantime, to help you achieve greater serenity and overall cross-platform nirvana, I've summarized some basic rules from this chapter to help you write file manipulation code that's robust across a multitude of platforms:

  • Never, never, never hardcode pathnames in your application.
  • Ask the user to name your files. If you must provide a name for a file, try to make it fit in an 8.3 DOS filename with only pure ASCII characters.
  • Do not assume the file separator is "/" (or anything else). Use File.separatorChar instead.
  • Do not parse pathnames to find directories. Use the methods of the java.io.File class instead.
  • Do not use renameTo( ) for anything except renaming a file. In particular, do not use it to move a file.
  • Try to avoid moving and copying files from within Java programs if at all possible.
  • Do not use . to refer to the current directory. Use System.getProperty("user.dir") instead.
  • Do not use .. to refer to the parent directory. Use getParent( ) instead.
  • Place any data files your program requires in JAR archives rather than directly in the filesystem and load them as resources from the classpath.
  • When in doubt, it never hurts to convert filenames to canonical form.
  • Do not assume anything about filesystem conventions. Some platform somewhere will surprise you. (Have you tested your program on BeOS yet?)
  • Test your code on as many different filesystems as you can get your hands on.

Despite all the problems I've pointed out, it is possible to write robust file access code that works across all platforms where Java runs, but doing so requires understanding, effort, and thought. You cannot simply write for Windows or Unix and hope things will work out for the best on other platforms. You must plan to handle a wide range of filesystems and filename conventions.

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O

Buffers

Channels

Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers

Text

Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text

Devices

The Java Communications API

USB

The J2ME Generic Connection Framework

Bluetooth

Character Sets



Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net