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. Python搭建Web服务器,与Ajax交互,接收处理Get和Post请求的简易结构

    用python搭建web服务器,与ajax交互,接收处理Get和Post请求:简单实用,没有用框架,适用于简单需求,更多功能可进行扩展. python有自带模块BaseHTTPServer.CGIHT ...

  2. .9-浅析webpack源码之NodeEnvironmentPlugin模块总览

    介绍Compiler的构造比较无趣,不如先过后面的,在用到compiler的时候再做讲解. 这一节主要讲这行代码: // 不管这里 compiler = new Compiler(); compile ...

  3. slurm-16.05.3任务调度系统部署与测试(1)

      1.概述2.同步节点时间3.下载并解压文件4.编译安装munge-0.5.125.配置munge6.编译安装slurm-16.05.37.配置slurm8.配置MySQL数据库环境9.启动slur ...

  4. Dell poweredge r210进BIOS改动磁盘控制器(SATA Controller)接口模式

    Dell poweredge r210进BIOS改动磁盘控制器(SATA Controller)接口模式 开机后按F2键进入BIOS设置,例如以下图: BIOS设置主界面: 使用上下键移动光标到&qu ...

  5. as 与 is

    在存储过程(PROCEDURE)和函数(FUNCTION)中没有区别:在视图(VIEW)中只能用AS:在游标(CURSOR)中只能用IS.

  6. 用泛型创建SqlServerHelper类实现增删改查(一)

    使用泛型,可以构建对数据库单表的基本增删改查. 首先有一数据库 Test_SqlServerHelper ,有2表 接下来创建项目,对数据库进行增删改查. 直接贴代码:(SqlServerHelper ...

  7. CI学习 CCNET Config 第一天

    CCNet的整体结构就是一个Xml文档,根元素就是cruisecontrol,具体的代码块如下所示: <cruisecontrol xmlns:cb="urn:ccnet.config ...

  8. spring框架整合springMVC时关于AOP无法切入的问题

    最开始springMVC的配置为: spring的配置为: 分析可知道spring的配置正确,由于在springmvc中已经扫描了@Controller相关的注解,所以就不需要再次扫描了,由于spri ...

  9. MD5加密--Java

    MD5 Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.该算法的文件号为RFC 1321(R.R ...

  10. effective java笔记之单例模式与序列化

    单例模式:"一个类有且仅有一个实例,并且自行实例化向整个系统提供." 单例模式实现方式有多种,例如懒汉模式(等用到时候再实例化),饿汉模式(类加载时就实例化)等,这里用饿汉模式方法 ...