文件复制管道流

 

public static void copyChannel(File src,File dest){
		FileInputStream fin = null;
		FileOutputStream fout = null;
		FileChannel in = null;
	    FileChannel out = null;
		try {
			fin = new FileInputStream(src);
			fout = new FileOutputStream(dest);
			in = fin.getChannel();
		    out = fout.getChannel();
		    in.transferTo(0, in.size(), out);
			in.close();
			out.close();
			fin.close();
			fout.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in!=null) {
					in.close();
				}
				if (out!=null) {
					out.close();
				}
				if (fin!=null) {
					fin.close();
				}
				if (fout!=null) {
					fout.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	public static void copy(File src,File dest){
		try {
			InputStream in = new FileInputStream(src);
			OutputStream out = new FileOutputStream(dest);
			int c = -1;
			while ((c=in.read())!=-1) {
				out.write(c);
			}
			in.close();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

你可能感兴趣的:(文件复制)