Java基础之访问文件与目录——列出目录内容(ListDirectoryContents)
控制台程序,列出目录的全部内容并使用过滤器来选择特定的条目。
import java.nio.file.*;
import java.io.IOException; public class ListDirectoryContents {
public static void main (String[] args) {
Path currentPath=Paths.get(System.getProperty("user.dir"));
currentPath=currentPath.getParent(); filterDirectoryContents(currentPath,null); System.out.println("\nFilter for .txt:");
filterDirectoryContents(currentPath,"*.txt"); System.out.println("\nFilter for .j???:");
filterDirectoryContents(currentPath,"*.j???");
} private static void filterDirectoryContents(Path path,String filter) {
try(DirectoryStream<Path> paths=filter != null ? Files.newDirectoryStream(path,filter) : Files.newDirectoryStream(path)){
System.out.println("\n" + path + " directory contains:");
for(Path p :paths){
System.out.println(" " + p.getFileName() + (Files.isDirectory(p) ? " is a directory." : ""));
}
}catch (NotDirectoryException e) {
System.err.println("Path is not a directory." + e);
}catch(IOException e) {
System.err.println("I/O error." + e);
}
}
}
Java基础之访问文件与目录——列出目录内容(ListDirectoryContents)的更多相关文章
- Java基础之访问文件与目录——测试文件或目录的路径(TryPath)
控制台程序,测试文件或目录的路径. import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.FileSy ...
- Java基础之访问文件与目录——获取与文件存储有关的信息(GetFileStores)
控制台程序,列出存储在系统中的文件的详细信息 import java.nio.file.FileStore; import java.nio.file.FileSystems; import java ...
- Java基础之访问文件与目录——移动或复制文件和目录(MoveAndCopyFiles)
控制台程序,创建和删除目录以及复制和移动文件. import java.nio.file.*; import java.nio.file.attribute.*; import java.io.IOE ...
- Java基础之访问文件与目录——创建目录(CreatingDirectories)
控制台程序,使用两种方法来创建目录. import java.nio.file.*; import java.io.IOException; public class CreatingDirector ...
- Java基础之读文件——使用通道读取混合数据2(ReadPrimesMixedData2)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法二:设置一个任意容量的.大小合适的字节缓冲区并且使用来自文件的字节进行填充.然后整理出缓冲区 ...
- Java基础之读文件——使用通道读取混合数据1(ReadPrimesMixedData)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法一:可以在第一个读操作中读取字符串的长度,然后再将字符串和二进制素数值读入到文本中.这种方式 ...
- Java基础之读文件——使用通道读二进制数据(ReadPrimes)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile)写入的primes.bin. import java.nio.file.*; import java.nio.*; import ...
- Java基础之读文件——从文件中读取文本(ReadAString)
控制台程序,使用通道从缓冲区获取数据,读取Java基础之写文件(BufferStateTrace)写入的charData.txt import java.nio.file.*; import java ...
- Java基础之读文件——使用缓冲读取器读取文件(ReaderInputFromFile)
控制台程序,本例读取Java基础之写文件部分(WriterOutputToFile)写入的Saying.txt. import java.io.*; import java.nio.file.*; i ...
随机推荐
- 【IOS笔记】Event Delivery: The Responder Chain
Event Delivery: The Responder Chain 事件分发--响应链 When you design your app, it’s likely that you want t ...
- laravel authorize(授权)
1.方法一 直接在AuthServiceProvider 中定义闭包,比较灵活 namespace App\Providers; ... class AuthServiceProvider exte ...
- 图解SQL多表关联查询
图解SQL多表关联查询 网上看了篇文章关于多表连接的,感觉很好,记录下来,以便日后自己学习 内连接 左连接 右连接 全外连接 1. 查两表关联列相等的数据 ...
- Checklist For Choosing The Right Database Engine
http://sqlite.org/whentouse.html Appropriate Uses For SQLite SQLite is not directly comparable to cl ...
- windows系统中ubuntu虚拟机安装及web项目到服务上(二)
ajp方式整合apache2和tomcat 7 1:在apache2.conf配置文件中启用模块mod_proxy_ajp,在里面添加 LoadModule proxy_module modules/ ...
- JAVA 调用matlab 出错总结
1.Java:Unsupported major.minor version 51.0 (unable to load class 出现该错误是由于class编译器的JDK版本高于运行期的JDK版本. ...
- 我的第一个chrome扩展(1)——读样例,实现时钟
学习chrome扩展开发: 与网页类似,需要的知识:html,javascript chrome扩展程序的构成: manifest.json:对扩展程序的整体描述文件 { "manifest ...
- iOS 键盘隐藏
IOS7 点击空白处隐藏键盘的几种方法 IOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我 ...
- go安装windows源码
直接安装就好,下面是安装地址http://pan.baidu.com/s/1gdDFi9t
- python环境搭建
Python下载 Python最新源码,二进制文档,新闻资讯等可以在Python的官网查看到: Python官网:http://www.python.org/ 你可以在一下链接中下载Python的文档 ...