文件IO简单读写操作

Java 文件 IO操作


I:input读的操作
O:output 写的操作

有兴趣移步新博客地址

1、用字节流读写文件

字节流可以读写所有类型的文件关键字:Stream

FileInputStream 为字节输入流
FileOutputStream 为字节输出流

	/*文件复制*/
	try {
	    File file = new File("text.txt");
		file.createNewFile();           //相对路径下创建一个txt文件
		FileInputStream fis =new FileInputStream(file); //将字节转化为输入流
		FileOutputStream fos = new FileOutputStream("text_new.txt");//将字节转化为输出流    text_new.txt为复制后得到的文件
		byte []bytes =new byte[20];
		while (fis.read(bytes)!=-1) {     //将字节用输入流读取到bytes里
			fos.write(bytes);            //将bytes里的字节写入到fos里
		}
		fis.close();
		fos.close();
								
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}

2、用带有缓冲的字节流读取文件

BufferedInputStream带缓冲的字节输入流 BufferedOutputStream带缓冲的字节输出流

	/**
	 * 使用BufferedInputStream  BufferedOutputStream 大文件复制
	 */
	try {
		FileInputStream fis  = new FileInputStream("D:\\txt\\项目.mp4");
		BufferedInputStream bis = new BufferedInputStream(fis);
		FileOutputStream fos = new FileOutputStream("D:\\txt\\pro.mp4");
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		byte [] bytes = new byte[10000];
		int count = 0;                                    //记次器
		long before = System.currentTimeMillis();        //计数器
		while (bis.read(bytes)!=-1){
		    bos.write(bytes);
		    count++;
		}
		bis.close();
		bos.close();
		
		System.out.println(System.currentTimeMillis()-before+"ms");   
		//计数,得到文件执行时间
		System.out.println("读取了"+count+"次");
	
	} catch (IOException e) {
	    e.printStackTrace();
	}

可以发现
使用带缓冲的字节流读写文件相较于字节流的读写文件耗费时间较少
可以将计数器放入字节流读写文件试一试(复制同一个文件所耗费时间比较)

3、用字符流读写文件

字符流只能读写纯文本类型的文件关键字: Writer Reader

InputStreamReader 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK,可设置utf-8。

OutputStreamWriter 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。

	File file = new File("text.txt");
	try {
		
		FileInputStream fis = new FileInputStream(file);               
		FileOutputStream fos = new FileOutputStream("text_new.txt");  
		InputStreamReader isr = new InputStreamReader(fis);       //输入流读取到字符    
		OutputStreamWriter osw = new OutputStreamWriter(fos);     //输出流写入到字符			
		char []input = new char[100];
		int l=0;                                           //l用于读取长度
		while ((l=isr.read(input))!=-1) {                 
	//		System.out.println(new String(input,0,l));     //读文件
			osw.write(input,0,l);                          //写文件 复制
		}
		isr.close();
		fis.close();
		osw.close();
		fos.close();									
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}

4、带缓冲的字节流读取文件

BufferedReader 带缓冲的字符输入流 BufferedWriter带缓冲的字符输出流

	File file = new File("text.txt");
	try {
		FileInputStream fis = new FileInputStream(file);               
		FileOutputStream fos = new FileOutputStream("text_new_Buffer.txt");  
		InputStreamReader isr = new InputStreamReader(fis);       //输入流读取到字符    
		OutputStreamWriter osw = new OutputStreamWriter(fos);     //输出流写入到字符
		BufferedReader br = new BufferedReader(isr);         //带缓冲的字符输入流
		BufferedWriter bw = new BufferedWriter(osw);         //带缓冲的字符输出流
		String input;			
		while ((input=br.readLine())!=null) {
			bw.write(input);            //发现文件读取时所有的文本到同一行内
			
		}
		bw.flush();             //缓冲区存在数据没有输出使其强制输出     
		br.close();
		bw.close();
	
		isr.close();
		fis.close();
		osw.close();
		fos.close();

	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
  • 可以看出带有缓冲的字符流读文件相较不带缓冲的流多了一个读行方法,也就是readLine()方法,
  • 可以看出带有缓冲的字符流写文件相较不带缓冲的流相同
  • 读写的优点就是它自带一个缓冲区

BufferedWriter写入数据时不会写入换行符解决方案

  1. 将BufferedWrite改为PrintWriter,用pw.println(input);输出即会换行。
  2. 可以使用和不带缓冲的流同样的方法

缓冲区如果数据没有完全写出解决方案

  1. 使用pw.flush();强制输出
  2. PrintWriter pw = new PrintWriter(osw,true);

以下是用字符流文件读写操作所产的三个文件
text.txt文件在这里插入图片描述
text_new_Buffer.txt文件
在这里插入图片描述
text_new_Print.txt文件
在这里插入图片描述


有兴趣的关注我的公众号,一起学习交流啊
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值