| 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();                                                                                 }                                                                         }                                                                      |                             
原文:https://www.cnblogs.com/meihao1203/p/9181922.html