eg8:使用字符流或字节流进行文档与图片的复制

1.字符流复制图片

package Hcybx;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
	public static void main(String[] args) {
		// 源文件路径
		String oldPictrue = "F:\\File\\file\\哪吒之魔童降世.jpg";
		// 复制文件存放路径
		String newPicture = "F:\\File\\file\\哪吒之魔童降世(复制版).jpg";
		// 复制图片目标文件
		try {
			FileReader fr = new FileReader(oldPictrue);
			FileWriter fw = new FileWriter(newPicture);
			// 方法一:一个字符复制
			int c = 0;
			while ((c = fr.read()) != -1) {
				fw.write(c);
			}
			System.out.println("图片复制成功!");
			fw.flush();//刷新
			fw.close();
			fr.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}

	}
}

运行结果:
在这里插入图片描述
在这里插入图片描述
得出结论:字符流不能复制图片

2.字符流复制文档

package Hcybx;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
	public static void main(String[] args) {
		// 源文件路径
		String oldPictrue = "F:\\File\\file\\字符流.txt";
		// 复制文件存放路径
		String newPicture = "F:\\File\\file\\字符流(复制版).txt";
		try {
			FileReader fr = new FileReader(oldPictrue);
			FileWriter fw = new FileWriter(newPicture); 
			//一个一个字符复制
//			int c = 0;
//			while ((c = fr.read()) != -1) {
//				fw.write(c);
//			}
//			System.out.println("字符流复制成功!");
//			fw.flush();
//			fw.close();
//			fr.close();
			//方法二:多个字符复制
			char[] ch = new char[1024*1024];
			while ((fr.read(ch)) != -1) {
				fw.write(ch);
			}
			System.out.println("字符流复制成功!");
			fw.flush();
			fw.close();
			fr.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

结果截图:
在这里插入图片描述
注意原文档和编译器的编码格式必须一致,否则会出现乱码的情况

3.字节流复制文档

package Hcybx;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {
	public static void main(String[] args) {
		// 源文件路径
		String oldPictrue = "F:\\File\\file\\字符流.txt";
		// 复制文件存放路径
		String newPicture = "F:\\File\\file\\字符流(复制版).txt";
		// 复制图片目标文件
		try {
			FileInputStream fis = new FileInputStream(oldPictrue);
			FileOutputStream fos = new FileOutputStream(newPicture);
			// 方法一:单个字节复制
//			int c = 0;
//			while ((c = fis.read()) != -1) {
//				fos.write(c);
//			}
			//方式二:多个字节复制
			byte[] by = new byte[1024*1024];
			while ((fis.read(by)) != -1) {
				fos.write(by);
			}
			System.out.println("文档复制成功!");
			fos.flush();// 刷新
			fos.close();
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

运行结果:
在这里插入图片描述

4.字节流复制图片

package Hcybx;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {
	public static void main(String[] args) {
		// 源文件路径
		String oldPictrue = "F:\\File\\file\\哪吒之魔童降世.jpg";
		// 复制文件存放路径
		String newPicture = "F:\\File\\file\\哪吒之魔童降世(字节流复制).jpg";
	    try {
			FileInputStream fis = new FileInputStream(oldPictrue);
			FileOutputStream fos = new FileOutputStream(newPicture);
			//一个一个字节复制
//			int c = 0;
//			while ((c = fis.read()) != -1) {
//				fos.write(c);
//			}
			//多个字节复制
			byte[] by = new byte[1024*1024];
			while ((fis.read(by)) != -1) {
				fos.write(by);
			}
			System.out.println("字节复制图片成功!");
			fos.flush();
			fos.close();
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

运行结果:
在这里插入图片描述

发布了46 篇原创文章 · 获赞 41 · 访问量 8428

猜你喜欢

转载自blog.csdn.net/weixin_42635052/article/details/100186780
今日推荐