io流之复制文件(包含文件读和写)

io流之复制文件(包含文件读和写)

	public static void method3(){
		try {
			FileInputStream inputStream = new FileInputStream("D:\\Files\\test\\a.txt");//参数为读取文件的路径
			BufferedInputStream bufferedIn = new BufferedInputStream(inputStream);
			FileOutputStream fileOut = new FileOutputStream("D:\\Files\\test\\b.txt");//要为写入文件的路径
			BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOut);
			byte[] bytes = new byte[15];
			int length = 0;
			//bufferedIn.read(bytes)) 如果文件的内容已经全部读取完成,返回值为-1
			while((length = bufferedIn.read(bytes)) != -1){
				bufferedOut.write(bytes, 0, length);
			}
			bufferedOut.flush();//强制把缓冲区中的内容写入到文件中
			bufferedOut.close();
			fileOut.close();
			bufferedIn.close();
			inputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

猜你喜欢

转载自blog.csdn.net/qq_43039260/article/details/90730879
今日推荐