Files 类使用

package com.xinyu.test;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.List; public class FileTest { public static void main(String[] args) {
// TODO Auto-generated method stub //Files创建,删除
// Path path =Paths.get("D:/jun/xinyu.txt");
// try {
// Files.createFile(path);
// Files.deleteIfExists(path);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } //Files复制,移动 // try {
// Path path = Paths.get("D:/jun/psb (4).jpg");
// Path target =Paths.get("D:/test/jun.jpg");
// Path parent = target.getParent();
// boolean exists = parent.toFile().exists();
// if(!exists){
// Files.createDirectories(parent);
// }
//// Files.copy(path, target, StandardCopyOption.REPLACE_EXISTING);
// Files.move(path, target, StandardCopyOption.REPLACE_EXISTING);
//
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } //查看文件属性
// Path path = Paths.get("D:/liebao/debug.log");
// try {
// System.out.println(Files.getLastModifiedTime(path));
// System.out.println(Files.size(path));
// System.out.println(Files.isDirectory(path));
// System.out.println(Files.readAttributes(path, "*"));
// System.out.println();
// System.out.println();
// System.out.println(19_12);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } //简化的文件读写
// Path path =Paths.get("D:/liebao/debug.log");
// try {
// List<String> readAllLines = Files.readAllLines(path,StandardCharsets.UTF_8);
// for (String entry : readAllLines){
// System.out.println(entry);
// }
//
// byte[] readAllBytes = Files.readAllBytes(path);
// System.out.println(new String(readAllBytes));
//
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } //读取文件的最后几个字节
Path path=Paths.get("D:/liebao/test.txt");
try {
FileChannel open = FileChannel.open(path, StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate(1024);
//从第23个字节开始读
// int read = open.read(buffer,open.size()-(open.size()-23));/
// open.read(buffer, 2, 10);
// System.out.println(new String(buffer.array()));
// System.out.println(open.size()); //下面的一直报错,不管了先
ByteBuffer[] dsts = new ByteBuffer[12];
System.out.println(dsts.length);
System.out.println(open == null);
open.read(dsts, 1, 3); System.out.println(dsts[0].array());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

Paths类的使用:

package com.xinyu.test;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; public class PathTest { public static void main(String[] args) {
// TODO Auto-generated method stub
//Path 基本操作
// Path path = Paths.get("D:/jun");
// System.out.println(path.getFileName());
// System.out.println(path.getFileSystem());
// System.out.println(path.getNameCount());
// System.out.println(path.getParent()); //path 的合并
// Path path = Paths.get("D:/");
// Path resolve = path.resolve("jun");
// System.out.println(resolve.getFileName());
// System.out.println(resolve.getFileSystem());
// System.out.println(resolve.getNameCount());
// System.out.println(resolve.getParent()); //从path到path2的相对路径,输出:..\cebdoc
// Path path = Paths.get("D:/jun");
// Path path2 = Paths.get("D:/cebdoc");
// Path relativize = path.relativize(path2);
// System.out.println(relativize); //path的一些其他操作,包括startWith,endWith
// Path path = Paths.get("D:/jun");
// Path path2 = Paths.get("D:/cebdoc");
// boolean startsWith = path.startsWith(path2);
// System.out.println(startsWith); //jdk1.7之后 path 和file是可以相互转换的
// File file = new File("D:/jun");
// Path path3 = file.toPath();
// file =path3.toFile(); //path遍历目录 只输出制定的文件 列出目录下的所有txt文件
// Path path = Paths.get("D:\\jun");
// try {
// DirectoryStream<Path> stream = Files.newDirectoryStream(path,"*.txt");
// for (Path entry : stream){
// System.out.println(entry.getFileName());
// }
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } //path遍历目录,以及目录下的目录 找出是所有的doc文件
class FindJavaVisitor extends SimpleFileVisitor<Path>{
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// TODO Auto-generated method stub
if(file.toString().endsWith(".txt")){
System.out.println(file.getFileName());
}
return super.visitFile(file, attrs);
} }
Path path = Paths.get("D:\\software");
try {
Files.walkFileTree(path, new FindJavaVisitor());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

文件观察服务的使用

WatchService

package com.xinyu.test;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService; public class FilWatchServicesTest {
public static void main(String[] args) {
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
Path path =Paths.get("D:/jun");
path.register(watchService,StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
WatchKey take = watchService.take();
for (WatchEvent<?> event : take.pollEvents()) {
if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
System.out.println("i is delete");
}
if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
System.out.println("i is modify");
} }
take.reset(); }
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

java 1.7 新io 实践 NIO2的更多相关文章

  1. java 21 - 15 新IO流 NIO

    1:JDK4  新IO要了解的类 Buffer(缓冲),Channer(通道) 2:JDK7  要了解的新IO类 Path:与平台无关的路径. Paths:包含了返回Path的静态方法. public ...

  2. Java 新IO

       NIO提供全新的底层I/O模型.与最初的java.io包中面向流(stream-oriented)概念不同,NIO采用了面向块的概念(block-oriented).在尽可能的情况下,I/O的操 ...

  3. JAVA(六)数据库/网络编程/新IO

    成鹏致远 | lcw.cnblog.com |2014-02-05 数据库 1.JDBC概述 JDBC(Java Database Connectivity,Java数据库连接)提供了一种与平台无关的 ...

  4. Java -- 新IO -- 目录

    20.1 Java 新IO简介 20.2 缓冲区与Buffer 例:演示缓冲区的操作流程 Class : IntBufferDemo01 20.2.2 深入缓冲区操作 20.2.3 创建子缓冲区 20 ...

  5. Java 8特性尝鲜:新新IO

    Java 8特性尝鲜:新新IO 在这个专题前面的文章中,我们已经看到,使用Java8的lambda表达式对现有的JDK1.2 I/O库的提升,主要是可以使用lambda表达式来构造java.io.Fi ...

  6. java 提供了哪些IO方式

    今天听了杨晓峰老师的java 36讲,感觉IO这块是特别欠缺的,所以讲义摘录如下: 欢迎大家去订阅: 本文章转自:https://time.geekbang.org/column/article/83 ...

  7. Java SE 6 新特性: Java DB 和 JDBC 4.0

    http://www.ibm.com/developerworks/cn/java/j-lo-jse65/index.html 长久以来,由于大量(甚至几乎所有)的 Java 应用都依赖于数据库,如何 ...

  8. [转] Java 8的新特性

    简介 毫无疑问,Java 8是Java自Java 5(发布于2004年)之后的最重要的版本.这个版本包含语言.编译器.库.工具和JVM等方面的十多个新特性.在本文中我们将学习这些新特性,并用实际的例子 ...

  9. Java 8的新特性—终极版

    作者:杜琪[译] 原文链接:http://www.jianshu.com/p/5b800057f2d8 1. 简介 毫无疑问,Java 8是Java自Java 5(发布于2004年)之后的最重要的版本 ...

随机推荐

  1. 翻译-ExcelDNA开发文档-首页

    转载自个人主页 前言 ExcelDNA是一名国际友人开发的开源框架,文档全是英文文档,当时看的时候非常吃力,现在将英文文档翻译过来,为的是让自己加深印象以及自己以后看的时候能不用这么吃力. 介绍 Ex ...

  2. 必须夸夸Sublime,大文件打开

    今天有个问题的事情日志文件67.8M大文件打开问题开始: 1.vscode必须挨批:直接就给个错误the file cannt be displayed in the editor because i ...

  3. 缓存头Cache-Control的含义和使用

    首先Cache-Control有哪些特性呢?一个是可缓存性 可缓存性 public: 代表这个http请求返回的内容所经过的任何路径中,包括一些中间的http的代理服务器,以及发出这个请求的客户端浏览 ...

  4. 轻量级HTTP服务器Nginx(常用配置实例)

    轻量级HTTP服务器Nginx(常用配置实例)   文章来源于南非蚂蚁   Nginx作为一个HTTP服务器,在功能实现方面和性能方面都表现得非常卓越,完全可以与Apache相媲美,几乎可以实现Apa ...

  5. JS中如何得到触发事件的属性?

    <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    ...

  6. PMVS论文随笔(1)

    博客园排版系统真的比较挫,可以访问我的github.io阅读 关于Unit的概念 在pmvs的源代码中,有一个函数是getUnit ,其函数如下(在PMVS2的windows版本代码,optim.cc ...

  7. Ubuntu 12.04 the system is running in low-graphics mode

    1.出现问题如图所示: 2.解决方案: Ctrl + Alt + F1 df -h 输入密码,到了这一步,也是可以使用terminal,那么没有图形界面也是可以的 cd /etc/X11 sudo c ...

  8. 【洛谷P1064】[NOIP2006] 金明的预算方案

    金明的预算方案 显然是个背包问题 把每个主件和它对应的附件放在一组,枚举每一组,有以下几种选法: 1.都不选 2.只选主件 3.一个主件+一个附件 4.一个主件+两个附件 于是就成了01背包.. #i ...

  9. 开源项目托管github步骤

    一.在github新建项目,复制到本地更改之后命令提交. 1.进入github主页新建项目:https://github.com/ccyinghua 2.复制项目地址 3.打开git Bash 命令行 ...

  10. 改变shape solid color

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...