留记录 耐心点,等待时间给你所有答案...

JAVA 文件复制

2019-04-19

一、IO流介绍

字节流: ​ 输入流:InputStream ​ 方法: ​ read(byte[] b) ​ 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。

​ 输出流:OutPutStream ​ write(byte[] b, int off, int len) ​ 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。 字符流: ​ 输入流:Reader ​ 输出流:Writer

二、InputStream 文件读取

	public static void inputStreamMethod(){
		File f = new File("F:\\z暂存文件\\test.txt");
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(f);
			byte[] car = new byte[10];
			//接收实际读取大小
			int len = 0;
			while ((len = inputStream.read(car))!=-1) {
				//输出、字节数组转成字符串
				String info = new String(car,0,len);
				System.out.println(info);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("文件不存在");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("文件读取失败");
		} finally {
			if(inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
					System.out.println("流关闭失败");
				}
			}
		}
	}

三、OutPutStream文件输出

	public static void outputStreamMethod() {
		File f = new File("F:\\z暂存文件\\test.txt");
		OutputStream os = null;
		try {
			//追加形式、否则默认是覆盖
			os = new FileOutputStream(f,true);
			//操作
			String str = "\r\n这是即将要输出去的字符串1";
			//字符串转字节数组
			os.write(str.getBytes("utf-8"));
			//强制刷新
			os.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("文件找不到");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("文件写出失败");
		} finally {
			if(os!=null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

四、文件复制

	public static void fileCopy() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss");
		Date start = new Date();
		System.out.println("start time:"+df.format(start));
		//源头文件 F:\\z暂存文件\\test.txt
		File inFile = new File("D:\\DevelopmentTools\\VM\\os\\CentOS-7-x86_64-DVD-1611.iso");
		//目的地文件
		File outFile = new File("F:\\z暂存文件\\CentOS-7-x86_64-Minimal-1511.iso");
		if(outFile.exists()) {
			outFile.mkdirs();
		}
		InputStream inStream = null;
		OutputStream oStream = null;
		try {
			inStream = new FileInputStream(inFile);
			oStream = new FileOutputStream(outFile);
			byte[] by = new byte[1024];
			int len = 0;
			while ((len = inStream.read(by))!=-1) {
				//String s = new String(by);
				//System.out.println(s);
				oStream.write(by, 0, len);
			}
			oStream.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.out.println("上传文件找不到");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("文件处理异常");
		} finally {
			if(oStream!=null) {
				try {
					oStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(inStream!=null) {
				try {
					inStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		Date end = new Date();
		System.out.println("end time:"+df.format(end));
		System.out.println("total time:"+(end.getTime()-start.getTime()));
	}

平行世界,多元生活,愿你我既可以朝九晚五,又能够浪迹天涯。

– 《好吗好的》


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏博主

上一篇 ActiveMq 安装

留言