`
TimerBin
  • 浏览: 355638 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java nio Files DirectoryStream 文件复制

    博客分类:
  • JAVA
阅读更多

JDK1.7 中新增了nio包,基于此包可实现java程序基于操作系统的情况下对文件或文件夹进行CRUD操作,同时支持跨系统间的文件操作。

一、对文件操作代码

1、在java中基于IO对系统文件进行操作

public static void oldCopyFileList(){
	long begin = System.currentTimeMillis();
	FileInputStream input = null;
	FileOutputStream output = null;
	try {
		File file = new File("D:/1");
		String newPath = "D:/2";  
		if(!file.isFile()){
			for(int i=0;i<file.listFiles().length;i++){
				File fileSun = file.listFiles()[i];
				input = new FileInputStream(file.listFiles()[i]);
				output = new FileOutputStream(newPath + "/"+(fileSun.getName()).toString());
				byte[] b = new byte[1024 * 5];
				int len;
				while ((len = input.read(b)) != -1) {
					output.write(b, 0, len);
				}
				output.flush();
			}
		}
	} catch (IOException e) {
		e.printStackTrace();
	}finally{
		if(null !=input ){
			try {
				input.close();
			} catch (IOException e) {
			}
		}
		if(null !=output ){
			try {
				output.close();
			} catch (IOException e) {
			}
		}
	}
	System.err.println("time:"+(System.currentTimeMillis()-begin));
}

 2、java中基于nio对文件进行操作

 

public static void newFileList(){
	long begin = System.currentTimeMillis();
	try {
		Path path = Paths.get("D:/1");  
		String newPath = "D:/2/";  
		DirectoryStream<Path> streamList = Files.newDirectoryStream(path);
		for (Path pathSun : streamList){
		     Files.copy(pathSun, Paths.get(newPath+pathSun.getFileName()) , StandardCopyOption.COPY_ATTRIBUTES);
		} 
	} catch (IOException e) {
			e.printStackTrace();
	}
	System.err.println("time:"+(System.currentTimeMillis()-begin));
}

3、在Files工具类中同样提供了基于InputStream的文件操作方式,代码如下所示:

public static void oldCopyFileList(){
	long begin = System.currentTimeMillis();
	FileInputStream input = null;
	try {
		File file = new File("D:/1");
		String newPath = "D:/2/";  
		if(!file.isFile()){
			for(int i=0;i<file.listFiles().length;i++){
				File fileSun = file.listFiles()[i];
				System.err.println(fileSun.getName());
				input = new FileInputStream(file.listFiles()[i]);
				    Files.copy(input, Paths.get(newPath+fileSun.getName()),  StandardCopyOption.REPLACE_EXISTING);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
		   if(null !=input ){
			try {
				input.close();
			} catch (IOException e) {
		   }
		}
	}
	System.err.println("time:"+(System.currentTimeMillis()-begin));
}

 这种方式可用于上传的图片添加水印,但是只有想法,未投入到生产使用,不知道是否有什么后遗症。

比较

在处理单个大文件的拷贝时使用IO的效率要高于nio。nio在进行多个小文件拷贝时效率远远高于IO。

二、源码分析

window系统源码(与上方代码相对应这里只针对copy源码)

1、Files.copy

public static Path copy(Path paramPath1, Path paramPath2, CopyOption[] 
paramArrayOfCopyOption)throws IOException{
    FileSystemProvider localFileSystemProvider = provider(paramPath1);
      if (provider(paramPath2) == localFileSystemProvider){
      localFileSystemProvider.copy(paramPath1, paramPath2, 
          paramArrayOfCopyOption);
    }else {
      //这里有支持其他系统的意思,我还未细研究写出应用实例
      CopyMoveHelper.copyToForeignTarget(paramPath1, 
      paramPath2, paramArrayOfCopyOption);
    }
    return paramPath2;
}

 通过以下代码来获得是当前是什么系统的,具体为什么还没有想通?

public static FileSystem getDefault(){
    return DefaultFileSystemHolder.defaultFileSystem;
}
private static class DefaultFileSystemHolder {
    static final FileSystem defaultFileSystem = defaultFileSystem();
    private static FileSystem defaultFileSystem(){
       FileSystemProvider localFileSystemProvider = (FileSystemProvider)AccessController.doPrivileged(new PrivilegedAction(){
         public FileSystemProvider run() {
            return FileSystems.DefaultFileSystemHolder.access$000();
         }
       });
       return localFileSystemProvider.getFileSystem(URI.create("file:///"));
    }
}

 2、WindowsFileSystemProvider.copy  在linux下的实现类为UnixFileSystemProvider

public void copy(Path paramPath1, Path paramPath2, CopyOption[] paramArrayOfCopyOption)
throws IOException{
     WindowsFileCopy.copy(WindowsPath.toWindowsPath(paramPath1), WindowsPath.toWindowsPath(paramPath2), paramArrayOfCopyOption);
}

 这里将公用的Path转为WindowsPath,linux下转为了UnixPath,还有相应的ZipPath,对zip文件操作。

 

3、WindowsFileCopy.copy() 这个方法操作非常复杂,这里只是最简单列了一种情况,而且其中好多异常捕获和线程的开关都删除掉了,有兴趣的请查看源码

WindowsFileAttributes localWindowsFileAttributes1 = null;
WindowsFileAttributes localWindowsFileAttributes2 = null;
//获得文件属性 比如只读 等等
long l1 = 0L;
l1 = paramWindowsPath1.openForReadAttributeAccess(bool);
localWindowsFileAttributes1 = WindowsFileAttributes.readAttributes(l1);
//获得文件属性 比如只读 等等
long l2 = 0L;
l2 = paramWindowsPath2.openForReadAttributeAccess(false);
localWindowsFileAttributes2 = WindowsFileAttributes.readAttributes(l2);
//获得文件路径
String str1 = asWin32Path(paramWindowsPath1);
String str2 = asWin32Path(paramWindowsPath2);
int i2 = ((paramWindowsPath1.getFileSystem().supportsLinks()) && (!bool)) ? 2048 : 0;
try {
   WindowsNativeDispatcher.CopyFileEx(str1, str2, i2, 0L);
} catch (WindowsException localWindowsException6) {
   localWindowsException6.rethrowAsIOException(paramWindowsPath1, paramWindowsPath2);
}

 4、WindowsNativeDispatcher 与操作系统对接的文件操作类

static void CopyFileEx(String paramString1, String paramString2, int paramInt, long paramLong)
    throws WindowsException{
    NativeBuffer localNativeBuffer1 = asNativeBuffer(paramString1);
    NativeBuffer localNativeBuffer2 = asNativeBuffer(paramString2);
    try {
      CopyFileEx0(localNativeBuffer1.address(), localNativeBuffer2.address(), paramInt, paramLong);
    }
    finally {
      localNativeBuffer2.release();
      localNativeBuffer1.release();
    }
  } 
private static native long CreateFile0(long paramLong1, int paramInt1, int paramInt2, long paramLong2, int paramInt3, int paramInt4)
    throws WindowsException;

 此类中还提供了CreateFile、DeleteFile、CreateDirectory等方法。

 

 

由于Jdk中添加了nio包,同时新增加了用以遍历文件目录的迭代器DirectoryStream继承于Iterable。

官方解释如下:

An object to iterate over the entries in a directory. A directory stream
allows for the convenient use of the for-each construct to iterate over a
directory.

其特点:其中定义了一个interface Filter<T> 静态内部接口。

 

分享到:
评论

相关推荐

    java nio 包读取超大数据文件

    Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据文件Java nio 超大数据文件 超大数据...

    java nio 读文件

    java nio 读文件,java nio 读文件

    JAVA NIO 简单PFT 文件服务

    JAVA NIO 简单PFT 文件服务 上传 下载 列表

    java NIO和java并发编程的书籍

    java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java NIO和java并发编程的书籍java...

    JavaNIO chm帮助文档

    Java NIO系列教程(一) Java NIO 概述 Java NIO系列教程(二) Channel Java NIO系列教程(三) Buffer Java NIO系列教程(四) Scatter/Gather Java NIO系列教程(五) 通道之间的数据传输 Java NIO系列教程(六)...

    JAVA NIO 按行读取大文件,支持 GB级别

    本类,是专门为了处理大文件,按行读取开发的类。 采用读文件的缓存 fbb 1024*5 行缓存 bb 256 字节 设计思想: 每次通过nio读取字节到 fbb中 然后对fbb自己中的内容进行行判断即 10 回车 13 行号 0 文件...

    JAVA NIO 按行读取大文件支持 GB级别-修正版

    本类,是专门为了处理大文件,按行读取开发的类。 采用读文件的缓存 fbb 1024*5 行缓存 bb 256 字节 设计思想: 每次通过nio读取字节到 fbb中 然后对fbb自己中的内容进行行判断即 10 回车 13 行号 0 文件结束 ...

    java nio 写文件

    java nio 写文件,java nio 写文件

    Java用NIO读取文件示范

    简单的用Java的NIO读取文件的程序,给大家参考。

    Java NIO英文高清原版

    Java NIO英文高清原版

    NIO与零拷贝_javanio_nio和零拷贝_

    java nio和零拷贝 方面知识,适合对nio和零拷贝感兴趣的开发人员

    java NIO 中文版

    讲解了 JavaIO 与 JAVA NIO区别,JAVA NIO设计理念,以及JDK中java NIO中语法的使用

    java NIO.zip

    java NIO.zip

    Java NIO 中文 Java NIO 中文 Java NIO 中文文档

    Java NIO 深入探讨了 1.4 版的 I/O 新特性,并告诉您如何使用这些特性来极大地提升您所写的 Java 代码的执行效率。这本小册子就程序员所面临的有代表性的 I/O 问题作了详尽阐述,并讲解了 如何才能充分利用新的 I/O ...

    java NIO 视频教程

    Java NIO(New IO)是一个可以替代标准Java IO API的IO API(从Java 1.4开始),Java NIO提供了与标准IO不同的IO工作方式。 Java NIO: Channels and Buffers(通道和缓冲区) 标准的IO基于字节流和字符流进行操作的,...

    java nio 实现socket

    java nio 实现socketjava nio 实现socketjava nio 实现socketjava nio 实现socketjava nio 实现socket

    java nio中文版

    java NIO是 java New IO 的简称,在 jdk1.4 里提供的新 api 。 Sun 官方标榜的特性如下: – 为所有的原始类型提供 (Buffer) 缓存支持。 – 字符集编码解码解决方案。 – Channel :一个新的原始 I/O 抽象。 – 支持...

    java NIO 写文件

    java nio 写文件样例,java写大数据文件时提高性能

    java NIO文件操作(中文版pdf)

    java NIO文件操作(中文版pdf),希望对大家有帮助,(转载)

    Java Nio selector例程

    java侧起server(NioUdpServer1.java),基于Java Nio的selector 阻塞等候,一个android app(NioUdpClient1文件夹)和一个java程序(UI.java)作为两个client分别向该server发数据,server收到后分别打印收到的消息...

Global site tag (gtag.js) - Google Analytics