基于【字节】操作的IO接口:InputStream、OutputStream
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的更多相关文章
- 基于【磁盘】操作的IO接口:File
基本操作Api import org.apache.commons.lang3.time.DateFormatUtils; import java.io.*; import java.util.Dat ...
- IO 流(InputStream,OutputStream)
1. InputStream,OutputStream都是抽象类,所以不能创建对象. 1个中文占两个字节 package com.ic.demo01; import java.io.File; imp ...
- 基于【字符】操作的IO接口:Writer、Reader
Reader public class BufferedReaderTest { public static void main(String[] args) throws IOException { ...
- IO流的字节输入输出流(InputStream,OutputStream)
字节输出流与文件字节输出流 文件存储原理和记事本打开文件原理 OutputStream及FileOutputStream import java.io.FileOutputStream; import ...
- JAVA I/O(一)基本字节和字符IO流
最近再看I/O这一块,故作为总结记录于此.JDK1.4引入NIO后,原来的I/O方法都基于NIO进行了优化,提高了性能.I/O操作类都在java.io下,大概将近80个,大致可以分为4类: 基于字节操 ...
- Java:IO流(二)——InputStream/OutputStream具体用法:FileXXXStream、ByteArrayXXXStream
1.说明 InputStream和OutputStream是Java标准库中最基本的IO流,它们都位于java.io包中,该包提供了所有同步IO的功能. 2.模块:java.io.InputStrea ...
- 字节输入流:io包中的InputStream为所有字节输入流的父类。
字节输入流:io包中的InputStream为所有字节输入流的父类. Int read();读入一个字节(每次一个): 可先使用new byte[]=数组,调用read(byte[] b) read ...
- 字节流InputStream/OutputStream
字节流InputStream/OutputStream 本篇将对JAVA I/O流中的字节流InputStream/OutputStream做个简单的概括: 总得来说,每个字节流类都有一个对应的用途, ...
- 一个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 ...
随机推荐
- Linux:在文件最后一列添加递增数(awk,cat函数)
假设有文件file1.txt: aa eeeee bb eeeee cc eeeee dd eeeee 先修改为: aa eeeee 1 bb eeeee 2 cc eeeee3 dd eeeee ...
- RabbitMQ入门-路由-有选择的接受消息
比如一个日志系统,之前的处理方式呢,是各种类型(info,error,warning)的消息都发给订阅者,可是实际情况上不一定都需要.可能A需要error,其他的都不需要.那么就引入了今天的处理方式- ...
- (sort)P1068 分数线划定 洛谷
题目描述 世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,AA市对 所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试.面试分数线根 据计划录取人数的150\% ...
- Go 的构建模式
Go 的八种 Build Mode exe (静态编译) exe (动态链接 libc) exe (动态链接 libc 和非 Go 代码) pie 地址无关可执行文件(安全特性) c-archive ...
- consul介绍
consul 是一个支持多数据中心分布式高可用,用于服务发现和配置共享的工具. consul与其它工具的不同,官方介绍如下: https://www.consul.io/intro/vs/index. ...
- 信用评分卡 (part 6 of 7)
python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...
- 信用评分卡 (part 3of 7)
python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...
- gcc生成含有C信息的汇编
title: gcc生成含有C信息的汇编 tags: gcc date: 2018-10-24 23:40:19 --- https://www.cnblogs.com/fengkang1008/p/ ...
- Hbase 1.3.0 Rsgroup
HBase RSGroup Git环境window环境下,警用crlf自动转换git config --global core.autocrlf false protobuf环境yum install ...
- vue基础篇---vue组件《2》
定义全局组件 我们通过Vue的component方法来定义一个全局组件. <div id="app"> <!--使用定义好的全局组件--> <coun ...