备份:nio文件读写

本文介绍了一个用于文件读取和保存的实用工具类FileUtil,并通过TransferFile类实现了一个将源文件内容复制到多个目标文件的功能。利用WriteFileThread类实现了文件复制过程中的多线程处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值