java IO之InputStream的简单demo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_20172379/article/details/87882029
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 字节输入流 InputStream
 * 1.读取单个字节 public abstact int read() throws IOException
 *   返回值:返回读取的字节内容,如果现在已经没有内容返回-1
 *
 * 2.读取的数据保存在字节数组 public abstact int read(byte [] b)  throws IOException
 *   返回值:返回读取的数据长度,如果读取到结尾了,返回-1
 *
 * 2.读取的数据保存在部分字节数组 public abstact int read(byte [] b,int off,int len)  throws IOException
 *    返回值: 返回读取的部分数据长度,如果读取到结尾了,返回-1
 *
 */
public class javaioinputstream {
    public static void main(String[] args) throws Exception {
        File file  = new File("c:"+File.separator+"demo"+File.separator+"my.txt");
        if(file.exists()){
            InputStream is  =  new FileInputStream(file);
            byte[] data  = new byte[1024*1024];
//            System.out.println("data1--------中國"+new String(data));
//            System.out.println("data1--------中國");
            int foot  = 0 ; //字节数组的操作脚标
            int temp  = 0 ;  //接收每次读取的字节数据
            while ((temp=is.read())!=-1){
                data[foot++] = (byte) temp;
            }
//            System.out.println("data2----"+new String(data));
//            int len  =  is.read(data);
            is.close();
//            System.out.println("【"+new String(data,0,len)+"】");
            System.out.println("foot:"+foot);//3
            System.out.println("【"+new String(data,0,foot)+"】");
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_20172379/article/details/87882029