黑马程序员__IO之文件复制、分割与合并
来源:互联网 发布:安卓开发 布局优化 编辑:程序博客网 时间:2024/05/22 08:12
黑马程序员—IO之文件的复制、分割与合并
-------android培训、java培训、期待与您交流! ----------
1. 文件的复制
(1).//使用字节流实现对文件的复制 public static void Copy1(String src,String dec){ long start =System. currentTimeMillis(); //1.提供要读入与写出的文件 File file1=new File (src); File file2=new File (dec); //2.提供相应的流对象 InputStream in=null; OutputStream out=null; try { in =new FileInputStream( file1); out =new FileOutputStream( file2); } catch (FileNotFoundException e) { e .printStackTrace() ; } //3.实现文件的复制(先读后写) byte[] bytes=new byte [1024* 1024]; int len =-1; try { while((len =in. read(bytes ))!=-1 ){ out .write(bytes, 0, len ); } } catch (IOException e) { // TODO Auto-generated catch block e .printStackTrace() ; }finally{ if (out!= null) { try { out .close() ; } catch (IOException e) { // TODO Auto-generated catch block e .printStackTrace() ; } } if (in!= null) { try { in .close() ; } catch (IOException e) { // TODO Auto-generated catch block e .printStackTrace() ; } } } long end =System. currentTimeMillis(); System .out. println("Copy1消耗的时间为:" +(end -start)) ; }
(2)、//使用字符流实现对文件的复制 private static void Copy2(String src , String dec) { //1.提供要读入与写出的文件对象 File file1=new File (src); File file2=new File (dec); //2.提供相应的流对象 Reader in=null; Writer out=null; try { in =new FileReader( src); out =new FileWriter( dec); //3.复制操作 char[] ch=new char [10]; int len =-1; while((len =in. read(ch ))!=-1 ){ out .write(ch, 0, len ); } } catch (IOException e) { // TODO Auto-generated catch block e .printStackTrace() ; }finally{ //4.关闭流 if (out!= null) { try { out .close() ; } catch (IOException e) { // TODO Auto-generated catch block e .printStackTrace() ; } } if (in!= null) { try { in .close() ; } catch (IOException e) { // TODO Auto-generated catch block e .printStackTrace() ; } } } }
(3)、//使用缓冲字节流实现文件的复制 private static void Copy3(String src, String dec) { long start =System. currentTimeMillis(); //1.提供文件对象 File file1=new File (src); File file2=new File (dec); //2.提供流对象 BufferedInputStream bs=null; BufferedOutputStream bw=null; try { bs = new BufferedInputStream( new FileInputStream(file1 )); bw =new BufferedOutputStream( new FileOutputStream(file2 )); //3.开始复制 byte[] bytes=new byte [1024* 1024]; int len =-1; while((len =bs. read(bytes ))!=-1 ){ bw .write(bytes, 0, len ); bw .flush() ; } } catch (IOException e) { e .printStackTrace() ; }finally{ if (bw!= null) { try { bw .close() ; } catch (IOException e) { // TODO Auto-generated catch block e .printStackTrace() ; } } if (bs!= null) { try { bs .close() ; } catch (IOException e) { // TODO Auto-generated catch block e .printStackTrace() ; } } } long end =System. currentTimeMillis(); System .out. println("Copy3执行时间为:" +(end -start)) ; }
(4)、//使用缓冲的文件字符流实现对文件的复制 private static void reader() { //1.创建文件对象 File file=new File ("C:\\Documents and Settings\\Administrator\\桌面\\Harvard.txt") ; //2.创建流对象 BufferedReader br=null; try { br =new BufferedReader( new FileReader (file)); String str=null; while((str =br. readLine())!= null){ System .out. println(str ); } } catch (IOException e) { e .printStackTrace() ; }finally{ if (br!= null) { try { br .close() ; } catch (IOException e) { e .printStackTrace() ; } } } }
(5)word\doc\xls等的复制只能用字节流,因为这些文件可能插入了图片、表格等内容,而且它本身已经经过封装,所以不能将其视为文本文件来操作。private static void copydoc() { //1.创建文件对象 File file=new File ("E:\\名单.xls"); File file2=new File ("d:\\名单11.xls"); //2.创建流对象 BufferedInputStream bis=null; BufferedOutputStream bos=null; try { bis =new BufferedInputStream( new FileInputStream(file )); bos =new BufferedOutputStream( new FileOutputStream(file2 )); byte[] bytes=new byte [100]; int len =-1; while((len =bis. read(bytes ))!=-1 ){ bos .write(bytes, 0, len ); bos .flush() ; } } catch (IOException e) { e .printStackTrace() ; }finally{ if (bos!= null) { try { bos .close() ; } catch (IOException e) { e .printStackTrace() ; } }if (bis!= null) { try { bis .close() ; } catch (IOException e) { e .printStackTrace() ; } } } System .out. println("复制完成" ); }
2.文件的分割
//将指定文件分割成指定的份数 private static void divide(String src ,int m) { long start =System. currentTimeMillis(); //目标文件 File file=new File (src); if(file .isFile()){ //文件的长度 int l =(int )file. length(); //获取目标目标文件的名字 String filename=file .getName() ; //System.out.println(filename); //每一份文件的长度 int L =l/ m; //获取截取之后的文件的名字 String parentName=file .getParent() ; String beforName=filename .substring( 0, filename.lastIndexOf( ".")); String endName=filename .substring( filename.indexOf (".")) ; //创建流对象 BufferedInputStream bi=null; BufferedOutputStream bo=null; try { bi =new BufferedInputStream( new FileInputStream(file )); byte[] bytes=new byte [1024* 1024]; int len =-1; for (int i = 1; i <=m; i++) { String name=parentName +File. separator+beforName +"_"+ i+endName ; File file2=new File (name); bo =new BufferedOutputStream( new FileOutputStream(file2 )); //开始读写了 while((len =bi. read(bytes ))!=-1 ){ bo .write(bytes, 0, len ); if (file2. length()> L) { break; } bo .flush() ; } } } catch (IOException e) { e .printStackTrace() ; }finally{ if (bo!= null) { try { bo .close() ; } catch (IOException e) { e .printStackTrace() ; } } if (bi!= null) { try { bi .close() ; } catch (IOException e) { e .printStackTrace() ; } } long end =System. currentTimeMillis(); System .out. println("分割时间为:" +(end -start)) ; System .out. println("分割完成!" ); } } }
3. 文件的合并
//将指定份数的文件合并为一个文件 private static void append(String ...name) { //获取目标文件 int n =name. length; //要写入的目标文件的名字 String beforName=name [0].substring (0, name[0].indexOf ("_")) ; String endName=name [0].substring (name[0].indexOf (".")) ; StringBuffer fileName=new StringBuffer (); fileName .append( beforName). append(endName ); File file=new File (fileName. toString()); //System.out.println(fileName); //创建流对象 BufferedInputStream bi=null; BufferedOutputStream bo=null; try { bo =new BufferedOutputStream( new FileOutputStream(file ,true ));//追加 for (int i = 0; i < name. length; i ++) { File file1=new File (name[i]); bi =new BufferedInputStream( new FileInputStream(file1 )); //开始读写 byte[] bytes=new byte [1024* 1024]; int len =-1; while((len =bi. read(bytes ))!=-1 ){ bo. write(bytes , 0, len); bo. flush(); } } } catch (IOException e) { e .printStackTrace() ; }finally{ if (bo!= null) { try { bo .close() ; } catch (IOException e) { e .printStackTrace() ; } }if (bi!= null) { try { bi .close() ; } catch (IOException e) { e .printStackTrace() ; } } } System .out. println("合并完成!" ); }
------- android培训、java培训、期待与您交流! ----------
0 0
- 黑马程序员__IO之文件复制、分割与合并
- 黑马程序员-学习日志-文件的分割与合并
- 黑马程序员 IO流 文件的分割与合并
- 黑马程序员__IO流
- 黑马程序员__IO流
- 黑马程序员__IO流
- 黑马程序员__IO流
- 黑马程序员__IO
- 黑马程序员__IO流
- 黑马程序员-PrintWriter-序列流-分割文件-合并文件
- 黑马程序员-SequenceInputStream文件合并-SplitFile文件分割
- 黑马程序员------ io流对一个图片文件进行分割与合并
- 黑马程序员—io之File的合并(SequenceInputStream)与分割
- c语言之分割与合并文件
- 文件分割与合并
- 黑马程序员-文件的切割与合并
- 黑马程序员__IO(字节流)
- 黑马程序员__IO流小结
- idea运行spark的wordcount与eclipse运行spark的wordcount示例,及本地运行sparkpi
- crash案例解析
- 设置vss为vs2010的默认源码管理器
- Python基础2 容器
- 初创团队-App如何在夹缝中生存
- 黑马程序员__IO之文件复制、分割与合并
- AndroidLoader详解
- Android之android.os.NetworkOnMainThreadException异常解决
- java中的堆、栈、常量池
- 初学Java—概念及小知识点的总结
- Java线程:新特征-阻塞队列
- log4j的配置--粗浅的拿来主义啊
- shell 脚本
- SVN和GIT的区别