Java字节流复制文件

版权声明:我的博客 © lidianchun.com | https://blog.csdn.net/qq_36120267/article/details/82114141

 字节流复制文件

	public static void main(String[] args) {

		BufferedInputStream bis = null;

		BufferedOutputStream bos = null;

		try {

			FileInputStream fis = new FileInputStream("c:\\from.txt");

			bis = new BufferedInputStream(fis);

			FileOutputStream fos =

					new FileOutputStream("D:\\to.txt");

			bos = new BufferedOutputStream(fos);

			long len = bis.available();

			if (len > 1024 * 1024 * 200) {

				int b;

				while ((b = bis.read()) != -1) {

					bos.write(b);

				}

			}

			else {

				byte[] bytes = new byte[(int) len];

				bis.read(bytes);

				bos.write(bytes);

			}

			System.out.println("文件复制已经完成!");

		}

		catch (Exception e) {

			e.printStackTrace();

		}

		finally {

			if (bis != null) {

				try {

					bis.close();

				}

				catch (IOException e) {

					e.printStackTrace();

				}

			}

			if (bos != null) {

				try {

					bos.close();

				}

				catch (IOException e) {

					e.printStackTrace();

				}

			}

		}

	}

猜你喜欢

转载自blog.csdn.net/qq_36120267/article/details/82114141