Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 备份:nio文件读写
【标  题】:备份:nio文件读写
【关键字】:nio
【来  源】:http://blog.csdn.net/lcllcl987/archive/2007/04/16/1566640.aspx

备份:nio文件读写

Your Ad Here

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

}
在struts架构下,用数据源连接数据库:【上一篇】
Servlet与JSP核心编程读书笔记:【下一篇】
【相关文章】
  • Get rid of 'Continious Partial attention'——警惕"持续局部注意力"效率杀手
  • 四元数(Quaternions)
  • TGE 官方好消息:获奖TGE网络游戏“Minions of Mirth”将于近期对外出售整套代码
  • NIO的使用
  • Java NIO原理和使用
  • Reactor模式和NIO
  • Oracle 8i 中的union all不支持null
  • 学习quaternion的一点点笔记
  • Java NIO 真的能快一些吗?
  • 关于java nio在windows下实现
  • 【随机文章】
  • ;;'
  • 交互设计7日通(五)
  • 非常好的Linux编译内核详解 - -
  • QQ升级天使:不开电脑挂等级
  • 1.1 linux boot----->
  • 吉比以太网交换体系结构分类
  • 深入Atlas系列:客户端网络访问基础结构(下) - WebRequestExecutor和XMLHttpExecutor
  • 建免费4个QQ群(不管你的QQ等级是多少都行)
  • tc3.0下的五子棋(课程设计 源代码)
  • uml 状态机视图事件
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.