Files常用函数笔记

Files 
1.copy
	可复制文本文件、图像文件
	使用:
		try(FileOutputStream fileOutputStream = new FileOutputStream(new File(".\\neRWtext.txt"))) {
				System.out.println(Files.exists(Paths.get(".\\neRWtext.txt")));
				//	如果不存在,输出流会自己新建出目标文件
				Files.copy(Paths.get(".\\RWtext.txt"),fileOutputStream);	
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	
	源码:返回值为 long ,读取的文件字节大小
	public static long copy(Path source, OutputStream out) throws IOException {
        // ensure not null before opening file
        Objects.requireNonNull(out);
        try (InputStream in = newInputStream(source)) {
            return copy(in, out);
        }
    }

2.delete
	删除文件,delete 不可以删除不存在的文件..报错.   ---> 可以使用 deleteIfExists文件 
	使用:
		Files.delete(Paths.get(".\\tt.txt"));
		System.out.println(Files.deleteIfExists(Paths.get(".\\tt.txt")));
	
	源码:返回类型为void
		public static void delete(Path path) throws IOException {
        	provider(path).delete(path);
    	}
    	
    	 :返回值为boolean
    	public static boolean deleteIfExists(Path path) throws IOException {
	        return provider(path).deleteIfExists(path);
	    }

3.size
	获取文件的字节数
	使用:
		Path path = Paths.get(".\\RWtext.txt");
		System.out.println(Files.size(path));
		File file = new File(".\\RWtext.txt");
		System.out.println(file.length());	
	
	源码: 返回值为 long
		public static long size(Path path) throws IOException {
	        return readAttributes(path, BasicFileAttributes.class).size();
	    }
	    
	    : 返回值为 length
	    public long length() {
	        SecurityManager security = System.getSecurityManager();
	        if (security != null) {
	            security.checkRead(path);
	        }
	        if (isInvalid()) {
	            return 0L;
	        }
	        return fs.getLength(this);
	    }

4.getLastModifiedTime、getOwner、isWritable、getFileStore、probeContentType
	获取文件最后修改时间&
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值