IO流之字节流知识总结
IO流分为字符流和字节流。
字节流;可以读取任何文件,电脑以字节的方式储存
字符流:用来读取字符。
下面是我总结的思维导图。

相关练习代码
public class Demo {
@Test
public void fun() throws IOException {
FileInputStream fis = new FileInputStream("zzz.txt");
/* int read = fis.read();//从文件中读取一个字节
System.out.println(read);
int read2 = fis.read();//读取下一个
System.out.println(read2);
int read3 = fis.read();//读取下一个
System.out.println(read3);//结果是-1说明文件的结束标记是-1*/
FileOutputStream fos = new FileOutputStream("xxx.txt");//如果没有文件,会自动创建
int b;
while ((b = fis.read()) != -1) {
fos.write(b);//将字节流写入文件
System.out.println(b);//通过循环读取
}
fis.close();//关闭流
fos.close();
}
//文件的追加
@Test
public void fun2() throws IOException {
FileOutputStream fos = new FileOutputStream("zzz.txt", true);//文件的追加
fos.write(98);
fos.write(99);
fos.close();
}
//文件的拷贝一个字节一个字节的拷贝
@Test
public void fun3() throws IOException {
FileInputStream fis = new FileInputStream("592.jpg");//创建输入流
FileOutputStream fos = new FileOutputStream("make/make/m/2.jpg", true);//输出流
int b;
while ((b = fis.read()) != -1) {
fos.write(b);
}
fis.close();
fos.close();
}
//利用字节数组拷贝
//使用的是available方法获取文件长度
//大文件都读取到自己数组,内存溢出
@Test
public void fun4() throws IOException {
FileInputStream fis = new FileInputStream("592.jpg");
FileOutputStream fos = new FileOutputStream("make/make/m/copy.jpg");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fos.write(bytes);
fis.close();
fos.close();
}
//标准小数组文件读取方式
@Test
public void fun5() throws IOException {
FileInputStream fis = new FileInputStream("592.jpg");//输入流
FileOutputStream fos = new FileOutputStream("make/make/m/copy2.jpg");//输出流
byte[] bytes = new byte[1024 * 8];//小数组
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fis.close();
fos.close();
}
//缓冲区文件读写
@Test
public void fun6() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("592.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("make/make/m/copy3.jpg"));
int len;
while ((len = bis.read()) != -1) {
System.out.println(len);
bos.write(len);
}
bis.close();
bos.close();
}
//close方法包含刷新功能,关闭之前先将缓冲区写到文件中
@Test
public void fun7() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("592.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("make/make/m/copy4.jpg"));
int len;
while ((len = bis.read()) != -1) {
bos.write(len);
bos.flush();//刷新功能
}
bis.close();
bos.close();
}
//字节流读写中文
@Test
public void fun8() throws IOException {
FileOutputStream fos = new FileOutputStream("zzz.txt");
fos.write("你好我是胡少君".getBytes());
fos.close();
}
//标准的异常处理1.6及以前版本
@Test
public void fun9() throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("xxx.txt");
fos = new FileOutputStream("zzz.txt");
byte[] bytes = new byte[1024 * 8];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
} finally {
try {
if (fis != null)
fis.close();
} finally {
if (fos != null)
fos.close();
}
}
}
//1.7及以上版本
@Test
public void fun10() throws IOException {
try (
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("zzz.txt");
) {
byte[] bytes = new byte[1024 * 8];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
}
}
@Test
public void fun11() throws IOException{
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("make/make/m/jm.jpg"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("make/make/m/encode.jpg"));
int len;
while ((len=bis.read())!=-1){
bos.write(len^123);//异或两次为本身
}
bis.close();
bos.close();
}
}
IO流之字节流知识总结的更多相关文章
- java io流(字节流)复制文件
java io流(字节流) 复制文件 //复制文件 //使用字节流 //复制文本文件用字符流,复制其它格式文件用字节流 import java.io.*; public class Index{ pu ...
- IO流(字节流复制)01
package ioDemo; import java.io.*; /** * IO流(字节流复制) * Created by lcj on 2017/11/2. */ public class bu ...
- JavaSE学习笔记(14)---File类和IO流(字节流和字符流)
JavaSE学习笔记(14)---File类和IO流(字节流和字符流) File类 概述 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 构造方 ...
- JavaSE(十二)之IO流的字节流(一)
前面我们学习的了多线程,今天开始要学习IO流了,java中IO流的知识非常重要.但是其实并不难,因为他们都有固定的套路. 一.流的概念 流是个抽象的概念,是对输入输出设备的抽象,Java程序中 ...
- IO流(字节流,字符流)
一,概述 IO流(input output):用来处理设备之间的数据. Java对数据的操作是通过流的对象. Java用于操作流的对象都在IO包中. 流是一组有顺序的,有起点和终点的字节集合,是对数据 ...
- 【JAVA IO流之字节流】
字节流部分和字符流部分的体系架构很相似,有四个基本流:InputStream.OutputStream.BufferedInputStream.BufferedOutputStream,其中,Inpu ...
- Java笔记(二十六)……IO流上 字节流与字符流
概述 IO流用来处理设备之间的数据传输 Java对数据的操作时通过流的方式 Java用于操作流的对象都在IO包中 流按操作的数据分为:字节流和字符流 流按流向不同分为:输入流和输出流 IO流常用基类 ...
- 【Java IO流】字节流和字符流详解
字节流和字符流 对于文件必然有读和写的操作,读和写就对应了输入和输出流,流又分成字节和字符流. 1.从对文件的操作来讲,有读和写的操作——也就是输入和输出. 2.从流的流向来讲,有输入和输出之分. 3 ...
- Java基础:IO流之字节流和字符流
1. 流的概念 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部文件等. 一个流,必有源端和目的端 ...
随机推荐
- PDFBox创建并打印PDF文件, 以及缩放问题的处理.
PDFBox带了一些很方便的API, 可以直接创建 读取 编辑 打印PDF文件. 创建PDF文件 public static byte[] createHelloPDF() { ByteArrayOu ...
- maven项目部署对Oracle jar包的处理
1.正常情况下,我们是访问不到ojdbc.jar的,需要建立一个本地仓. 2.先找到自己的Oracle中ojdbc.jar将其放入到 C:\Users\Administrator 这个目录下,然 ...
- java学习笔记之字符流文件复制
字符文件复制 FileReader fr =new FileReader("b.txt");//绑定源文件 FileWriter fw= new FileWriter(" ...
- Linux分区的注意事项以及远程连接排错
分区方式一般有三种 第一种:数据不是很重要 /boot(系统的引导分区): 系统引导的信息/软件 系统的内核 200M swap( 交换分区): 为了避免系统内存用光了导致系统 宕机 如果系统内存 ...
- ettercap的中间人欺骗
环境准备:kali系统 因为kali系统自带ettercap,比较方便, 不需要安装 ifcofing命令查看当前网关 ,当前的IP是: 172.16.42.1 查找局域网所有主机 通过netdisc ...
- Web离线应用解决方案——ServiceWorker
什么是ServiceWorker 在介绍ServiceWorker之前,我们先来谈谈PWA.PWA (Progressive Web Apps) 是一种 Web App 新模型,并不是具体指某一种前沿 ...
- 大白话Vue源码系列(03):生成AST
阅读目录 AST 节点定义 标签的正则匹配 解析用到的工具方法 解析开始标签 解析结束标签 解析文本 解析整块 HTML 模板 未提及的细节 本篇探讨 Vue 根据 html 模板片段构建出 AST ...
- 使用.Net Core+EF7 CodeFirst(2)
上一篇的话,说了下怎么使用EF7 实现 CodeFirst去生成数据库, 其实还有好多问题的,这次一点一点的解决吧,都挺简单,不过零零散散的,, 1.读取配置文件,获得链接字符串 2.使用数据库进行增 ...
- 20150605面试汇总--js与java的差别
javascript与java都是编程语言,不同在于代码格式不同. js基于对象,java是面向对象: java是强变量.编译前必须作出声明.js是弱变量,使用前不需做声明: JavaScript 是 ...
- 回溯法之求n个集合的幂集
幂集:有一个集合A,集合A的幂集是由集合A的全部子集所组成的集合. 集合中的每一个元素仅仅有两种状态:属于幂集的元素集或不属于幂集的元素集. 集合{1,2,3},用一棵二叉树来表示. 递归函数 voi ...