23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> I/O<br />

findResource() methods of java.lang.Class or java.lang.ClassLoader. This works<br />

irrespective of the current working directory as long as the JAR archive has been placed<br />

somewhere in the class path.<br />

12.3 The File Class<br />

When you consider all the difficulties of writing file access code that<br />

works on all the platforms <strong>Java</strong> supports, you can become grateful that<br />

applets aren't allowed to write files. Although this limits the sort of<br />

applets you can write, perhaps what's really needed is not unlimited (or<br />

even limited) file access but some non-file-oriented persistent storage<br />

mechanism, probably some sort of database, that hides the<br />

idiosyncrasies of each individual platform. <strong>Java</strong>Soft is now working on<br />

exactly such a mechanism called <strong>Java</strong>Spaces, though it probably won't<br />

appear in web browsers for some time<br />

Instances of the java.io.File class represent filenames on the local system, not actual files.<br />

Occasionally, this distinction is crucial. For instance, File objects can represent directories as<br />

well as files. Also, you cannot assume that a file exists just because you have a File object<br />

for a file.<br />

public class File extends Object implements Serializable<br />

In <strong>Java</strong> 2, the File class also implements the java.lang.Comparable interface:<br />

public class File extends Object implements Serializable, Comparable //<br />

<strong>Java</strong> 2<br />

Although there are no guarantees that a file named by a File object actually exists, the File<br />

class does contain many methods for getting information about the attributes of a file and for<br />

manipulating those files. The File class attempts to account for system-dependent features<br />

like the file separator character and file attributes, though in practice it doesn't do a very good<br />

job, especially in <strong>Java</strong> 1.0 and 1.1.<br />

Each File object contains a single String field called path that contains either a relative or<br />

absolute path to the file, including the name of the file or directory itself:<br />

private String path<br />

Many methods in this class work solely by looking at this string. They do not necessarily look<br />

at any part of the filesystem.<br />

12.3.1 Constructing File Objects<br />

The java.io.File class has three constructors. Each accepts some variation of a filename as<br />

an argument. This one is the simplest:<br />

public File(String path)<br />

280

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!