zoukankan      html  css  js  c++  java
  • 使用字节流(InputStream、OutputStream)简单完成对文件的复制

    文件的复制

    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 CopyFile {
    
    	public static void main(String[] args) {
    		// 将F盘下的test.txt文件复制到D盘
    		System.out.println("开始复制。。");
    		copy("F:/test.txt", "D:/test.txt");
    		System.out.println("复制成功!");
    	}
    
    	public static void copy(String src, String dest) {
    		// 准备复制源文件和目的地
    		File srcFile = new File(src);
    		File destFile = new File(dest);
    
    		// 准备输入输出流
    		InputStream in = null;
    		OutputStream out = null;
    
    		try {
    			in = new FileInputStream(srcFile);
    			out = new FileOutputStream(destFile);
    
    			byte[] flush = new byte[1024];
    			int len = -1;
    
    			// 边读边写
    			while ((len = in.read(flush)) != -1) {
    				out.write(flush, 0, len);
    			}
    
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    
    			// 关闭原则,先用后关
    			// 关闭输出流
    			if (out != null) {
    				try {
    					out.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    
    			// 关闭输入流
    			if (in != null) {
    				try {
    					in.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}// copy
    
    }
  • 相关阅读:
    Codeforces 1528E Mashtali and Hagh Trees
    Codeforces Round #709 (Div. 1, based on Technocup 2021 Final Round)
    Codeforces 1517G Starry Night Camping
    Codeforces 1508E Tree Calendar
    Codeforces 1508D Swap Pass
    Codeforces 1511G Chips on a Board
    Codeforces 1511F Chainword
    Codeforces 1516E Baby Ehab Plays with Permutations
    CF1539A Contest Start 题解
    关于 manacher
  • 原文地址:https://www.cnblogs.com/zxfei/p/10864248.html
Copyright © 2011-2022 走看看