Java基础之访问文件与目录——测试文件或目录的路径(TryPath)
控制台程序,测试文件或目录的路径。
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)的更多相关文章
- Java基础——protected访问修饰符探讨
Java基础——protected访问修饰符探讨 根据官方说法:(如图) protected修饰符是可以修饰其他包中的子孙类的,但是我做了个实验,结果发现了一个有趣的现象! 具体请往下看: packa ...
- Java基础之访问权限控制
Java基础之访问权限控制 四种访问权限 Java中类与成员的访问权限共有四种,其中三种有访问权限修饰词:public,protected,private. Public:权限最大,允许所有类访问,但 ...
- 【Java基础】通过getResourceAsStream() 加载资源文件
Class.getResourceAsStream(String path) path不以"/"开头时,默认是从当前类所在的包下面获取资源 path以"/"开头 ...
- Java基础学习(六)-- 递归以及文件I/O流基础详解
递归 1.递归的概念: 在函数自身内部,调用函数本身的方式,称为递归. 2.递归的注意事项:包括递进去,归出来两步. 即:首先依次执行[函数调自身语句]上半部分的代码,知道最里层.(递进去),然后 ...
- java基础十[包、Jar存档文件和部署](阅读Head First Java记录)
将Java的class文件生成为可执行的Java应用程序.Java应用程序有三种:完全在本机执行的Jar(例如本机的GUI可执行程序):完全在服务器端远程执行的(例如浏览器来进行存取):介于两者之间的 ...
- 【Java基础 】Java7 NIO Files,Path 操作文件
从Java1.0到1.3,我们在开发需要I/O支持的应用时,要面临以下问题: 没有数据缓冲区或通道的概念,开发人员要编程处理很多底层细节 I/O操作会被阻塞,扩展能力有限 所支持的字符集编码有限,需要 ...
- Java基础之访问文件与目录——移动或复制文件和目录(MoveAndCopyFiles)
控制台程序,创建和删除目录以及复制和移动文件. import java.nio.file.*; import java.nio.file.attribute.*; import java.io.IOE ...
- Java基础之访问文件与目录——列出目录内容(ListDirectoryContents)
控制台程序,列出目录的全部内容并使用过滤器来选择特定的条目. import java.nio.file.*; import java.io.IOException; public class List ...
- Java基础之访问文件与目录——获取与文件存储有关的信息(GetFileStores)
控制台程序,列出存储在系统中的文件的详细信息 import java.nio.file.FileStore; import java.nio.file.FileSystems; import java ...
随机推荐
- #define与运算精度问题探究
#include <stdio.h> #define SQR(X) X*X int main(int argc, char* argv[]) { ; ; ; printf("SQ ...
- 【转载】Linux系统与性能监控
原文地址:http://kerrigan.sinaapp.com/post-7.html Linux System and Performance Monitoring http://www.hous ...
- jsp页面揣出现Invalid action class configuration that references an unknown class解决方案
jsp页面中,增加和修改用了同一个页面,能正常增加,却不能修改,后来发现页面中有一个hidden的id, 这个input的name写成name="designType.id"时就会 ...
- servlet等一些砸碎的
1:servlet 中的synchronized 关键字能保证一次只有一个线程 2:servlet的线程问题只有在大量的方位时 3:AutoCloseable接口:资源自动关闭 4:EntityUti ...
- json 数组转换为js数组
$(function(){ var json = '[{"id":"1","tagName":"apple"},{&qu ...
- C++ builder的文件操作
在编程的过程中,文件的操作是一个经常用到的问题,在C++Builder中,可以使用多种方法对文件操作,下面我就按以下几个部分对此作详细介绍,就是:1.基于C的文件操作:2.基于C++的文件操作:3.基 ...
- (转)常用的js设计模式
模式是解决或者避免一些问题的方案. 在JavaScript中,会用到一些常用的编码模式.下面就列出了一些常用的JavaScript编码模式,有的模式是为了解决特定的问题,有的则是帮助我们避免一些Jav ...
- freebsd 禁用root登录ssh并给普通用户登录权限
转自http://www.linux521.com/2009/system/200904/2021.html http://www.myhack58.com/Article/48/67/2011/30 ...
- Intervals---poj1201(差分约束系统)
题目链接:http://poj.org/problem?id=1201 题目说[ai, bi]区间内和点集Z至少有ci个共同元素,那也就是说如果我用Si表示区间[0,i]区间内至少有多少个元素的话,那 ...
- 解决调用context.Session["NAME"]时总出现Object reference not set to an instance of an object.异常的方法
if (context.Session != null) { }