使用NIO进行文件操作

  从Java 1.4开始添加了一种新的处理I/O操作的方法,称为NIO(新的I/O)。NIO API 提供了一种处理I/O操作的基于通道(Channel)的方法,可以用来查看并解决某种特定类型的I/O操作。NIO并没有取代java.io中的I/O类,相反,NIO类对标准的I/O系统做了补充,提供了另一种方法。

以下是NIO对文件操作的简单例子:

package  com.simon.test.javanio.file;

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

import  org.apache.commons.logging.Log;
import  org.apache.commons.logging.LogFactory;
import  org.junit.Test;

public   class  FileIOTest  {
    
private static Log log = LogFactory.getLog(FileIOTest.class);
    
    @Test
    
public void testRead()
    
{
        FileInputStream fIn;
        FileChannel fChan;
        
long fSize;
        ByteBuffer bBuf;
        
        
try {
            fIn 
= new FileInputStream("test.txt");
            fChan 
= fIn.getChannel();
            fSize 
= fChan.size();
            bBuf 
= ByteBuffer.allocate((int)fSize);
            
// Read the file into Buffer.
            fChan.read(bBuf);
            bBuf.rewind();
            
for(int i=0; i<fSize; i++)
                System.out.println((
char)bBuf.get());
            
            fChan.close();
            fIn.close();
        }
 catch (FileNotFoundException e) {
            log.error(e.getMessage());
        }
 catch (IOException ioExcep) {
            log.error(ioExcep.getMessage());
        }

    }

    
    @Test
    
public void testMapRead()
    
{
        FileInputStream fIn;
        FileChannel fChan;
        
long fSize;
        MappedByteBuffer bBuf;
        
        
try {
            fIn 
= new FileInputStream("test.txt");
            fChan 
= fIn.getChannel();
            fSize 
= fChan.size();
            bBuf 
= fChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
            
            
for(int i=0; i<fSize; i++)
                System.out.println((
char)bBuf.get());
            
            fChan.close();
            fIn.close();
        }
 catch (FileNotFoundException fileExcep) {
            log.error(fileExcep.getMessage());
        }
 catch (IOException ioExcep) {
            log.error(ioExcep.getMessage());
        }

    }

    
    @Test
    
public void testFileCopy()
    
{
        FileInputStream fIn;
        FileOutputStream fOut;
        FileChannel fIChan, fOChan;
        
long fSize;
        MappedByteBuffer bBuf;
        
        
try {
            fIn 
= new FileInputStream("test.txt");
            fOut 
= new FileOutputStream("testOut.txt");
            fIChan 
= fIn.getChannel();
            fOChan 
= fOut.getChannel();
            fSize 
= fIChan.size();
            
            bBuf 
= fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
            fOChan.write(bBuf);
            fIChan.close();
            fIn.close();
            fOChan.close();
            fOut.close();
            
        }
 catch (FileNotFoundException fileExcep) {
            log.error(fileExcep.getMessage());
        }
 catch (IOException ioExcep) {
            log.error(ioExcep.getMessage());
        }

    }

    
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值