java Io梳理

java IO分为字节流和字符流

字节流

操作的是计算机中的字节,最小的存储单位1个字节1个字节8个二进制位,是很具体的存储空间,可以操作任意文件(mp3 图 视频等java中都是以Stream结尾

字符流

  专门用于读写文本文件的(记事本可以正常打开),查询本机上的编码表(GBK)java中都是以Reader或者Writer结尾


1、字节流

    字节流在进行读写时操作的基本单位是字节,一般在使用字节流时会包装成带缓冲区的字节流,加上缓冲区后,在进行读写时会优先读取缓冲数据,减少访问磁盘IO的次数,并且在读取速度上也比较快

  

public class TerstIO {
	public static void main(String[] args) throws Exception {
		
		/**
		 * 字节先写入缓冲区,然后在写入到Buffer,指定写入缓冲区的大小
                 *当指定了缓冲区后从从缓冲区读,缓冲区没有时会从磁盘读,并一次性读满缓冲区,减少磁盘IO 写也一样
                 */
		FileInputStream in = new FileInputStream("aa测试.avi");
		BufferedInputStream bin = new BufferedInputStream(in,100000);
		
		FileOutputStream out = new FileOutputStream("aa.avi");
		BufferedOutputStream bout = new BufferedOutputStream(out,100000);
		long t1 = System.currentTimeMillis();
		//指定buffer的大小,用于接受读取的内容
		byte input[] = new byte[100000];
		int count= 0;
		//将数据读取到数组input中,直到装满数组。
		while(bin.read(input)!=-1){
			//将读到的字节数组直接写入
			bout.write(input);
			count++;
		}
		bout.close();
		bin.close();
		System.out.println(System.currentTimeMillis()-t1+"ms");
		System.out.println(count);
	}
}

2、字符流

      对于纯文本文件操作的基本单位可以是字符,此时可以使用字符流:

 a)字节流和字符流可以相互转换

    public static void main(String[] args) throws IOException {
        File f = new File("java.txt");
        FileInputStream in = new FileInputStream(f);
        //创建一个字符流
        InputStreamReader input = new InputStreamReader(in);
        
        
        FileOutputStream ou = new FileOutputStream("java_test.txt");
        OutputStreamWriter w = new OutputStreamWriter(ou);
        char [] ch = new char[100];
        int l =0;
        //同样是将读取的字符放入到ch中
        while((l=input.read(ch))!=-1){
            
            //由于最后时 读取的字符个数可能不够100,为避免之前读取的一些冗余数据再次被输出需要制定起点位置和长度l
            System.out.println(new String(ch, 0, l));
            //写入的时同理
            w.write(ch, 0, l);
        }
        input.close();
        in.close();
        ou.close();
        w.close();
        
    }
b)同样带有缓冲区的字符流

	public static void main(String[] args) throws IOException {
		File f = new File("java.txt");
		
		FileInputStream in = new FileInputStream(f);
		InputStreamReader input = new InputStreamReader(in);
		
		FileOutputStream ou = new FileOutputStream("java_test.txt");
		OutputStreamWriter w = new OutputStreamWriter(ou);

		//如果使用Buffer 在进行读写的时候是先读写到缓冲区中然后在写入到磁盘 减少了对磁盘的访问次数
		BufferedReader read = new BufferedReader(input);
		BufferedWriter write = new BufferedWriter(w);//如果读的文件里面有换行符使用这是方式不会把换行写入进去
		//这种方式可以含有换行
//		PrintWriter pw =new PrintWriter(w);
//		pw.println(sstr);
		String str;
		while((str=read.readLine())!=null){
			write.write(str);
		}
		
		//由于在进行写文件时,会先把读到的内容读入到缓存中,如果缓存满了会自动写如到磁盘
		//如果不满不会写入,在写文件结束时为避免缓存不满无法写入到磁盘 需要flush 强制输出下 PrintWriter也一样
		//或者:PrintWriter pw =new PrintWriter(w,true)
		write.flush();
		input.close();
		in.close();
		ou.close();
		w.close();
		
	}
}
c)上述操作太过于麻烦,java.io 的API提供了对纯文本文件的字符流操作方式

	/**
	 * 是一个非常便捷的操作纯文本文件的字符操作类,如果操作的是字节流使用FileInputStream
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		FileReader fr = new FileReader("java.txt");
		//读入缓存
		BufferedReader br = new BufferedReader(fr);
		
		FileWriter fw = new FileWriter("java_new.txt");
		//写入缓存中
		BufferedWriter bw = new BufferedWriter(fw);
		
		String src = null;
		while((src = br.readLine())!=null){
			bw.write(src+"\n");
		}
		
		bw.flush();
		fw.flush();
		bw.close();
		fw.close();
		br.close();
		fr.close();
		
	}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值