body, table{font-family: 微软雅黑}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}

字节流读取数据
█ 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();
        }
}

Java——IO类,字节流读数据的更多相关文章

  1. Java——IO类 字节流概述

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  2. Java IO: 其他字节流(上)

    作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) 本小节会简要概括Java IO中的PushbackInputStream,SequenceInputS ...

  3. Java IO 类一览表

    下表列出了大多数(非全部)按输/输出,基于字节或字符划分的 Java IO 类.

  4. Java——IO类,字节流写数据

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  5. Java——IO类,字节流缓冲区

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  6. [Java IO]02_字节流

    概要 字节流有两个核心抽象类:InputStream 和 OutputStream.所有的字节流类都继承自这两个抽象类. InputStream 负责输入,OutputStream 负责输出. 字节流 ...

  7. Java IO之字节流

    Java中的输入是指从数据源等读到Java程序中,这里的数据源可以是文件,内存或网络连接,输出则是指从Java程序中写到目的地. 输入输出流可以分为以下几种类型(暂时不考虑File类) 类名 中文名 ...

  8. Java IO流-字节流

    2017-11-05 17:48:17 Java中的IO流按数据类型分类分为两种,一是字节流,二是字符流.字符流的出现是为了简化文本数据的读入和写出操作. 如果操作的文件是文本文件,那么使用字符流会大 ...

  9. Java—IO流 字节流

    IO流(输入流.输出流),又分为字节流.字符流. 流是磁盘或其它外围设备中存储的数据的源点或终点. 输入流:程序从输入流读取数据源.数据源包括外界(键盘.文件.网络…),即是将数据源读入到程序的通信通 ...

随机推荐

  1. git源码阅读

    https://github.com/git-for-windows/git/issues/1854 https://github.com/git-for-windows/git/pull/1902/ ...

  2. 【第十九章】 springboot + hystrix(1)

    hystrix是微服务中用于做熔断.降级的工具. 作用:防止因为一个服务的调用失败.调用延时导致多个请求的阻塞以及多个请求的调用失败. 1.pom.xml(引入hystrix-core包) 1 < ...

  3. 【第五章】 springboot + mybatis

    springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成.集成方式相当简单. 1.项目结构 2.pom.xml <!-- 与数 ...

  4. 贪心算法-Best cow line-字典序问题

    代码: #include<cstdio> #include<iostream> #include<stdlib.h> #include<string> ...

  5. js分号的重要性

    js中语句末尾可以不加分号, 很多时候在做练习或写几个页面时,我都是不会加的.虽然知道加了会好一点.但就是觉得很敲一句就要多按一次分号键(;)来加分号,而不加也不怎么样,然后就不想加了. 也听说在对j ...

  6. tornado 如何向html传list变量啊?

    py: html:

  7. python 集合从头部删除元素

    num_set = , , , , ]) num_set.pop() print(num_set) num_set.pop() print(num_set)

  8. spring boot: freemarket模板引擎

    spring boot: freemarket模板引擎 freemarket模板引擎,可以和thymeleaf模板引擎共存 pom.xml引入 <!-- Freemarket --> &l ...

  9. Linux下搜索文件

    使用linux系统难免会忘记文件所在的位置,可以使用以下命令对系统中的文件进行搜索.搜索文件的命令为"find":"locate":"whereis& ...

  10. 12月15日 session:Ruby on Rails Security Guide//从第3节开始没有学习//关于find_by 和where的区别用法思考。

    http://guides.rubyonrails.org/security.html#user-management 2.session笔记见13日的随笔. http://www.cnblogs.c ...