• Java——IO类,字节流读数据


    字节流读取数据
    █ InputStream
         ☞FileInputStream;             

    █ FileInputStream的构造方法
        ☞FileInputStream(File file);                   //构造对象不比要求文件存在,会自动创建文件   
        ☞FileInputStream(String name);              

    █ FileInputStream的成员方法
        ☞public int read();               //read 读完一次会自动偏移到下一个字节,返回一共读到字节长度,读完文件尾就返回-1;
        ☞public int read(byte[] b);             
        ☞int read(byte[] b, int off, int len);                
    public class IOTest2 {
    public static void main(String[] args) throws IOException {
                     FileInputStream fis = new FileInputStream("1.txt");
                     //int read = fis.read();        //文件里是 d,当时write(100),写进文件是 d 读出来又变为 100
                                                                           //read 只读最后一个字节
                     /*System.out.println("read(): "+read);    //read(): 100
                     char ch = (char)read;
                     System.out.println("转化:"+ch);       //转化:d   */       
                     byte[] bs = new byte[10];
                    int readLength = fis.read(bs);         //返回读到的实际字节长度;read每次读完一个字节会自动偏移到下一个
                    String string = new String(bs);          //这里构造string,前面开辟多少空间,就算数组里只有部分有数据,转换的时候还是会按定义长度转换,没数据的当空格;
                                                                           //如果前面的是10 ,后面生成的string就会比是6的时候多一些空白,更长一点
                    System.out.println("readTobs: "+string+"  readLength: "+readLength);                                 //readTobs: 201703  readLength: 6        //数组长度为6
      //readTobs: 201703      readLength: 6   //数组长度为10
                    String string1 = new String(bs,0,readLength);     //消除上面存在的多余空格
                    System.out.println("readTobs: "+string1+"  readLength: "+readLength);

    int length = fis.read(bs,0,10);    //参数一表示往哪个数组读,参数二表示从数组的那个位置写,第三个参数表示数组长度
    String string2 = new String(bs,0,length);       //构建字符串的时候指定从数组哪里开始构建,构建多长;
    System.out.println("readTobs: "+string2+"  readLength: "+length);
    fis.close();     //释放资源
            }
    }



    public class IOTest3 {

            public static void main(String[] args) throws IOException {
                    FileInputStream fis = new FileInputStream("DiguiTest.java");
    /*             int read = 0;
                    while((read = fis.read()) != -1){           //read 读到文件尾会返回 -1
                            char ch = (char)read;
                            System.out.print(ch);
                    }*/

                    //一次读取一个字节数组
                    //使用字节流不能处理文本中的中文,会出现乱码;但是构建成String就不会出现乱码
                    byte[] bytes = new byte[100];
                    int length = 0;
                    while((length = fis.read(bytes)) != -1){
                            String string = new String(bytes,0,length);
                            System.out.print(string);
                    }
                    fis.close();
            }
    }






  • 相关阅读:
    集成学习
    集成学习
    集成学习
    集成学习-Majority Voting
    pandas 之 groupby 聚合函数
    LDA-作为线性判别 降维 推导
    LDA-作为线性判别 分类器 推导
    ML-逻辑回归推导
    第一册:lesson eighty one.
    第一册:lesson seventy nine.
  • 原文地址:https://www.cnblogs.com/meihao1203/p/9181922.html
Copyright © 2020-2023  润新知