码迷,mamicode.com
首页 > 其他好文 > 详细

I/O 流

时间:2018-08-22 01:10:25      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:==   lips   exce   clip   输入流   directory   lse   java   mkdir   

输入流的几个常用方法:

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 流

标签:==   lips   exce   clip   输入流   directory   lse   java   mkdir   

原文地址:https://www.cnblogs.com/ysg520/p/9515293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!