java- 文件路径

The subject of file names is actually more complicated than I've let on so far. To fully specify a file, you have to give both the name of the file and the name of the directory where that file is located. A simple file name like "data.dat" or "result.dat" is taken to refer to a file in a directory that is called the current directory (also known as the "default directory" or "working directory"). The current directory is not a permanent thing. It can be changed by the user or by a program. Files not in the current directory must be referred to by a path name, which includes both the name of the file and information about the directory where it can be found.

To complicate matters even further, there are two types of path names, absolute path names and relative path names. An absolute path name uniquely identifies one file among all the files available to the computer. It contains full information about which directory the file is in and what the file's name is. A relative path name tells the computer how to locate the file starting from the current directory.

Unfortunately, the syntax for file names and path names varies somewhat from one type of computer to another. Here are some examples:

data.dat -- on any computer, this would be a file named "data.dat" in the current directory.
/home/eck/java/examples/data.dat -- This is an absolute path name in a UNIX operating system, including Linux and Mac OS X. It refers to a file named data.dat in a directory named examples, which is in turn in a directory named java, ....
C:\eck\java\examples\data.dat -- An absolute path name on a Windows computer.
examples/data.dat -- a relative path name under UNIX. "examples" is the name of a directory that is contained within the current directory, and data.dat is a file in that directory. The corresponding relative path name for Windows would be examples\data.dat.
../examples/data.dat -- a relative path name in UNIX that means "go to the directory that contains the current directory, then go into a directory named examples inside that directory, and look there for a file named data.dat." In general, ".." means "go up one directory." The corresponding path on Windows is ..\examples\data.dat.
When working on the command line, it's safe to say that if you stick to using simple file names only, and if the files are stored in the same directory with the program that will use them, then you will be OK. Later in this section, we'll look at a convenient way of letting the user specify a file in a GUI program, which allows you to avoid the issue of path names altogether.

It is possible for a Java program to find out the absolute path names for two important directories, the current directory and the user's home directory. You can then use the path name, for example, in a constructor for a File or a PrintWriter. The names of these directories are system properties, and they can be read using the function calls:

System.getProperty("user.dir") -- returns the absolute path name of the current directory as a String.
System.getProperty("user.home") -- returns the absolute path name of the user's home directory as a String.
To avoid some of the problems caused by differences in path names between platforms, Java has the class java.io.File. An object belonging to this class represents a file. More precisely, an object of type File represents a file name rather than a file as such. The file to which the name refers might or might not exist. Directories are treated in the same way as files, so a File object can represent a directory just as easily as it can represent a file.

A File object has a constructor, "new File(String)", that creates a File object from a path name. The name can be a simple name, a relative path, or an absolute path. For example, new File("data.dat") creates a File object that refers to a file named data.dat, in the current directory. Another constructor, "new File(File,String)", has two parameters. The first is a File object that refers to a directory. The second can be the name of the file in that directory or a relative path from that directory to the file.

File objects contain several useful instance methods. Assuming that file is a variable of type File, here are some of the methods that are available:

file.exists() -- This boolean-valued function returns true if the file named by the File object already exists. You can use this method if you want to avoid overwriting the contents of an existing file when you create a new output stream.
file.isDirectory() -- This boolean-valued function returns true if the File object refers to a directory. It returns false if it refers to a regular file or if no file with the given name exists.
file.delete() -- Deletes the file, if it exists. Returns a boolean value to indicate whether the file was successfully deleted.
file.list() -- If the File object refers to a directory, this function returns an array of type String[] containing the names of the files in that directory. Otherwise, it returns null. The method file.listFiles() is similar, except that it returns an array of File instead of an array of String.
Here, for example, is a program that will list the names of all the files in a directory specified by the user. In this example, I have used a Scanner to read the user's input:

import java.io.File;
import java.util.Scanner;

/**

  • This program lists the files in a directory specified by
  • the user. The user is asked to type in a directory name.
  • If the name entered by the user is not a directory, a
  • message is printed and the program ends.
    */
    public class DirectoryList {

public static void main(String[] args) {

  String directoryName;  // Directory name entered by the user.
  File directory;        // File object referring to the directory.
  String[] files;        // Array of file names in the directory.
  Scanner scanner;       // For reading a line of input from the user.

  scanner = new Scanner(System.in);  // scanner reads from standard input.

  System.out.print("Enter a directory name: ");
  directoryName = scanner.nextLine().trim();
  directory = new File(directoryName);
  
  if (directory.isDirectory() == false) {
      if (directory.exists() == false)
         System.out.println("There is no such directory!");
      else
         System.out.println("That file is not a directory.");
  }
  else {
      files = directory.list();
      System.out.println("Files in directory \"" + directory + "\":");
      for (int i = 0; i < files.length; i++)
         System.out.println("   " + files[i]);
  }

} // end main()

} // end class DirectoryList
All the classes that are used for reading data from files and writing data to files have constructors that take a File object as a parameter. For example, if file is a variable of type File, and you want to read character data from that file, you can create a FileReader to do so by saying new FileReader(file).

@参考文献

2019 年 05月 19日

你可能感兴趣的:(java- 文件路径)