控制台程序,测试文件或目录的路径。

 import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.io.IOException; public class TryPath{
public static void main(String[] args){
FileSystem fileSystem=FileSystems.getDefault(); Path path=fileSystem.getPath("garbage.java");
checkPath(path); path=Paths.get(System.getProperty("user.dir"));
checkPath(path); //Amend the following path to your environment
path=fileSystem.getPath("E:","JavaProject","BeginningJava","Ch9_Directories","TryPath","TryPath.java");
checkPath(path); return;
} //check the attributes of a path
static void checkPath(Path path){
System.out.println("\n"+path+" has "+path.getNameCount() + " elements."); if(path.isAbsolute()){
System.out.println(path + "is an absolute path.");
System.out.println("The parent path is "+path.getParent());
System.out.println("The root is "+path.getRoot());
}
else{
System.out.println(path + " is a relative path.");
path=path.toAbsolutePath();
} if(Files.notExists(path)){
System.out.println(path + " does not exist.");
return;
} try {
BasicFileAttributes attr=Files.readAttributes(path,BasicFileAttributes.class); if(attr.isDirectory())
System.out.println(path.getFileName() + " is a directory.");
else if(attr.isRegularFile()){
System.out.println(path.getFileName() + " is a file containing " + attr.size() + " bytes.");
}
System.out.println(path + " was created " + attr.creationTime());
System.out.println(path + " was last accessed " + attr.lastAccessTime());
System.out.println(path + " was last modified " + attr.lastModifiedTime());
System.out.println(path + " is " + attr.size() + " bytes.");
}catch(IOException e){
System.err.println(e);
}
}
}

Java基础之访问文件与目录——测试文件或目录的路径(TryPath)的更多相关文章

  1. Java基础——protected访问修饰符探讨

    Java基础——protected访问修饰符探讨 根据官方说法:(如图) protected修饰符是可以修饰其他包中的子孙类的,但是我做了个实验,结果发现了一个有趣的现象! 具体请往下看: packa ...

  2. Java基础之访问权限控制

    Java基础之访问权限控制 四种访问权限 Java中类与成员的访问权限共有四种,其中三种有访问权限修饰词:public,protected,private. Public:权限最大,允许所有类访问,但 ...

  3. 【Java基础】通过getResourceAsStream() 加载资源文件

    Class.getResourceAsStream(String path) path不以"/"开头时,默认是从当前类所在的包下面获取资源 path以"/"开头 ...

  4. Java基础学习(六)-- 递归以及文件I/O流基础详解

    递归 1.递归的概念: 在函数自身内部,调用函数本身的方式,称为递归. 2.递归的注意事项:包括递进去,归出来两步.   即:首先依次执行[函数调自身语句]上半部分的代码,知道最里层.(递进去),然后 ...

  5. java基础十[包、Jar存档文件和部署](阅读Head First Java记录)

    将Java的class文件生成为可执行的Java应用程序.Java应用程序有三种:完全在本机执行的Jar(例如本机的GUI可执行程序):完全在服务器端远程执行的(例如浏览器来进行存取):介于两者之间的 ...

  6. 【Java基础 】Java7 NIO Files,Path 操作文件

    从Java1.0到1.3,我们在开发需要I/O支持的应用时,要面临以下问题: 没有数据缓冲区或通道的概念,开发人员要编程处理很多底层细节 I/O操作会被阻塞,扩展能力有限 所支持的字符集编码有限,需要 ...

  7. Java基础之访问文件与目录——移动或复制文件和目录(MoveAndCopyFiles)

    控制台程序,创建和删除目录以及复制和移动文件. import java.nio.file.*; import java.nio.file.attribute.*; import java.io.IOE ...

  8. Java基础之访问文件与目录——列出目录内容(ListDirectoryContents)

    控制台程序,列出目录的全部内容并使用过滤器来选择特定的条目. import java.nio.file.*; import java.io.IOException; public class List ...

  9. Java基础之访问文件与目录——获取与文件存储有关的信息(GetFileStores)

    控制台程序,列出存储在系统中的文件的详细信息 import java.nio.file.FileStore; import java.nio.file.FileSystems; import java ...

随机推荐

  1. pycharm使用笔记

    Basic code completion (the name of any class, method or variable) control + 空格  # 代码补全,如果跟系统spotligh ...

  2. Remote 的远程使用

        <script type="text/javascript">        $(function () {            //每次隐藏的时候 删除页面 ...

  3. Nginx 配置文件模板

    user www www; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/loc ...

  4. poj1012约瑟夫

    #include<stdio.h>int a[14];int f(int k,int m){    int n,i,s;    n=2*k;s=0;    for(i=0;i<k;i ...

  5. 【重要】ASCII码表

    我们在做业务项目,客户端的输入总是无法控制,有各种各样的特殊字符,这些特殊字符就要借助ASCII码表才能判断,所以我做了一张图,方便查看 为什么要搞个表出来,下面的字符串中,你看看你能否看的出来是什么 ...

  6. 单选按钮控件(Ridio Button)的使用

    VC学习笔记5:单选按钮控件(Ridio Button)的使用 一.对单选按钮进行分组: 每组的第一个单选按钮设置属性:Group,Tabstop,Auto;其余按钮设置属性Tabstop,Auto. ...

  7. Uploadify上传问题

    版本:Uploadify Version 3.2官网:http://www.uploadify.com Uploadify是一款基于Jquery的上传插件,用起来很方便.但上传过程中的提示语言为英文, ...

  8. Abstract Algebra chapter 7

    7.7:Encrypt each of the following RSA messages x so that x is divided into blocks of integers of len ...

  9. Netty 4(一) zero copy

    Netty的“零拷贝”主要体现在如下三个方面: 1) Netty的接收和发送ByteBuffer采用DIRECT BUFFERS,使用堆外直接内存进行Socket读写,不需要进行字节缓冲区的二次拷贝. ...

  10. 规则html表单对象赋值

    function grid_load_callback(data, status) {            if (data.rows.length > 0)            {     ...