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. IIS Logs

    日志路径 %SystemDrive%\inetpub\logs\LogFiles https://stackify.com/where-are-iis-log-files-located/ Where ...

  2. java中new一个对象和对象=null有什么区别

    原创:转载请注明出处 今天在写代码时,遇到一个问题,特此进行记录. for (ProfileDto profileDto : profile)            { // Profile resP ...

  3. WPF基础学习笔记整理 (一)

    基础知识: WPF:Windows Presentation Foundation,用于Windows的现代图形显示系统: WPF用于编写应用程序的表示层: 引入“内置硬件加速”和“分辨率无关”: S ...

  4. [原][osg][osgEarth]关于在OE中使用物理引擎的调研

    关于物理引擎旋转的一些整理 参考文档 http://blog.wolfire.com/2010/03/Comparing-ODE-and-Bullet 介绍ODE和bullet的利弊 http://s ...

  5. 《剑指offer》第十题(斐波那契数列)

    // 面试题:斐波那契数列 // 题目:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项. #include <iostream> using namespace std; ...

  6. java日期获取前一天和后一天

    import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import j ...

  7. JPA、SpringData JPA 、Hibernate和Mybatis 的区别和联系

    一.JPA 概述 1. Java Persistence API(Java 持久层 API):用于对象持久化的 API 2. 作用:使得应用程序以统一的方式访问持久层 3. 前言中提到了 Hibern ...

  8. ViewPager 如何得到当前的Fragment (使用FragmentPagerAdapter)

    使用FragmentPagerAdapter时,难免要在MainActivity 和 当前显示的Fragment间传递数据.但是FragmentPagerAdapter并没有给我们提供类似getCur ...

  9. English trip -- Phonics 6 元音字母 u + Unit 5 B课 review

    Vowel  u [ʌ]  闭音节 bunny cut bug mushroom lunch ar er ur or ir = R (读音类似儿) e.g. dollar  美元 collar  n. ...

  10. 第7章使用请求测试-测试API . Rspec: everyday-rspec实操。

    测试应用与非人类用户的交互,涵盖外部 API 7.1request test  vs feature test 对 RSpec 来说,这种专门针 对 API 的测试最好放在 spec/requests ...