java基础巩固 二.字节流操作

9 篇文章 0 订阅

读文件

/**
	 * 读文件
	 * @param file 目标文件
	 * @throws IOException
	 */
	public static void readFile(File file) throws IOException { 
		//实例化文件输入流
		if(!file.exists()){
			throw new IllegalAccessError("文件" + file + "不存在");
		}
		FileInputStream in = new FileInputStream(file);
		//按每个字节进行读取
		int temp;//读取的数据
		int count = 0;//读取的数据的长度
		while((temp = in.read()) != -1){
			//按照16进制进行输出
			
			//小于两个字节进行补零操作
			if(temp < 0Xf){
				System.out.print("0");
			}
			System.out.print(Integer.toHexString(temp) + "  ");
			//换行
			if(++count % 10 == 0){
				System.out.println();
			}
		}
		in.close();
		
		
	}
	
	public static void readFile(String file) throws IOException {
		readFile(new File(file));
	}

写入文件

/**
	 * 写文件
	 * @param file 目标文件
	 * @param data 向文件中写的内容
	 * @param isAdd 是否向文件内追写内容,false则删除原文件,新建一个文件进行写操作
	 * @throws IOException
	 */
	public static void putFile(File file,byte[] data,FileOutputStream out) throws IOException{
		//创建文件输出流
		for(byte b: data){
			out.write(b);
		}
		out.close();
		
	}
	
	public static void putFile(String file,byte[] data,boolean isAdd) throws IOException{
		FileOutputStream out = new FileOutputStream(file,true);
		putFile(new File(file),data,out);
	}
	
	public static void putFile(String file,byte[] data) throws IOException{
		FileOutputStream out = new FileOutputStream(file);
		putFile(new File(file),data,out);
	}
	

拷贝文件

/**
	 * 按字节进行文件复制
	 * @param fromFile 数据来源文件
	 * @param ToFile 复制到的文件
	 * @return
	 * @throws IOException 
	 */
	public static boolean copyFileByBite(File fromFile,File toFile) throws IOException {
		if(!fromFile.exists()){
			throw new IllegalArgumentException("文件" + fromFile + "不存在");
		}
		if(!fromFile.isFile()){
			throw new IllegalArgumentException("文件" + fromFile + "不是文件");
		}
		
		//创建读取文件的输入流
		FileInputStream in = new FileInputStream(fromFile);
		//创建写入文件的输出流
		FileOutputStream out = new FileOutputStream(toFile,true);
		
		int inFlag = 0;//输入流读取的文件的长度
		while((inFlag = in.read()) != -1){
			out.write(inFlag);
			out.flush();
		}
		in.close();
		out.close();
		return true;
	}
	
	
	/**
	 * 按通过一定的缓冲数组进行文件复制
	 * @param fromFile 数据来源文件
	 * @param ToFile 复制到的文件
	 * @return
	 * @throws IOException 
	 */
	public static boolean copyFileByBuffer(File fromFile,File toFile) throws IOException {
		if(!fromFile.exists()){
			throw new IllegalArgumentException("文件" + fromFile + "不存在");
		}
		if(!fromFile.isFile()){
			throw new IllegalArgumentException("文件" + fromFile + "不是文件");
		}
		
		//创建读取文件的输入流
		FileInputStream in = new FileInputStream(fromFile);
		//创建写入文件的输出流
		FileOutputStream out = new FileOutputStream(toFile,true);
		//创建读取文件的缓冲区
		byte[] buffer = new byte[10 * 1024];
		int inFlag = 0;//输入流读取的文件的长度
		while((inFlag = in.read(buffer)) != -1){
			//将缓冲区中读入的数据写入文件
			for(int i = 0;i < inFlag;i++){
				out.write(buffer[i]);
			}
			out.flush();
		}
		in.close();
		out.close();
		return true;
	}
	
	
	/**
	 * 通过带缓冲的字节流来复制
	 * @param fromFile 数据来源文件
	 * @param ToFile 复制到的文件
	 * @return
	 * @throws IOException 
	 */
	public static boolean copyFileByBufferStream(File fromFile,File toFile) throws IOException {
		if(!fromFile.exists()){
			throw new IllegalArgumentException("文件" + fromFile + "不存在");
		}
		if(!fromFile.isFile()){
			throw new IllegalArgumentException("文件" + fromFile + "不是文件");
		}
		
		//创建读取文件的输入缓冲流
		BufferedInputStream bin = new BufferedInputStream(
				new FileInputStream(fromFile));
		//创建写入文件的输出缓冲流
		BufferedOutputStream bout = new BufferedOutputStream(
				new FileOutputStream(toFile));

		int flag;
		while((flag = bin.read()) != -1){
			bout.write(flag);
			bout.flush();
		}
		
		bin.close();
		bout.close();
		return true;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值