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. cygwin下使用apt-cyg安装新软件

    1.获取  (记得先安装好git) git clone https://github.com/transcode-open/apt-cyg.git 2.安装apt-cyg cd apt-cyg chm ...

  2. mysql-5.7.20-winx64.zip Zip版、解压版MySQL安装

    1.  zip下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-winx64.zip 2.官方文档位置: http:// ...

  3. SxsTrace

    https://troubleshooter.xyz/wiki/fix-the-application-has-failed-to-start-because-the-side-by-side-con ...

  4. Luogu 2671 求和 NOIP2015T3

    题目链接 题解 20pts $O(n^3)$枚举$x,y,z$,根据题目要求判断 40pts $O(n^2)$枚举$x,z$,需要满足$x,z$奇偶相同 20~40pts的代码我都没有写过...就不贴 ...

  5. 最常用的15大Eclipse开发快捷键技巧【转】

    引言 做java开发的,经常会用Eclipse或者MyEclise集成开发环境,一些实用的Eclipse快捷键和使用技巧,可以在平常开发中节约出很多时间提高工作效率,下面我就结合自己开发中的使用和大家 ...

  6. Visual Studio 项目模板制作(一)

    我们编写项目的时候,很多时候都是在写重复代码,比如一个比较完整的框架,然后下面有很多代码都是重复的Copy,其实我们可以利用Visual Studio的模板替我们干这些活,我们只要关注项目具体的业务就 ...

  7. hive学习4(hive的脚本执行)

    hive的脚本执行 hive -e "SQL" hvie -f file 实例 [root@spark1 ~]# hive -e "show tables" # ...

  8. UVa 1343 旋转游戏(dfs+IDA*)

    https://vjudge.net/problem/UVA-1343 题意:如图所示,一共有8个1,8个2和8个3,如何以最少的移动来使得中间8个格子都为同一个数. 思路:状态空间搜索问题. 用ID ...

  9. 【Python】【内置函数】【bytes&bytearray&str&array】

    [bytes] 英文文档: class bytes([source[, encoding[, errors]]]) Return a new “bytes” object, which is an i ...

  10. WPF基础学习笔记整理 (八) 命令

    基础知识: 命令是应用程序的任务,并跟踪任务是否能够被执行. 命令不包含执行应用程序任务的代码. 命令是比事件更高级的元素.默认的命令目标是当前获得焦点的元素. 良好的Win应用程序,应用程序逻辑不应 ...