java _io_ .read () to fetch bytes

byte [] flush = new [n ] byte // byte array acts as a buffer vessel
.read (flush) // return each read n bytes, when the number of data bytes is not enough to return the actual number of bytes
int len = -1; // receiving read (flush) returns the actual length of the
String s = new String (flush, 0, len) // decoding operation, len actual size must, otherwise returns garbage characters excess length

public static void main(String[]args) 
{
    //创建源
    File f=new File("C:/Users/10853/eclipse-workspace/hell/src/hell/abc");
    InputStream is =null;

    //选择流
    try {
         is =new FileInputStream(f);
    //操作(读取),分段读取   
        byte[] flush =new byte[3]; //缓冲容器
        int len=-1; //接收长度
        while((len=is.read(flush))!=-1) //is.read(car)每次读取三个字节,读取完毕会返回-1,
                                        //当数据不够3时,将返回实际数据个数
        {                           
            String s =new String(flush,0,len); //必须返回实际的大小,否则多余的长度会返回垃圾字符
            System.out.println(s);
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally
    {
        try {
            if(null!=is)
            {
                is.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

} 

Guess you like

Origin blog.51cto.com/14437184/2423102