JHTP (Java how to program)中只给出了绝对路径的测试示例,自己查阅了网上资料,学会了相对路径的测试方法(在Eclipse)。
代码如下:
// Fig. 15.2: FileAndDirectoryInfo.java
// File class used to obtain file and directory information.
package ch15;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
public class FileAndDirectoryInfo
{
public static void main(String[] args) throws IOException
{
Scanner input = new Scanner(System.in);
System.out.println("Enter file or directory name:");
// create Path object based on user input
Path path = Paths.get(input.nextLine());
if (Files.exists(path)) // if path exists, output info about it
{
// display file (or directory) information
System.out.printf("%n%s exists%n", path.getFileName());
System.out.printf("%s a directory%n",
Files.isDirectory(path) ? "Is" : "Is not");
System.out.printf("%s an absolute path%n",
path.isAbsolute() ? "Is" : "Is not");
System.out.printf("Last modified: %s%n",
Files.getLastModifiedTime(path));
System.out.printf("Size: %s%n", Files.size(path));
System.out.printf("Path: %s%n", path);
System.out.printf("Absolute path: %s%n", path.toAbsolutePath());
if (Files.isDirectory(path)) // output directory listing
{
System.out.printf("%nDirectory contents:%n");
// object for iterating through a directory's contents
DirectoryStream<Path> directoryStream =
Files.newDirectoryStream(path);
for (Path p : directoryStream)
System.out.println(p);
}
}
else // not file or directory, output error message
{
System.out.printf("%s does not exist%n", path);
}
}
} // end class FileAndDirectoryInfo
测试结果:
1. Eclipse环境:
1). 绝对路径
Enter file or directory name:
C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15
C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15
ch15 exists
Is a directory
Is an absolute path
Last modified: 2017-04-29T16:01:31.563936Z
Size: 0
Path: C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15
Absolute path: C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15
Is a directory
Is an absolute path
Last modified: 2017-04-29T16:01:31.563936Z
Size: 0
Path: C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15
Absolute path: C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15
Directory contents:
C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\FileAndDirectoryInfo.java
C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\test
C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\FileAndDirectoryInfo.java
C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\test
2). 相对路径
nter file or directory name:
src\ch15\test
src\ch15\test
test exists
Is a directory
Is not an absolute path
Last modified: 2017-04-29T16:04:17.5139Z
Size: 4096
Path: src\ch15\test
Absolute path: C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\test
Is a directory
Is not an absolute path
Last modified: 2017-04-29T16:04:17.5139Z
Size: 4096
Path: src\ch15\test
Absolute path: C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\test
Directory contents:
src\ch15\test\helloJava.java
src\ch15\test\relative path test.xlsx
src\ch15\test\相对路径测试文件.rtf
src\ch15\test\helloJava.java
src\ch15\test\relative path test.xlsx
src\ch15\test\相对路径测试文件.rtf
3)绝对路径(文件名)
Enter file or directory name:
C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\test\helloJava.java
C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\test\helloJava.java
helloJava.java exists
Is not a directory
Is an absolute path
Last modified: 2017-04-29T16:04:09.99025Z
Size: 0
Path: C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\test\helloJava.java
Absolute path: C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\test\helloJava.java
Is not a directory
Is an absolute path
Last modified: 2017-04-29T16:04:09.99025Z
Size: 0
Path: C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\test\helloJava.java
Absolute path: C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\test\helloJava.java
4)相对路径(文件名)
Enter file or directory name:
src\ch15\test\helloJava.java
src\ch15\test\helloJava.java
helloJava.java exists
Is not a directory
Is not an absolute path
Last modified: 2017-04-29T16:04:09.99025Z
Size: 0
Path: src\ch15\test\helloJava.java
Absolute path: C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\test\helloJava.java
Is not a directory
Is not an absolute path
Last modified: 2017-04-29T16:04:09.99025Z
Size: 0
Path: src\ch15\test\helloJava.java
Absolute path: C:\01.iworkspace\eclipseWorkspace\jhtp_2017\src\ch15\test\helloJava.java
2. 命令行窗口
没搞定,下次再说吧。