使用 FileInputStream / FileOutputStream 复制文件

版权声明:借鉴时注明出处就行 https://blog.csdn.net/weixin_42144379/article/details/84854240

使用 FileInputStream / FileOutputStream 复制文件 code:

	/**
	 * @param source	必须存在此文件
	 * @param aim		此文件可以不存在
	 * @throws IOException	可能会抛出的异常
	 */
	private static void copyFile(File source,File aim) throws IOException {
		FileInputStream in =new FileInputStream(source);
		FileOutputStream out = new FileOutputStream(aim);
		byte [] b = new byte[1024*8];
		int len = 0;
		while ((len=in.read(b))>0) {
			out.write(b, 0, len);
		}
		in.close();
		out.close();
	}

测试代码:

public static void main(String[][] args) throws IOException{
		//从桌面复制到桌面并重命名
		copyFile(new File("C:\\Users\\jacktu\\Desktop\\源文件.zip"), 
				 new File("C:\\Users\\jacktu\\Desktop\\目标文件.zip"));
	}

回到桌面,发现多了一个  "目标文件.zip" -- 就不放图了.

猜你喜欢

转载自blog.csdn.net/weixin_42144379/article/details/84854240