Fork me on GitHub

I/O 流

输入流的几个常用方法:

1,复制一个文件;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class copyDeno {

    public static void copy(String src, String dest) {//src 原文件,dest 待复制的文件
//这儿需要加下判断src是否为文件和是否存在, InputStream in
= null; OutputStream out = null; byte[] b = new byte[1024]; int len; try { in = new FileInputStream(src); out = new FileOutputStream(dest); while ((len=in.read(b) )!= -1) { out.write(b,0,len); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void copyWithBuffer(String src, String dest) {//以上加入缓冲流 InputStream in = null; OutputStream out = null; byte[] b = new byte[20]; int len; try { in = new BufferedInputStream(new FileInputStream(src)); out =new BufferedOutputStream(new FileOutputStream(dest)); while ((len=in.read(b) )!= -1) { out.write(b,0,len); } //out.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { Release.free(in, out); } } public static void main(String[] args) throws IOException { copy("F:\\Word\\eclipseword\\a4\\file\\a.txt", "F:\\Word\\eclipseword\\a4\\copyfile\\a1.txt"); copyWithBuffer("F:\\Word\\eclipseword\\a4\\file\\b.txt", "F:\\Word\\eclipseword\\a4\\copyfile\\b1.txt"); } }

封装一个关闭流(释放资源)方法:

package IO;

import java.io.Closeable;
import java.io.IOException;
public class Release {
 public static void free(Closeable...stream){
	 for(Closeable st:stream){
		 if(st != null){
			 try {
				st.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
	 }
	
 }
}

  2,复制一个文件夹的所有内容到另外一个文件夹中


package IOfuxi;


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 IO.Release;


/*复制多级文件夹
* A:封装数据源File
* B:封装目的地File
* C:判断该File是文件夹还是文件
* a:是文件夹
* 就在目的地目录下创建该文件夹
* 获取该File对象下的所有文件或者文件夹File对象
* 遍历得到每一个File对象
* 回到C
* b:是文件
* 就复制(字节流)
* */
public class CopyFolderduo {


public static void main(String[] args) throws IOException {
copyfolder("F:\\Word\\eclipseword\\a4\\file",
"D:\\Java视频\\day21\\day21");

}

private static void copyfolder(String src,String des) throws IOException{
File file=new File(src);
File newfile=new File(des);
//判断文件是否存在
if(!file.exists()||!newfile.exists()){
return;
}else{
fuzhi(file,newfile);
}

}


private static void fuzhi(File file, File newfile) throws IOException {

// 判断该File是文件夹还是文件
if(file.isDirectory()){
File newFloder=new File(newfile,file.getName());
//创建目标路径文件夹
newFloder.mkdir();
//// 获取该File对象下的所有文件或者文件夹File对象
File[] arrfile=file.listFiles();
for(File f:arrfile){
//递归调用
fuzhi(f,newFloder);
}

}else{
File newf=new File(newfile,file.getName());
copyfile(file,newf);

}

}



//复制文件的方法
private static void copyfile(File file, File newf) throws IOException {
BufferedInputStream in= new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(newf));
byte[] arr=new byte[1024];
int len;
while((len=in.read(arr))!=-1){
out.write(arr, 0, len);
out.flush();
}
// out.flush();
Release.free(in,out);
}


}

 

 3.压缩一个文件夹

 1 package zy823;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.FileNotFoundException;
 8 import java.io.FileOutputStream;
 9 import java.io.InputStream;
10 import java.util.zip.ZipEntry;
11 import java.util.zip.ZipOutputStream;
12 
13 public class zipFolder {
14      public static void zipFile(String src,String dest){
15             ZipOutputStream zos = null;
16             try {
17                 zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));//
18                 File fileSrc = new File(src);
19               if(!fileSrc.exists()){
20                throw new FileNotFoundException();
21               }else{
22                 zip(fileSrc.getName(),zos,fileSrc);
23                   zos.flush();
24                   zos.close();
25               }
26             } catch (FileNotFoundException e1) {
27             // TODO Auto-generated catch block
28               e1.printStackTrace();
29             }catch (Exception e) {
30               // TODO Auto-generated catch block
31              e.printStackTrace();
32             }
33             
34           }
35           private static void zip(String dest,ZipOutputStream zos,File src)throws Exception{
36             byte[] b = new byte[1024];
37             InputStream is = null;
38             if(src.isFile()){
39                 zos.putNextEntry(new ZipEntry(dest));
40               is = new BufferedInputStream(new FileInputStream(src));
41               int len;
42               while((len = is.read(b)) != -1){
43                 zos.write(b, 0, len);
44               }
45               zos.flush();
46               is.close();
47               zos.closeEntry();
48             }else{
49                  File[] files = src.listFiles();
50                zos.putNextEntry(new ZipEntry(dest+"/")); 
51                for(File temp:files){
52                  zip(dest+"/"+temp.getName(),zos,temp);
53                }
54             }
55           }
56 
57 }

 

posted @ 2018-08-22 00:06  雪人奥特曼  阅读(191)  评论(0编辑  收藏  举报