Java NIO的工具类

/*
 * io:
 * 	阻塞式IO
 * nio:  JDK1.4-->JDK1.7
 * 	 非阻塞式的IO
 * 
 * Path(接口):类似于File,用路径名表示一个目录或文件
 * Paths:工具类,用来创建Path接口的对象
 * 
 * Files工具类,操作文件或目录的工具类:
 * (1)Files.copy(src, dest, StandardCopyOption.REPLACE_EXISTING);
 * (2)delete(Path path)       功能类似于File类的delete()	
 * 							不同的地方是会报异常,当文件不存在时
 *    deleteIfExists(Path path) 功能相同于File类的delete()	存在就删除,不存在就什么也不干
 *
 * (3)move(Path source, Path target, CopyOption... options) 
 * (4)List<String> readAllLines(Path path, Charset cs)  
 * ...
 */
public class TestNIO {
	@Test
	public void test06() throws IOException{
		Path file = Paths.get("testIO","java","2.txt");
		List<String> allLines = Files.readAllLines(file, Charset.forName("UTF-8"));
		for (String string : allLines) {
			System.out.println(string);
		}
	}
	
	@Test
	public void test05() throws IOException{
		Path src = Paths.get("2.txt");
		Path dest = Paths.get("testIO","java","2.txt");
		Files.move(src, dest, StandardCopyOption.REPLACE_EXISTING);
	}
	
	
	@Test
	public void test04() throws IOException{
		File file = new File("1.txt");
		file.delete();
	}
	
	@Test
	public void test03() throws IOException{
		Path src = Paths.get("1.txt");
		Files.delete(src);
	}
	
	@Test
	public void test02() throws IOException{
		Path src = Paths.get("1.txt");
		Path dest = Paths.get("2.txt");
		Files.copy(src, dest, StandardCopyOption.REPLACE_EXISTING);
	}
	
	@Test
	public void test01(){
		Path file = Paths.get("testIO", "java" ,"1.txt");
		
		int count = file.getNameCount();
		System.out.println(count);
		
		Path name = file.getName(0);
		System.out.println(name);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值