java 1.7 新io 实践 NIO2
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的更多相关文章
- java 21 - 15 新IO流 NIO
1:JDK4 新IO要了解的类 Buffer(缓冲),Channer(通道) 2:JDK7 要了解的新IO类 Path:与平台无关的路径. Paths:包含了返回Path的静态方法. public ...
- Java 新IO
NIO提供全新的底层I/O模型.与最初的java.io包中面向流(stream-oriented)概念不同,NIO采用了面向块的概念(block-oriented).在尽可能的情况下,I/O的操 ...
- JAVA(六)数据库/网络编程/新IO
成鹏致远 | lcw.cnblog.com |2014-02-05 数据库 1.JDBC概述 JDBC(Java Database Connectivity,Java数据库连接)提供了一种与平台无关的 ...
- Java -- 新IO -- 目录
20.1 Java 新IO简介 20.2 缓冲区与Buffer 例:演示缓冲区的操作流程 Class : IntBufferDemo01 20.2.2 深入缓冲区操作 20.2.3 创建子缓冲区 20 ...
- Java 8特性尝鲜:新新IO
Java 8特性尝鲜:新新IO 在这个专题前面的文章中,我们已经看到,使用Java8的lambda表达式对现有的JDK1.2 I/O库的提升,主要是可以使用lambda表达式来构造java.io.Fi ...
- java 提供了哪些IO方式
今天听了杨晓峰老师的java 36讲,感觉IO这块是特别欠缺的,所以讲义摘录如下: 欢迎大家去订阅: 本文章转自:https://time.geekbang.org/column/article/83 ...
- Java SE 6 新特性: Java DB 和 JDBC 4.0
http://www.ibm.com/developerworks/cn/java/j-lo-jse65/index.html 长久以来,由于大量(甚至几乎所有)的 Java 应用都依赖于数据库,如何 ...
- [转] Java 8的新特性
简介 毫无疑问,Java 8是Java自Java 5(发布于2004年)之后的最重要的版本.这个版本包含语言.编译器.库.工具和JVM等方面的十多个新特性.在本文中我们将学习这些新特性,并用实际的例子 ...
- Java 8的新特性—终极版
作者:杜琪[译] 原文链接:http://www.jianshu.com/p/5b800057f2d8 1. 简介 毫无疑问,Java 8是Java自Java 5(发布于2004年)之后的最重要的版本 ...
随机推荐
- 监控系统-nagios
https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/ install yum -y install nagios-4 ...
- Oracle 安装教程图解
一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要出现中文,也不要出现空格等不规则字符. 官方下地址: http://www.ora ...
- bootstrap-table之通用方法( 时间控件,导出,动态下拉框, 表单验证 ,选中与获取信息)
1.bootstrap-table 单击单行选中 $('#gzrwTable').on('click-row.bs.table', function(e, row, $element) { $('.s ...
- 原生Js在各大浏览器上、火狐、ie、谷歌、360等出现的不兼容问题。
1 document.getElementsByName("name") 在Ie低版本,360普通版本,以及火狐低版本不支持. 2 element.innerText 在低版本的 ...
- 解压war包
unzip cat-alpha-3.0.0.war -d /tmp/test 说明:-d指定解压的路径和文件,文件名不存在会自动创建
- Altium_Designer-PCB中各层作用详解
一直以来,对PCB中各层,比如:solder层.paste层.Top overlay层等等这些一知半解.今天仔细看了下,向大家介绍一下,有不对的地方还请指正. 1.mechanical机械层是定义整个 ...
- MySQL数据库实验五:数据更新
实验五 数据更新 一.实验目的 掌握数据更新操作的用法. 二.实验环境 三.实验示例 1.往基本表SC中插入元组. ① INSERT INTO S(S#,SNAME,AGE,SEX) VA ...
- EF写distinct
在日常开发中常常是这么写的 var logErrorRequest = from l in _logErrorRepository.Table select new { WrongTime = l.W ...
- Codeforces 760A Petr and a calendar
题目链接:http://codeforces.com/problemset/problem/760/A 题意:日历需要多少列. #include <bits/stdc++.h> using ...
- sql server 基础
1 .左连接 select a.* ,b.* from student as aleft join hobby as bon a.hobbyid=b.hobbyid 2. 右 连接 select a. ...