I/O 流
Posted ysg520
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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[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 流的主要内容,如果未能解决你的问题,请参考以下文章