pro java nio 2 pdf,Pro Java7 Nio.2 学习(第一章)

package chapter1;

import java.io.File;

import java.io.IOException;

import java.nio.file.LinkOption;

import java.nio.file.Path;

import java.nio.file.Paths;

/**

* 定义Path 对象

*

* @author longlong

*

* @mail borrip0419@gmail.com

*

*/

public class PathTest {

public static void main(String[] args) {

/**

* 1.定义一个绝对路径

*/

Path path = Paths.get("c:/my/test1.log");

System.out.println(path);

//get方法可以将参数

path = Paths.get("c:", "my" , "test1.log") ;

System.out.println(path);

/**

* 2.定义一个相对路径

*/

path = Paths.get("/my" , "test1.log") ;

System.out.println(path);

/**

* 3.相对于工作目录路径

*/

path = Paths.get("my" , "test1.log") ;

System.out.println(path);

/**

* 4.路径的简写 : .表示当前目录   ..表示上级目录

*/

path = Paths.get("c:/my/./test1.log").normalize() ;

System.out.println(path);

path = Paths.get("c:/my/./../test1.log").normalize() ;

System.out.println(path);

/**

* 5. 定义 Path 同样可以使用 FileSystems.getDefault().getPath(...)

*/

/**

* 6.取得Home路径

*/

path = Paths.get(System.getProperty("user.home"), "downloads", "game.exe");

System.out.println(path);

/**

* 7.得到Path对象之后,获取其更多信息内容

*/

path = Paths.get("c:/my/test1.log");

//a.获取其文件名称

System.out.println(path.getFileName());

//b.获取文件的根目录

System.out.println(path.getRoot());

//c.获取文件的上级目录路径

System.out.println(path.getParent());

//d.获取文件其路径上所有名称

for (int i = 0; i 

System.out.println("Name element " + i + " is: " + path.getName(i)) ;

}

//e.获得子路径

System.out.println("Subpath (0,1): " + path.subpath(0, 1));

/**

* 8.获得文件路径的URI 用于浏览器可以访问

*/

System.out.println(path.toUri());

/**

* 9.将相对路径转换为绝对路径

*/

path = Paths.get("c:" , "my" ,"test" , "1.log") ;

System.out.println(path.toAbsolutePath());

try {

path.toRealPath(LinkOption.NOFOLLOW_LINKS);

System.out.println(path);

} catch (IOException e) {

e.printStackTrace();

}

/**

* 10.将path转换为File

*/

File file = path.toFile() ;

System.out.println(file);

/**

* 11.比较两个路径

*/

Path path01 = Paths.get("BNP.txt");

Path path02 = Paths.get("AEGON.txt");

Path path02_to_path01 = path02.relativize(path01);

System.out.println(path02_to_path01);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Input/output (I/O) is not a sexy subject, but it’s an important part of non-trivial applications. This book introduces you to most of Java’s I/O capabilities as of Java 8 update 51. Chapter 1 presents a broad overview of I/O in terms of Java’s classic I/O, New I/O (NIO), and NIO.2 categories. You learn what each category offers in terms of its capabilities, and you also learn about concepts such as paths and Direct Memory Access. Chapters 2 through 5 cover classic I/O APIs. You learn about the File and RandomAccessFile classes along with streams (including object serialization and externalization) and writers/readers. Chapters 6 through 11 focus on NIO. You explore buffers, channels, selectors, regular expressions, charsets, and formatters. (Formatters were not introduced with the other NIO types in Java 1.4 because they depend on the variable arguments capability that was introduced in Java 5.) NIO is missing several features, which were subsequently provided by NIO.2. Chapters 12 through 14 cover NIO.2’s improved file system interface, asynchronous I/O, and the completion of socket channel functionality. Each chapter ends with assorted exercises that are designed to help you master its content. Along with long answers and true/false questions, you are often confronted with programming exercises. Appendix A provides the answers and solutions. Appendix B provides a tutorial on sockets and network interfaces. Although not directly related to classic I/O, NIO, and NIO.2, they leverage I/O capabilities and are mentioned elsewhere in this book.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值