`
v64500
  • 浏览: 22189 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论

nio文件读写

    博客分类:
  • java
阅读更多
FileUtil .java
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;



/** *//**
 * @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 main(String[] args) throws Exception 
    ...{     
        final String suffix = ".txt";
        final String filename = "bufferTemp";
        final String srcFile =  filename + suffix ;
        final String backupFile = filename + "-" + System.currentTimeMillis() + suffix;
        ByteBuffer byteBuffer = FileUtil.readFile(srcFile);
        FileUtil.saveFile(byteBuffer, backupFile);
        byteBuffer = null;
        
    }
}


TransferFile.java
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();        
    }
}


WriteFileThread.java
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 (Exception e) ...{
            System.out.println("The file is not founded: " + fileName);
            e.printStackTrace();
        }
        System.out.println("end Thread: " + this.getName());
    }
}
分享到:
评论

相关推荐

    JAVA_IO/NIO(demo,压缩jar文件)

    io/nio各种文件读写方法。文件压缩成jar包。

    java文件读写处理

    对文件的读取,写入,文件复制等,包括:customBuffer复制文件,nioBuffer复制文件,nioTransfer复制文件

    Java文件读写IO/NIO及性能比较详细代码及总结

    主要介绍了Java文件读写IO/NIO及性能比较详细代码及总结,具有一定借鉴价值,需要的朋友可以参考下。

    javaNIO实例

    该资源包含了一个用javaNIO实现的读写文件以及复制文件的简单的demo,程序注释清晰,简单易懂,喜欢的下载!!!

    java.nio新功能的优点

    java 1.4中推出的java.nio(新输入/输出)软件包允许像在其他低级语言——如C语言——中一样进行输入/输出。许多开发者认为这些新功能只能进行非阻塞操作;但是,这些新功能提供许多其他新鲜有趣的特性,它们包括:...

    java多线程读取文件

    Java多线程读大文件 java多线程写文件:多线程往队列中写入数据

    Java NIO原理和使用

    Java NIO非堵塞应用通常适用用在I/O读写等方面,我们知道,系统运行的性能瓶颈通常在I/O读写,包括对端口和文件的操作上,过去,在打开一个I/O通道后,read()将一直等待在端口一边读取字节内容,如果没有内容进来,...

    JAVA Nio 学习探究

    包含NIO核心概念、基本文件读写、缓冲区内部实现机制、异步IO、缓冲区更多特性探究、文件锁与字符集

    java文件读写工具类分享

    主要为大家详细介绍了java文件读写工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    利用JDK7的NIO2.0进行I/O读写和监视

    NULL 博文链接:https://sharong.iteye.com/blog/1569883

    Java NIO 网络编程初探

    NIO同样拥有文件读写,网络通信等IO操作,今天我们来看看NIO中的TCP网络通信的使用方法。 2. Java NIO 三大核心 Java NIO 有三大核心要素:Channel、Buffer和Selector。Java IO 的操作都是基于输入输出流的,而NIO则...

    详解JavaNIO

    所以,jdk1.4发布了NIO包,NIO的文件读写设计颠覆了传统IO的设计,采用『通道』+『缓存区』使得新式的IO操作直接面向缓存区,并且是非阻塞的,对于效率的提升真不是一点两点,我们一起来看看。我们说过,NIO的核心...

    Java流NIO

    NIO于原来的IO有相同的功能,但是他们之间的使用方式是完全不同的,NIO是面向缓冲区,面向通道的的IO操作,NIO拥有更加高效的进行文件读写。 另外NIO在网络编程可以是一个无阻塞的IO交互,可以大大提升Socket交互的...

    NIO概述

    2.2 NIO概述 NIO NIO ==&gt; New IO(新IO), ... 缓冲使用可以提供操作效率,减少不必要的读写次数 选择器 Selector 真·核心 老大 boss 2.3 Buffer Channel完成文件操作 2.3.1 常用API java.nio.Buffer Buffe

    javasnmp源码-nio-learn:JavaNIO使用示例,NIO的使用,TCP,UDP的简单示例

    NIO将以更加高效的方式进行文件的读写操作。 Java NIO与普通IO的主要区别 io nio 面向流 面向缓冲区(buffer,channel) 堵塞io 非堵塞io - 选择器 java nio主要的核心组件 缓冲区 buffer 通道 Channels 选择器 ...

    nio-demo:NIO编辑演示

    nio-demo NIO编程demo 主要内容: 1、使用NIO实现文件读写,文件拷贝 2、使用NIO实现Socket编程 注:com.minghui.nio.demo包下是IBM官方demo

    Java性能优化之使用NIO提升性能(Buffer和Channel)

    在软件系统中,由于IO的速度要比内存慢,因此,I/O读写在很多场合都会成为系统的瓶颈。提升I/O速度,对提升系统整体性能有着很大的好处。在Java的标准I/O中,提供了基于流的I/O实现,即InputStream和OutputStream。...

    Java NIO与IO的差别和比较

    当中还提供了一个特殊类用于内存映射文件的I/O操作。  2. Charset:它提供Unicode字符串影射到字节序列以及逆影射的操作。  3. Channels:包括socket,file和pipe三种管道,它实际上是双向交流的通道。  

Global site tag (gtag.js) - Google Analytics