Java文件读写

Java文件读写

字节流

可用于读写任意场景的文件,已二进制的方式,一个一个字节方式读取,也可以一次性以数组的方式读写多个字节,提高读写效率,常用与:音频、图片等文件。

public class FileUtils {
	/**
	 * 字节流读写文件
	 * @param fileReadPath
	 * @param fileWritePath
	 */
	public static void readFileByBytes(String fileReadPath, String fileWritePath){
//		File fileRead = new File(fileReadPath);
//		File fileWrite = new File(fileWritePath);
		InputStream fis = null;
		OutputStream fos = null;
		// 一次读取一个字节
		try {
			fis = new FileInputStream(fileReadPath);
			fos = new FileOutputStream(fileWritePath);
			int tempbyte;
			while((tempbyte = fis.read()) != -1) {
				System.out.println((char)tempbyte);
				fos.write(tempbyte);
			}
			fos.flush();
			fis.close();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(fos != null) {
					fos.close();
				}
				if(fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 一次性读写多个字节
		try {
			fis = new FileInputStream(fileReadPath);
			// 续写
			fos = new FileOutputStream(fileWritePath, true);
			int tempbyte;
			byte[] content = new byte[1024];
			// 换行
			fos.write("\r\n".getBytes());
			while((tempbyte = fis.read(content)) != -1) {
				System.out.println((char)tempbyte);
				fos.write(content, 0, tempbyte);
			}
			fos.flush();
			fis.close();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(fos != null) {
					fos.close();
				}
				if(fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 字符流读取数据,常用语于读文本,数字等类型的文件
	 * 
	 * @param fileReadPath
	 * @param fileWritePath
	 */
	public static void readFileByChars(String fileReadPath, String fileWritePath){
//		File fileRead = new File(fileReadPath);
//		File fileWrite = new File(fileWritePath);
		Reader reader = null;
		Writer writer = null;
		try {
			reader = new InputStreamReader(new FileInputStream(fileReadPath));
			// 文件不存在自动创建文件
			writer = new FileWriter(fileWritePath);
			int tempchar;
			while((tempchar = reader.read()) != -1) {
				// 对于windows下,\r\n这两个字符在一起时,表示一个换行。  
                // 但如果这两个字符分开显示时,会换两次行。  
                // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
				if (((char) tempchar) != '\r') {
//					System.out.println((char)tempchar);
					writer.write(tempchar);
				}
			}
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(reader != null) {
					reader.close();
				}
				if(writer != null) {
					writer.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		try {
			reader = new InputStreamReader(new FileInputStream(fileReadPath));
			// 自动往后添加
			writer = new FileWriter(fileWritePath, true);
			int tempchar;
			// 如果数据不能读满地 话,就会出现null字符,需要分情况处理
			char[] chars = new char[1024];
			while((tempchar = reader.read(chars)) != -1) {
				writer.write(chars, 0, tempchar);
			}
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(reader != null) {
					reader.close();
				}
				if(writer != null) {
					writer.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 文件按行读取写入
	 */
	public static void readRileByLine(String fileReadPath, String fileWritePath) {
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader(fileReadPath));
			bw = new BufferedWriter(new FileWriter(fileWritePath));
			String str = null;
			while ((str = br.readLine()) != null) {
				System.out.println(str);
				bw.write(str);
			}
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(br != null) {
					br.close();
				}
				if(bw != null) {
					bw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 无论是以上哪种方式读取文件,都需要频繁的读取,在读取小文件的时候,会使IO的开销过大
	 * BufferedInputStream 缓冲字节流,内部有一个8m的缓冲池,用于文件内容的读取,每次读取时,都近最大可能读满8m
	 * 已减少IO的开销,在调用read的时候,从8m的缓存中读取文件内容
	 * @param fileReadPath
	 * @param fileWritePath
	 */
	public static void readFileByBuffer(String fileReadPath, String fileWritePath){
		BufferedInputStream  bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(fileReadPath));
			bos = new BufferedOutputStream(new FileOutputStream(fileWritePath));
			int size=0;
            byte[] buffer=new byte[1024];
            while((size=bis.read(buffer))!=-1){
                bos.write(buffer, 0, size);
            }
            //刷新此缓冲的输出流,保证数据全部都能写出
            bos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(bis != null) {
					bis.close();
				}
				if(bos != null) {
					bos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	public static void main(String[] args) {
		// C:\Users\Ds\Desktop\1.txt	
//		BufferedInputStream
//		readFileByBytes("C:\\Users\\Ds\\Desktop\\1.txt","C:\\Users\\Ds\\Desktop\\2.txt");
//		readFileByChars("C:\\Users\\Ds\\Desktop\\1.txt","C:\\Users\\Ds\\Desktop\\3.txt");
		readFileByBuffer("C:\\Users\\Ds\\Desktop\\1.txt","C:\\Users\\Ds\\Desktop\\4.txt");
	}

转载:BufferedInputStream原理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值