|
|
|
|
The File class encapsulates the attributes and behavior needed to represent a file or a directory on the native system. The File class is not concerned with the contents of the file; it is concerned with the storage of the file on disk. You can use an instance of the File class to perform certain operations on a corresponding file on the native system:
Note that, while you can create a directory using the Using Class
|
package inputOutput;
import java.io.File;
public class RenameFile
{
public static void main(String[] args)
{
File fromFile = new File("myFile.txt");
File toFile = new File("yourFile.txt");
boolean renamed = fromFile.renameTo(toFile);
if (renamed)
System.out.println("File was successfully renamed");
else
System.out.println("File was not renamed");
}
} |
Note that no exception is thrown when the rename fails;
you have to check the return value. The delete() method has the same return
value convention.
If you wish to write portable Java code, do not do the following:
package inputOutput;
import java.io.File;
/**
* This class creates an instance of File in a non-portable way.
*/
public class FilePathName
{
public static void main(String[] args)
{
File file = new File("path/path1/name.txt");
// ...
}
} |
The above is UNIX-specific (although it will also work on Microsoft Windows). It can't be assumed to work on other platforms.
Class File has the
following, which you might think would help:
public static final String
separator -- set to the value of the System
property file.separatorpublic static final char
separatorChar -- set to the value of the System
property file.separator(Note that File also has
the following:
public static final String
pathSeparator -- set to the value of the
System property path.separatorpublic static final char
pathSeparatorChar -- set to the value of the
System property path.separatorbut these relate to CLASSPATH separator characters, not
file path separator characters.)
For example, you might think that something like:
package inputOutput;
import java.io.File;
/**
* This class creates an instance of File in a non-portable way.
*/
public class FilePathName
{
public static void main(String[] args)
{
File file = new File("path" + File.separator +
"path1" + File.separator +
"name.txt");
// ...
}
} |
would make things portable, but it doesn't. There are some computer systems which do not use the pure file separator convention to separate directories from the files they contain.
How can we make things more portable? Well, take a look at
the constructors available in the File
class:
public File(String path)public File(String path,
String name)public File(File dir, String
name)Let's see how we can use these constructors to solve the portability problem (at least to the extent that we can):
package inputOutput;
import java.io.File;
/**
* This class creates an instance of File in a portable way.
*/
public class FilePathName
{
public static void main(String[] args)
{
File path = new File("path");
path = new File(path, "path1");
path = new File(path, "name.txt");
System.out.println("File path = '" +
path.getAbsolutePath() + "'");
// ...
}
} |
Notice how no explicit separators are used at all. If you
type this in (or copy it from this page and paste it into an
editor), you'll find that the println
produces a full (absolute) path specification (despite the
fact that there is probably no such file on your system).
Note that we entered a relative path specification.
File Is NotIt is important to note that an instance of class File merely represents a
file or a directory. Creating an instance of class File does not
create a corresponding file on the native system. Further, an
instance of class File may
exist without there necessarily existing a corresponding
file. However, using an instance of class File, you can
discover a number of things about the corresponding file,
including whether it exists or not:
canRead() and canWrite()exists()getName() and getPath()isAbsolute()getAbsolutePath() and getCanonicalPath()isFile() and isDirectory()lastModified()length()| The page was last updated February 19, 2008 |