`

nio 备份

    博客分类:
  • java
阅读更多
package test.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Date;



/** *//**
 * @author lichun
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class FileUtil{
    
    public static ByteBuffer readFile(String filename) throws Exception{
        FileChannel fiChannel = new FileInputStream(filename).getChannel();
        MappedByteBuffer mBuf;
        mBuf = fiChannel.map(FileChannel.MapMode.READ_ONLY, 0, fiChannel.size());
        fiChannel.close();
        fiChannel = null;
        
        return mBuf;      
        
    }
    
    
    public static void saveFile(ByteBuffer src, String filename) throws Exception{
        FileChannel foChannel = new FileOutputStream(filename).getChannel();
        foChannel.write(src);
        foChannel.close();  
        foChannel = null; 
    }
    
    public static void saveFile(FileChannel fiChannel, String filename) throws IOException{
        MappedByteBuffer mBuf;
        mBuf = fiChannel.map(FileChannel.MapMode.READ_ONLY, 0, fiChannel.size());

        FileChannel foChannel = new FileOutputStream(filename).getChannel();
        foChannel.write(mBuf);
        
        fiChannel.close();
        foChannel.close(); 
        
        fiChannel = null;
        foChannel = null; 
    }
    
    public static void copyFile(String in,String out) throws IOException{
        FileChannel fin=new FileInputStream(in).getChannel();
        FileChannel fout=new FileOutputStream(out).getChannel();
        fout.transferFrom(fin,0,fin.size());
    }
    public static long forChannel(String f1,String f2) throws Exception{
        long time=new Date().getTime();
        int length=3097152;
        FileInputStream in=new FileInputStream(f1);
        FileOutputStream out=new FileOutputStream(f2);
        FileChannel inC=in.getChannel();
        FileChannel outC=out.getChannel();
        ByteBuffer b=null;
        while(true){
            if(inC.position()==inC.size()){
                inC.close();
                outC.close();
                return new Date().getTime()-time;
            }
            if((inC.size()-inC.position())<length){
                length=(int)(inC.size()-inC.position());
            }else
                length=2097152;
            b=ByteBuffer.allocateDirect(length);
            inC.read(b);
            b.flip();
            outC.write(b);
            outC.force(false);
        }
    }
    public static void main(String[] args) throws Exception {    
    	util.MyTools_common.startTimeDesc();
        final String from = "E:\\maying\\test\\HouseStructure.img";
        final String to = "E:\\xx.img";
//        ByteBuffer byteBuffer = FileUtil.readFile(from);
//        FileUtil.saveFile(byteBuffer, to);
//        byteBuffer = null;
        FileUtil.copyFile(from, to);
//        FileUtil.forChannel(from, to);
        util.MyTools_common.endTimeDesc();
        util.MyTools_common.useTimeDesc();
        
    }
}

 

package test.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.*;


/** *//**
 * @author lichun
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TransferFile{
    
    private String srcFile;
    
    List files = new ArrayList();
    ByteBuffer byteBuffer;
   
    public TransferFile(){
        createFileNames();
    }
    
    private void createFileNames(){
        for (int i = 0; i < 10; i++){
            files.add(i + "");
        }
    }  
    
    public List getFiles(){
        return this.files;
    }

    public String getSrcFile() {
        return srcFile;
    }

    public void setSrcFile(String srcFile){
        this.srcFile = srcFile;
    }
    
    public void saveFiles() throws FileNotFoundException{
        String tempFile;
        for (int i = 0; i < this.files.size(); i++) {
            tempFile = "test-files/" + (String)files.get(i) + ".txt";
            System.out.println("begin create thread for " + tempFile);
            
            FileChannel fiChannel = new FileInputStream(this.srcFile).getChannel();
            
            WriteFileThread writeFileThread = new WriteFileThread("writeFile", fiChannel, tempFile);
            writeFileThread.start();   
        }
        
    }

    public static void main(String[] args) throws Exception {
        final String srcFile = "test-files/transferFile.txt";
        TransferFile transferFile = new TransferFile();
        transferFile.setSrcFile(srcFile);
        transferFile.saveFiles();        
    }
}

 

 

package test.nio;

import java.io.IOException;
import java.nio.channels.FileChannel;

/** *//**
 * @author lichun
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class WriteFileThread extends Thread{

    private FileChannel fiChannel;
    private String fileName;
   
    public WriteFileThread(String name){
        super(name);
    }
    
    public WriteFileThread(String name, FileChannel fiChannel, String fileName){
        this(name);
        this.fiChannel = fiChannel;
        this.fileName = fileName;
    }
    
    public void setFiChannel(FileChannel fiChannel){
        this.fiChannel = fiChannel;
    }
    
    public FileChannel getFiChannel(){
    	return fiChannel;
    	
    }
    public void setFileName(String fileName){
    	this.fileName = fileName;
    }
    public String getFileName(){
    	return fileName;
    }
    public void run(){
    	System.out.println("in Thread: "+this.getName());
    	try {
			FileUtil.saveFile(this.fiChannel, this.fileName);
		} catch (IOException e) {
			System.out.println("The file is not founded: "+fileName);
			e.printStackTrace();
		}
		System.out.println("end Thread : "+ this.getName());
    }
}

 

 

分享到:
评论

相关推荐

    talent-nio3.0.0备份

    NULL 博文链接:https://tywo45.iteye.com/blog/1950250

    JavaNIO chm帮助文档

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

    NIO 入门.chm,NIO 入门.chm

    NIO入门.chm NIO入门.chm NIO入门.chm

    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...

    java nio 实现socket

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

    java nio 包读取超大数据文件

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

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

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

    java NIO.zip

    java NIO.zip

    Java NIO英文高清原版

    Java NIO英文高清原版

    Java NIO系列教程(一) Java NIO 概述

    Java NIO系列教程(一) Java NIO 概述

    尚硅谷Java视频_NIO 视频教程

    尚硅谷_NIO_NIO 与 IO 区别 ·02. 尚硅谷_NIO_缓冲区(Buffer)的数据存取 ·03. 尚硅谷_NIO_直接缓冲区与非直接缓冲区 ·04. 尚硅谷_NIO_通道(Channel)的原理与获取 ·05. 尚硅谷_NIO_通道的数据传输与内存映射文件 ...

    Java IO NIO and NIO 2 无水印pdf

    Java IO NIO and NIO 2 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn...

    NIO和AIO介绍

    NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍NIO和AIO介绍

    java基于NIO实现Reactor模型源码.zip

    java基于NIO实现Reactor模型源码java基于NIO实现Reactor模型源码java基于NIO实现Reactor模型源码java基于NIO实现Reactor模型源码java基于NIO实现Reactor模型源码java基于NIO实现Reactor模型源码java基于NIO实现...

    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 IO, NIO and NIO.2 原版pdf by Friesen

    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 ...

    Java IO NIO and NIO 2 epub

    Java IO NIO and NIO 2 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    NIO与零拷贝_javanio_nio和零拷贝_

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

Global site tag (gtag.js) - Google Analytics