1. 程式人生 > >I/O 流

I/O 流

fileinput with 文件夾 isf java style dir files list

輸入流的幾個常用方法:

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[20]; 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 zy821;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
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 copywenjian {
    public static void copyDir(String path, String destpath) {
        File src = new File(path);

        File dest = new File(destpath);

        if (!src.exists())
            return;

        if (!dest.exists())
            return;

        if (src.isFile()) {
            copyFile(path, destpath + "/" + src.getName());
        }

        if (dest.isDirectory()) {

        }
        copywenjianjia(src.getAbsolutePath(), dest.getAbsolutePath() + "/" + src.getName());
    }

    public static void copywenjianjia(String path, String destpath) {
        File src = new File(path);
        File dest = new File(destpath);

        if (!dest.exists()) {
            dest.mkdir();
        }

        // (new File(destpath + path)).mkdir();

        File[] file = src.listFiles();
        if (file == null) {
            return;
        }
        for (File f : file) {
            if (f.isFile()) {
                // new File(f.getName()).mkdir();
                copyFile(f.getAbsolutePath(), (destpath + "/" + f.getName()));
            } else if (f.isDirectory()) {
                copywenjianjia(f.getAbsolutePath(), (destpath + "/" + f.getName()));
            }
        }
    }

    public static void copyFile(String oldPath, String newPath) {// 復制文件方法
        InputStream in = null;
        OutputStream out = null;
        byte[] b = new byte[2048];
        int len;
        try {
            in = new BufferedInputStream(new FileInputStream(oldPath));
            out = new BufferedOutputStream(new FileOutputStream(newPath));
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
            }
            out.flush();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            release.free(in, out);
        }
    }

}

package IO;
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) {InputStream in = null;OutputStream out = null;byte[] b = new byte[20];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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (in != null) {try {in.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (out != null) {try {out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {Release.free(in, out);/*if (in != null) {try {in.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (out != null) {try {out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}*/}}
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");FileInputStream fis = new FileInputStream("");// 封裝目的地FileOutputStream fos = new FileOutputStream("");
// 復制數據int by = 0;while ((by = fis.read()) != -1) {fos.write(by);}// 釋放資源fos.close();fis.close();}}

I/O 流