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流之字节流知识总结的更多相关文章

  1. java io流(字节流)复制文件

    java io流(字节流) 复制文件 //复制文件 //使用字节流 //复制文本文件用字符流,复制其它格式文件用字节流 import java.io.*; public class Index{ pu ...

  2. IO流(字节流复制)01

    package ioDemo; import java.io.*; /** * IO流(字节流复制) * Created by lcj on 2017/11/2. */ public class bu ...

  3. JavaSE学习笔记(14)---File类和IO流(字节流和字符流)

    JavaSE学习笔记(14)---File类和IO流(字节流和字符流) File类 概述 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. 构造方 ...

  4. JavaSE(十二)之IO流的字节流(一)

    前面我们学习的了多线程,今天开始要学习IO流了,java中IO流的知识非常重要.但是其实并不难,因为他们都有固定的套路. 一.流的概念     流是个抽象的概念,是对输入输出设备的抽象,Java程序中 ...

  5. IO流(字节流,字符流)

    一,概述 IO流(input output):用来处理设备之间的数据. Java对数据的操作是通过流的对象. Java用于操作流的对象都在IO包中. 流是一组有顺序的,有起点和终点的字节集合,是对数据 ...

  6. 【JAVA IO流之字节流】

    字节流部分和字符流部分的体系架构很相似,有四个基本流:InputStream.OutputStream.BufferedInputStream.BufferedOutputStream,其中,Inpu ...

  7. Java笔记(二十六)……IO流上 字节流与字符流

    概述 IO流用来处理设备之间的数据传输 Java对数据的操作时通过流的方式 Java用于操作流的对象都在IO包中 流按操作的数据分为:字节流和字符流 流按流向不同分为:输入流和输出流 IO流常用基类 ...

  8. 【Java IO流】字节流和字符流详解

    字节流和字符流 对于文件必然有读和写的操作,读和写就对应了输入和输出流,流又分成字节和字符流. 1.从对文件的操作来讲,有读和写的操作——也就是输入和输出. 2.从流的流向来讲,有输入和输出之分. 3 ...

  9. Java基础:IO流之字节流和字符流

    1. 流的概念 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部文件等. 一个流,必有源端和目的端 ...

随机推荐

  1. POI tools 参数化生成excel表格

    package com.eccom.neteagle.server.confsave.service.impl; import java.io.File; import java.io.FileNot ...

  2. EBS採购模块中的级联接收和级联接收事务

    EBS採购模块中的级联接收和级联接收事务 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习.请注明出处:否则请与本人联系.违者必究) 级联接收和级联接收事务 级联功能对来自于同一个供应商 ...

  3. Uber的成功绝非偶然

    拥有打造一个初创企业并将其做强做大的梦想并不是难事,困难的是怎样将该梦想变成现实.娱乐媒体行业经常将企业成功的过程进行美化,干净利落的将企业成功前所经历的艰苦时刻进行大刀阔斧的剪裁,让其刚好可以达到拍 ...

  4. 轻松上云,从容实施Office 365项目

    这个是我在MVP 社区活动的一节课程,讲述Office 365部署中一些大的挑战和解决的方法 视频URL 例如以下: http://edu.51cto.com/lesson/id-17440.html ...

  5. [Phonegap+Sencha Touch] 移动开发26 Android下的sencha touch程序,转屏时,Ext.Viewport不能触发orientationchange事件的解决的方法

    Sencha touch 2.4.2 已经解决问题了. 比方你为Ext.Viewport的orientationchange事件加入了一个监听方法: Ext.Viewport.on('orientat ...

  6. RedHat Linux AS4 DNS 配置

     RedHat Linux AS4 DNS配置   检查当前系统中安装 DNS功能组件bind情况 [root@svr01 /]# rpm -qa|grep bind* ypbind-1.17.2 ...

  7. 在Intellij里使用Erlang依赖库

    这里以protobuffs为例,记录一下环境的配置,发现这种东西中文的资料真的不多,无论是分享还是记录都是很好的 1.创建一个文件夹名use_proto, 配置rebar.config文件如下: {d ...

  8. Java高级开发工程师面试——多线程

    来自:Sanesee 链接:http://www.sanesee.com/article/java-engineer-interview-of-thread 1.进程和线程的区别? 进程是一个具有独立 ...

  9. Python 项目实践一(外星人入侵)第一篇

    python断断续续的学了一段实践,基础课程终于看完了,现在跟着做三个小项目,第一个是外星人入侵的小游戏: 一 Pygame pygame 是一组功能强大而有趣的模块,可用于管理图形,动画乃至声音,让 ...

  10. ASP.NET Core WebApi 返回统一格式参数

    业务场景: 业务需求要求,需要对 WebApi 接口服务统一返回参数,也就是把实际的结果用一定的格式包裹起来,比如下面格式: { "response":{ "code&q ...