InputStream

参考链接:对java中FileInputStream、BufferInputStream的理解

/**
 * Author:Mr.X
 * Date:2017/10/9 17:11
 * Description:
 * @read():一个一个字节的读(单字节读取)
 */
public class Demo1 {
    public static void main(String[] args) throws IOException{
        FileInputStream fis = new FileInputStream("e:/abc.txt");
        int by = 0;
        while ((by = fis.read()) != -1) {
            System.out.print((char) by);
        }
        fis.close();
    }
}

/**
 * Author:Mr.X
 * Date:2017/10/9 17:12
 * Description:
 *
 * @read(byte[] buf):先把字节存入到缓冲区字节数组中,一下读一个数组(常用)
 * @即(多字节读取)
 */
public class Demo2 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream(new File("e:/abc.txt"));
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = fis.read(buf)) != -1) {
            System.out.print(new String(buf, 0, len));
        }
        fis.close();
    }
}

因为中文占用2Byte,英文占用1Byte,所以按照一个一个字节的读取得时,中文是要出问题的!
单个字节流只能操作英文,不能操作中文,所以要一次性读取多个字节才能读取中文信息!
abc.txt编码为uft-8,如果为其他编码的话,需要使用到字符流(专门用于操作字符)设置编码,不然中文会出问题!

OutputStream

参考链接:BufferedInputStream与BufferedOutputStream用法简介

public class Demo1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("e:/b.txt");
        fos.write(65);
        fos.write(66);
        fos.write(67);
        fos.write(68);

        String str = " hello word!";
        fos.write(str.getBytes());
        fos.close();
    }
}

public class Demo2 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream(new File("e:/abc.txt"));
        FileOutputStream fos = new FileOutputStream("e:/b.txt");
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = fis.read(buf)) != -1) {
            fos.write(buf, 0, len);
        }
        fos.close();
        fis.close();
    }
}

基于【字节】操作的IO接口:InputStream、OutputStream的更多相关文章

  1. 基于【磁盘】操作的IO接口:File

    基本操作Api import org.apache.commons.lang3.time.DateFormatUtils; import java.io.*; import java.util.Dat ...

  2. IO 流(InputStream,OutputStream)

    1. InputStream,OutputStream都是抽象类,所以不能创建对象. 1个中文占两个字节 package com.ic.demo01; import java.io.File; imp ...

  3. 基于【字符】操作的IO接口:Writer、Reader

    Reader public class BufferedReaderTest { public static void main(String[] args) throws IOException { ...

  4. IO流的字节输入输出流(InputStream,OutputStream)

    字节输出流与文件字节输出流 文件存储原理和记事本打开文件原理 OutputStream及FileOutputStream import java.io.FileOutputStream; import ...

  5. JAVA I/O(一)基本字节和字符IO流

    最近再看I/O这一块,故作为总结记录于此.JDK1.4引入NIO后,原来的I/O方法都基于NIO进行了优化,提高了性能.I/O操作类都在java.io下,大概将近80个,大致可以分为4类: 基于字节操 ...

  6. Java:IO流(二)——InputStream/OutputStream具体用法:FileXXXStream、ByteArrayXXXStream

    1.说明 InputStream和OutputStream是Java标准库中最基本的IO流,它们都位于java.io包中,该包提供了所有同步IO的功能. 2.模块:java.io.InputStrea ...

  7. 字节输入流:io包中的InputStream为所有字节输入流的父类。

    字节输入流:io包中的InputStream为所有字节输入流的父类. Int read();读入一个字节(每次一个): 可先使用new  byte[]=数组,调用read(byte[] b) read ...

  8. 字节流InputStream/OutputStream

    字节流InputStream/OutputStream 本篇将对JAVA I/O流中的字节流InputStream/OutputStream做个简单的概括: 总得来说,每个字节流类都有一个对应的用途, ...

  9. 一个I/O线程可以并发处理N个客户端连接和读写操作 I/O复用模型 基于Buf操作NIO可以读取任意位置的数据 Channel中读取数据到Buffer中或将数据 Buffer 中写入到 Channel 事件驱动消息通知观察者模式

    Tomcat那些事儿 https://mp.weixin.qq.com/s?__biz=MzI3MTEwODc5Ng==&mid=2650860016&idx=2&sn=549 ...

随机推荐

  1. 原生js实现each方法

    首先我们了解一下什么是callback函数 CALLBACK,即回调函数,是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就 ...

  2. 斯坦福大学公开课机器学习: advice for applying machine learning - evaluatin a phpothesis(怎么评估学习算法得到的假设以及如何防止过拟合或欠拟合)

    怎样评价我们的学习算法得到的假设以及如何防止过拟合和欠拟合的问题. 当我们确定学习算法的参数时,我们考虑的是选择参数来使训练误差最小化.有人认为,得到一个很小的训练误差一定是一件好事.但其实,仅仅是因 ...

  3. CSS——nth-child()

    nth-child()选择器:CSS3新属性 用法:p:nth-child(2) 选择p标签的父元素 的第二个子元素,并且这个子元素必须是p才起作用 有点绕,有点无厘头,举个栗子: <!DOCT ...

  4. testng+maven一些坑

    1. [TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM & ...

  5. BSGS与exBSGS学习笔记

    \(BSGS\)用于解决这样一类问题: 求解\(A^x ≡B(modP)\)的最小\(x\),其中\(P\)为质数. 这里我们采用分块的方法,把\(x\)分解为\(i *t-b\)(其中\(t\)是分 ...

  6. 字符缓冲流BufferedWriter BufferedReader

    //字符缓冲流主要用于文本数据的高速写入 package cn.lijun.demo1; import java.io.BufferedReader; import java.io.FileNotFo ...

  7. mysql常用快速查询修改操作

    mysql常用快速查询修改操作 一.查找并修改非innodb引擎为innodb引擎 # 通用操作 mysql> select concat('alter table ',table_schema ...

  8. 《玩转Django2.0》读书笔记-Django配置信息

    <玩转Django2.0>读书笔记-Django配置信息 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 项目配置是根据实际开发需求从而对整个Web框架编写相应配置信息. ...

  9. Linux日志痕迹清除

    #coding=utf-8 import os import sys import subprocess def Clear_The_Log(host): logs = ["/var/log ...

  10. com.netflix.client.ClientException: Load balancer does not have available server for client xxxx

    版本 spring boot: 2.0.1.RELEASE spring cloud: Finchley.M9 错误 通过zuul调用eureka注册的服务,错误内容如下 Caused by: com ...