Java源码——文件/文件夹的路径分析(相对/绝对)(File and Directory Info Inquiry)
来源:互联网 发布:表达知错悔恨的古诗 编辑:程序博客网 时间:2024/06/06 00:52
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. 命令行窗口
没搞定,下次再说吧。
0 0
- Java源码——文件/文件夹的路径分析(相对/绝对)(File and Directory Info Inquiry)
- Java源码——使用JFileChooser获取文件及目录信息(Obtain file and directory information)
- java(web)绝对相对路径
- jsp 和 java 文件的绝对/相对路径问题
- How To Get Application Path,And How To Get The Path's Directory,File Name,And Type Info(如何得到运行程序的路径,以及如何得到路径的文件夹,文件名,以及类型等等信息)
- 文件路径(相对、绝对路)、项目设置中的使用
- java File 的相对路径
- java.io.File类基本使用——遍历某路径的所有文件夹及文件
- java 路径(绝对路径/相对路径) 路径名 目录 文件夹 文件
- java中的绝对 相对路径
- URL路径(绝对与相对)
- Java源码——一个简单的余额查询应用(Credit Inquiry Program)
- java文件的相对路径
- java.io.File中的绝对路径和相对路径. (转载)
- (转)java项目的中绝对相对路径问题
- java File的抽象路径名、相对路径、绝对路径
- java 使用相对路径读取文件File(类构造方法)
- java new file 使用相对路径读取文件
- UVA 514 栈判断出栈序列是否合法
- HDOJ--2503 a/b + c/d
- python机器学习——KNN算法简单入门(真的很简单!)
- RobotFramework之Collections
- Python 命名规范
- Java源码——文件/文件夹的路径分析(相对/绝对)(File and Directory Info Inquiry)
- Android神技之 使用SVG以及自定义IconFont字体库
- QT程序,如何在ARM板上运行
- Matlab的mapminmax函数说明
- 关于revit二次开发中,用winform剖切房间进行房间定位的问题
- ArrayList与LinkedList源码解析
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- 两招让你成为牛X的T型人才
- 从客户端中检测到有潜在危险的 Request.Form 值的详细解决方法