`

文件读写交替执行

    博客分类:
  • java
阅读更多

 

文件读写的几种方式

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import org.apache.commons.io.FileUtils;

public class FileTest {

 public static void main(String[] args) throws IOException,Exception{

  long st=System.currentTimeMillis();
  String srcFile="D:\\DBW\\pig-0.14.0.tar.gz";
  String destFile="D:\\DBW\\log\\test.gz";
  
  
  
  copyFileThread(srcFile,destFile);
  
  long et=System.currentTimeMillis();
  System.out.println(et-st);
 }

 static void copyFileCommonIO(String srcFile,String destFile)throws IOException{
  FileUtils.copyFile(new File(srcFile), new File(destFile));//2546
 }
 
 static void copyFileStream(String srcFile,String destFile)throws IOException{//255
  FileInputStream fis=new FileInputStream(new File(srcFile));
  FileOutputStream fos=new FileOutputStream(new File(destFile));
  byte[] buf=new byte[8192];
  BufferedInputStream bs=new BufferedInputStream(fis);
  BufferedOutputStream bos=new BufferedOutputStream(fos);
  
  int len=0;
  
  do{
   len=bs.read(buf, 0, 8192);
   bos.write(buf, 0, len);
  }while(len==8192);
 
  bs.close();
  fis.close();
  bos.close();
  fos.close();
 }
 
 static void copyFileChannel(String srcFile,String destFile)throws IOException{//133
  FileChannel srcChl=new FileInputStream(new File(srcFile)).getChannel();
  FileChannel desChl=new FileOutputStream(new File(destFile)).getChannel();
  desChl.transferFrom(srcChl, 0, Long.MAX_VALUE);
  srcChl.close();
  desChl.close();
 }
 
 static void copyFileThread(String srcFile,String destFile)throws Exception{//480
  FileVO file=new FileVO();
  file.setSrcFile(srcFile);
  file.setDestFile(destFile);
  
  
  WriteThread w=new WriteThread(file);
  ReadThread r=new ReadThread(file);
  Thread t1=new Thread(w);
  t1.start();
  Thread t2=new Thread(r);
  t2.start();
  
  t1.join();
  t2.join();
 }
}

 

 

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ReadThread implements Runnable {
 private FileVO file;
 public ReadThread(FileVO file){
  this.file=file;
 }

 public void run() {
  FileInputStream fis=null;
  BufferedInputStream bs=null;
  try {
   fis = new FileInputStream(new File(file.getSrcFile()));
   byte[] buf=new byte[8192];
   bs=new BufferedInputStream(fis);
   int len=0;
   do{
    synchronized(file){
     len=bs.read(buf, 0, 8192);
     //System.out.println("read.."+len);
     file.setLen(len);
     file.setBuf(buf);
     file.notify();
     if(len<8192)
      break;
     file.wait();
    }
   }while(true);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e){
   e.printStackTrace();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  finally{
   try {
    bs.close();
    fis.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
 }

}

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteThread implements Runnable {
 private FileVO file;
 public WriteThread(FileVO file){
  this.file=file;
 }
 
 public void run() {
  FileOutputStream fos=null;
  BufferedOutputStream bos=null;
  try {
   fos = new FileOutputStream(new File(file.getDestFile()));
   bos=new BufferedOutputStream(fos);
   do{
   synchronized(file){
    if(file.getLen()==0){
     file.wait();
    }
    //System.out.println("write.."+file.getLen());
    bos.write(file.getBuf(), 0, file.getLen());
    if(file.getLen()<8192)
     break;
    file.setLen(0);
    file.notify();
   }
   }while(true);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e){
   e.printStackTrace();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  finally{
   try{
   bos.close();
   fos.close();
   }catch(IOException e){
    e.printStackTrace();
   }
  }
 }

}

public class FileVO {
 private String srcFile;
 private String destFile;
 //private volatile int len;
 private int len;
 private byte[] buf;
 
 public String getSrcFile() {
  return srcFile;
 }
 public void setSrcFile(String srcFile) {
  this.srcFile = srcFile;
 }
 public String getDestFile() {
  return destFile;
 }
 public void setDestFile(String destFile) {
  this.destFile = destFile;
 }
 public int getLen() {
  return len;
 }
 public void setLen(int len) {
  this.len = len;
 }
 public byte[] getBuf() {
  return buf;
 }
 
 
 public void setBuf(byte[] buf) {
  this.buf = buf;
 }
 
 
}

分享到:
评论

相关推荐

    1345个易语言模块

    ZCL_文件读写 1.01.ec ZCL_核库函数1.01.ec zip.ec Z计算器.ec [神2也教你学E] - 可执行动态载入&输出其他文件模块.ec _仿真shell库.ec √功能键状态√.ec √取功能键状态√.ec 万 年历.ec 万能注册验证模块.ec 世宝...

    1350多个精品易语言模块

    ZCL_文件读写 1.01.ec ZCL_核库函数1.01.ec zip.ec Z计算器.ec [神2也教你学E] - 可执行动态载入&输出其他文件模块.ec _仿真shell库.ec √功能键状态√.ec √取功能键状态√.ec 万 年历.ec 万能注册验证模块.ec 世宝...

    易语言模块914个

    ZCL_文件读写1.01.ec ZCL_核库函数1.01.ec zip.ec zip压缩.ec 万能注册验证模块.ec 世恒通用安装系统文件压缩模块.ec 个性信息框1.1.ec 个性信息框1.21.ec 个性信息框1.5.ec 个性信息框1.ec 个性化电脑...

    易语言模块大全(374个)

    执行批文件.ec 窗口外形任我设.ec 求真文件拖放模块.ec 快捷方式.ec freemark模块_取文件.ec 取mdb表名.ec 频谱.ec 飛狐DLL.ec EdbServer1.0客户端.ec 读.xls文件到高级表格.ec 超级列表框交替色.ec 模拟按键.ec ...

    E语言1000模块

    2005-10-21 15:30 14164 3949 易语言模块大全\IC卡读写模块 1.0.ec 2002-06-24 10:54 5555 2002 易语言模块大全\IP地址编辑框2.0.ec 2002-03-26 10:00 1765 740 易语言模块大全\kernel模块.EC 2005-10-21 15:30 ...

    Python Cookbook

    16.11 自动化py2exe将脚本编译成Windows可执行文件的过程 585 16.12 在UNIX中将主脚本和模块绑成一个可执行文件 587 第17章 扩展和嵌入 590 引言 590 17.1 实现一个简单的扩展类型 592 17.2 用Pyrex实现一个...

    《C语言高级编程与实例剖析》源码

    3.2.5 文件的随机读写函数 78 3.2.6 实例解析 82 3.3 文件的加密与解密 83 3.3.1 移位加密法 83 3.3.2 伪随机数加密法 84 3.3.3 命令加密法 86 3.3.4 逐字节异或加密法 88 3.4 文件分割程序 ...

    Linux系统编程之线程同步

    文件同步,是指让两个或多个文件夹里的文件保持一致。等等 而,编程中、通信中所说的同步与生活中大家印象中的同步概念略有差异。“同”字应是指协同、协助、互相配合。主旨在协同步调,按预定的先后次序运行。 ...

    2020易语言模块大全持续更新1.zip

    ZCL_文件读写1.01.ec ZCL_核库函数1.01.ec zip.ec zip压缩.ec zyJson.ec [YY][IS]查找模块.ec _易语言皮肤模块.EC 代码编辑器部分模块.ec 传世注册.ec 保存图片(1.0).EC 保存图片1.0.ec 保存扩展界面设置.ec 保存...

    易语言模块大全(共775个模块)

    edb数据库文件转Excel文件1.5(1.5).zip edb数据库文件转Excel文件1.3(1.0).zip ETimeFly API模块(1.0).zip Excel功能模块(1.0).zip EXE→SWF转换模块(1.0).zip E库多条件查询模块(1.0).zip E库模糊查询模块(1.0).zip...

    易语言700模块打包

    edb数据库文件转Excel文件1.5(1.5).zip edb数据库文件转Excel文件1.3(1.0).zip ETimeFly API模块(1.0).zip Excel功能模块(1.0).zip EXE→SWF转换模块(1.0).zip E库多条件查询模块(1.0).zip E库模糊查询模块...

    github-to-cos

    github-to-cos 使用方法: 一,腾讯云配置桶 1,腾讯云创建COS存储桶,访问权限选择:公有读私有写。例如区域广州(REGION):ap-guangzhou,桶名(BUCKET):jd-...2,手动运行,随便编辑一下文件提交可以触发运行

    你必须知道的495个C语言问题

    这导致空间浪费而且无法与外部数据文件进行“二进制”读写。能否关掉填充,或者控制结构域的对齐方式? 2.14 为什么sizeof返回的值大于结构大小的期望值,是不是尾部有填充? 2.15 如何确定域在结构中的字节偏移...

    《你必须知道的495个C语言问题》

    这导致空间浪费而且无法与外部数据文件进行“二进制”读写。能否关掉填充,或者控制结构域的对齐方式? 27  2.14 为什么sizeof返回的值大于结构大小的期望值,是不是尾部有填充? 28 2.15 如何确定域在结构中的...

Global site tag (gtag.js) - Google Analytics