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. MySQL slave_exec_mode 参数说明

    背景: 今天无意当中看到参数slave_exec_mode,从手册里的说明看出该参数和MySQL复制相关,是可以动态修改的变量,默认是STRICT模式(严格模式),可选值有IDEMPOTENT模式(幂 ...

  2. Coursera课程 Programming Languages, Part A 总结

    Coursera CSE341: Programming Languages 感谢华盛顿大学 Dan Grossman 老师 以及 Coursera . 碎言碎语 这只是 Programming La ...

  3. WebLogic部署报java.lang.ClassCastException: weblogic.xml.jaxp.RegistrySAXParserFactory cannot be cast to javax.xml.parsers.SAXParserFactory

    今天在部署WebLogic项目时,报了java.lang.ClassCastException: weblogic.xml.jaxp.RegistrySAXParserFactory cannot b ...

  4. iOS 断点上传文件

    项目开发中,有时候我们需要将本地的文件上传到服务器,简单的几张图片还好,但是针对iPhone里面的视频文件进行上传,为了用户体验,我们有必要实现断点上传.其实也不是真的断点,这里我们只是模仿断点机制. ...

  5. 使用hiredis实现pipeline方式访问

    1.介绍 hiredis: 一个c/c++的访问redis的api库 地址:https://github.com/redis/hiredis pipeline方式: redis中的pipeline方式 ...

  6. 》》3D轮播

    * { margin: 0; padding: 0; } .slide { position: absolute; top: calc(50% - 150px); left: calc(50% - 3 ...

  7. 【LDA】修正 GibbsLDA++-0.2 中的两个内存问题

    周末这两天在家用LDA做个小实验. 在LDA的众多实现的工具包中.GibbsLDA 是应用最广泛的.包含c++版本号.java版本号等.GibbsLDA++ 是它的C++版本号的实现.眼下最新版本号是 ...

  8. Unable to resolve persistence unit root URL

    异常信息 时间:2017-03-07 11:46:05,516 - 级别:[ WARN] - 消息: [other] The web application [ROOT] appears to hav ...

  9. Nginx各种配置

    日志 日志中属性 $remote_addr,$http_x_forwarded_for 记录客户端IP地址 $remote_user 记录客户端用户名称 $request 记录请求的URL和HTTP协 ...

  10. 百度云BCC配置Apache VirtualHost 实现相同域名不同端口访问不同应用

    问题描述:前戏:本人在百度云上购买了BCC虚拟服务,并购买域名,部署应用,可以正常访问(这里一切都很正常^_^). 事情正在起变化:随着开发的不断推进,工程在本地测试成功后,部署到服务器,会发现有些页 ...