Java源码——文件/文件夹的路径分析(相对/绝对)(File and Directory Info Inquiry)

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
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
Directory contents:
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
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
Directory contents:
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
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

4)相对路径(文件名)
Enter file or directory name:
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

2. 命令行窗口

没搞定,下次再说吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值