JAVA 输入流 笔记

package com.winston.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;


public class InputStreamTest {		
	
	public static void main (String[] args) {
		test2();
    }
	
	
    private static void test2 () {
    	
    	try {
	        InputStream is = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\test.txt"));
	        //缓冲区
	        byte[] b = new byte[4];
			//本次读取长度	
	        int len;
	        StringBuffer sbf = new StringBuffer();
			while((len=is.read(b))!=-1){//把读取的数据放到i中
				for (byte c : b) {
	                System.out.print(c+" ");
                }
				System.out.println();
				String tmp = new String(b,0,len,"UTF-8");
				System.out.println("本次读取长度:"+len+"、内容:"+tmp);
				sbf.append(tmp);
			}
			System.out.println("-------读取完成------");
			System.out.println(sbf.toString());
			//把字节数组转成字符串
			//关闭流
			is.close();
        } catch (Exception e) {
	        e.printStackTrace();
        }
    }
}

 

 

本例设置的byte数组长度为4。每次读取4个字节。

 

文档采用UTF-8编码格式。其中字母各占一个字节。汉字占3个或4个字节。

读取字节流的时候 第一次读取sss三个字节。UTF-8编码的“大”占了3个字节。因此会读取sss加上“大”的第一个字节。所以字节流读取中文的时候 会存在乱码的情况。

 

 

public class BufferedReader extends Reader

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

使用字符流读取文件。

 


package com.winston.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;


public class InputStreamTest {		
	
	public static void main (String[] args) {
		test3();
    }
	
	 private static void test3 () {
		 BufferedReader in = null ;
		 try {
	        in = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\test.txt"));
	        StringBuffer sbf = new StringBuffer();
	        String tmp;
	        while ((tmp=in.readLine())!=null) {
	        	System.out.println(tmp);
	        	sbf.append(tmp);
            }
	        System.out.println("-------读取完成------");
	        System.out.println(sbf.toString());
        } catch (Exception e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
        }finally{
        	if(in!=null){
        		try {
	                in.close();
                } catch (IOException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
                }
        	}
        }
		 
	 }
	
    private static void test2 () {
    	InputStream in =null;
    	try {
	         in = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\test.txt"));
	        //缓冲区
	        byte[] b = new byte[4];
			//本次读取长度	
	        int len;
	        StringBuffer sbf = new StringBuffer();
			while((len=in.read(b))!=-1){//把读取的数据放到i中
				for (byte c : b) {
	                System.out.print(c+" ");
                }
				System.out.println();
				String tmp = new String(b,0,len,"UTF-8");
				System.out.println("本次读取长度:"+len+"、内容:"+tmp);
				sbf.append(tmp);
			}
			System.out.println("-------读取完成------");
			System.out.println(sbf.toString());
			//把字节数组转成字符串
			//关闭流
			in.close();
        } catch (Exception e) {
	        e.printStackTrace();
        }finally{
        	if(in!=null){
        		try {
	                in.close();
                } catch (IOException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
                }
        	}
        }
    }
    
    
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值