#字节流

字节输出流FileOutputStream

创建输出流对象

OutputStream 流对象是一个抽象类,不能实例化。所以,我们要找一个具体的子类 :FileOutputStream。 查看FileOutputStream的构造方法:

FileOutputStream(File file)
FileOutputStream(String name)

创建字节输出流对象了做了几件事情:

  1. 调用系统功能去创建文件

  2. 创建字节输出流对象

  3. 把该字节输出流对象引用指向这个文件

public class OutputStreamDemo {
public static void main(String[] args) throws IOException {
OutputStream fos = new FileOutputStream("demo1.txt");
/*
* 创建字节输出流对象了做了几件事情:
* A:调用系统功能去创建文件
* B:创建fos对象
* C:把fos对象指向这个文件
*/ //写数据
fos.write("hello,IO".getBytes());
fos.write("java".getBytes()); //释放资源
//关闭此文件输出流并释放与此流有关的所有系统资源。
fos.close();
/*
* 为什么一定要close()呢?
* A:让流对象变成垃圾,这样就可以被垃圾回收器回收了
* B:通知系统去释放跟该文件相关的资源
*/
//java.io.IOException: Stream Closed
//fos.write("java".getBytes());
}
}
  • 为什么一定要close()呢?
  1. 让流对象变成垃圾,这样就可以被垃圾回收器回收了

  2. 通知系统去释放跟该文件相关的资源

写出数据

字节输出流操作步骤:

  1. 创建字节输出流对象

  2. 写数据

  3. 释放资源

FileOutputStream中的写出方法:

public void write(int b) //写一个字节
public void write(byte[] b) //写一个字节数组
public void write(byte[] b,int off,int len) //写一个字节数组的一部分
public class OutputStreamDemo2 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("demo2.txt"); // 调用write()方法
fos.write(97); //97 -- 底层二进制数据 -- 通过记事本打开 -- 找97对应的字符值 -- a //public void write(byte[] b):写一个字节数组
byte[] bys={98,99,100,101};
fos.write(bys); //public void write(byte[] b,int off,int len):写一个字节数组的一部分
fos.write(bys,1,3); //释放资源
fos.close();
}
}

如何实现数据的换行?不同的系统针对不同的换行符号识别是不一样的?

windows:\r\n
linux:\n
Mac:\r
public class OutputStreamDemo3 {
public static void main(String[] args) throws IOException {
// 创建一个向具有指定 name 的文件中写入数据的输出文件流。
// 如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
FileOutputStream fos = new FileOutputStream("demo2.txt",true); // 写数据
for (int x = 0; x < 10; x++) {
fos.write(("hello" + x).getBytes());
fos.write("\r\n".getBytes());
} //释放资源
fos.close();
}
}

加入异常处理的字节输出流操作

public class OutputStreamDemo4 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("demo1.txt");
fos.write("java".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 如果fos不是null,才需要close()
if (fos != null) {
// 为了保证close()一定会执行,就放到这里了
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

字节输入流FileInputStream

  • 字节输入流操作步骤:
  1. 创建字节输入流对象

  2. 调用read()方法读取数据,并把数据显示在控制台

  3. 释放资源

  • 读取数据的方式:
int read() //一次读取一个字节
int read(byte[] b) //一次读取一个字节数组
  • 一次读取一个字节
public class InputStreamDemo {
public static void main(String[] args) throws IOException {
InputStream fis=new FileInputStream("demo1.txt");
int by = 0;
// 读取,赋值,判断
while ((by = fis.read()) != -1) {
System.out.print((char) by);
} // 释放资源
fis.close();
}
}
一次读取一个字节数组
public class InputStreamDemo2 {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("demo1.txt");
//数组的长度一般是1024或者1024的整数倍
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
} // 释放资源
fis.close();
}
}

带缓冲区的字节流BufferedOutputStream

public class BufferedOutputStreamDemo {
public static void main(String[] args) throws IOException {
BufferedOutputStream bos=new
BufferedOutputStream(new FileOutputStream("demo1.txt"));
bos.write("hello".getBytes());
bos.close();
}
}

带缓冲区的字节流BufferedInputStream

public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
BufferedInputStream bis=new
BufferedInputStream(new FileInputStream("demo1.txt"));
int by=-1;
while((by=bis.read())!=-1){
System.out.print((char)by);
}
bis.close();
}
}

复制文件

public class CopyFile {
public static void main(String[] args) throws IOException {
String strFile="国旗歌.mp4";
String destFile="demo3.mp4";
long start = System.currentTimeMillis();
//copyFile(strFile,destFile); //共耗时:75309毫秒
//copyFile2(strFile,destFile); //共耗时:153毫秒
//copyFile3(strFile,destFile);//共耗时:282毫秒
copyFile4(strFile,destFile);//共耗时:44毫秒
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
} /**
* 基本字节流一次读写一个字节
*/
public static void copyFile(String srcFile,String destFile) throws IOException {
FileInputStream fis=new FileInputStream(srcFile);
FileOutputStream fos=new FileOutputStream(destFile); int by=0;
while((by=fis.read())!=-1){
fos.write(by);
} fis.close();
fos.close();
} /**
* 基本字节流一次读写一个字节数组
*/
public static void copyFile2(String srcFile,String destFile) throws IOException{
FileInputStream fis=new FileInputStream(srcFile);
FileOutputStream fos=new FileOutputStream(destFile); int len=0;
byte[] bys=new byte[1024];
while((len=fis.read(bys))!=-1){
fos.write(bys,0,len);
} fis.close();
fos.close();
} /**
* 高效字节流一次读写一个字节
*/
public static void copyFile3(String srcFile,String destFile) throws IOException{
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile)); int by=0;
while((by=bis.read())!=-1){
bos.write(by);
} bis.close();
bos.close();
} /**
* 高效字节流一次读写一个字节数组
*/
public static void copyFile4(String srcFile,String destFile) throws IOException{
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile)); int len=0;
byte[] bys=new byte[1024];
while((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
} bis.close();
bos.close();
}
}

装饰者模式

Java I/O 使用了装饰者模式来实现。以 InputStream 为例,

  • InputStream 是抽象组件;
  • FileInputStream 是 InputStream 的子类,属于具体组件,提供了字节流的输入操作;
  • FilterInputStream 属于抽象装饰者,装饰者用于装饰组件,为组件提供额外的功能。例如 BufferedInputStream 为 FileInputStream 提供缓存的功能。

实例化一个具有缓存功能的字节流对象时,只需要在 FileInputStream 对象上再套一层 BufferedInputStream 对象即可。

FileInputStream fileInputStream = new FileInputStream(filePath);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

DataInputStream 装饰者提供了对更多数据类型进行输入的操作,比如 int、double 等基本类型。

免费Java高级资料需要自己领取,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo高并发分布式等教程,一共30G。
传送门:https://mp.weixin.qq.com/s/JzddfH-7yNudmkjT0IRL8Q

字节输出流FileOutputStream的更多相关文章

  1. 字节输出流 FileOutputStream

    输入和输出 : 参照物 都是java程序来参照 output 内存---->硬盘 input 持久化数据-->内存 字节流输出 定义:流按照方向可以分为输入和输出流 字节流 :可以操作任何 ...

  2. 一切皆为字节和字节输出流_OutputStream类&FileOutputStream类介绍

    一切皆为字节 一切文件数据(文本.图片.视频等)在存储时,都是以二进制数字的形式保存,都一个一个的字节,那么传输时一样如此.所以,字节流可以传输任意文件数据.在操作流的时候,我们要时刻明确,无论使用什 ...

  3. java 20 - 9 带有缓冲区的字节输出流和字节输入流

    由之前字节输入的两个方式,我们可以发现,通过定义数组读取数组的方式比一个个字节读取的方式快得多. 所以,java就专门提供了带有缓冲区的字节类: 缓冲区类(高效类) 写数据:BufferedOutpu ...

  4. java 20 - 6 加入了异常处理的字节输出流的操作

    昨天坐了十几个钟的车回家,累弊了.... ————————————割掉疲劳————————————— 前面的字节输出流都是抛出了异常不管,这次的加入了异常处理: 首先还是创建一个字节输出流对象,先给它 ...

  5. java 20 - 5 字节输出流写出数据的一些方法

    首先回顾下 字节输出流操作步骤:  A:创建字节输出流对象  B:调用write()方法  C:释放资源 创建字节流输出对象 FileOutputStream fos = new FileOutput ...

  6. Java输出流FileOutputStream使用详解

    Java输出流FileOutputStream使用详解 http://baijiahao.baidu.com/s?id=1600984799323133994&wfr=spider&f ...

  7. FileOutputSream文件字节输出流

    1.FileOutputSream文件字节输出流:  输入--写出--使用:  输出--写入--存储: 写出写入是对硬盘而言: 其中,OutputStream为所有类型的字节输出流的超类: FileO ...

  8. 021.2 IO流——字节输出流

    内容:流的分类,文件写入(字节输出流),异常处理,获取一个文件夹下的特定文件集合 字节流的抽象基类:InputStream,OutputStream字符流的抽象基类:Reader,Writer由这四个 ...

  9. java IO流 之 字节输出流 OutputString()

    Java学习重点之一:OutputStream 字节输出流的使用 FileOutPutStream:子类,写出数据的通道 步骤: 1.获取目标文件 2.创建通道(如果原来没有目标文件,则会自动创建一个 ...

随机推荐

  1. kolla部署openstack allinone,报错APIError: 500 Server Error: Internal Server Error (\"oci runtime error: container_linux.go:235: starting container process caused \"container init exited prematurely

    使用 kolla-ansible 部署 opnenstack:stein 执行 kolla-ansible -i ./all-in-one deploy 开始自动化部署 在部署过程中报错,报错信息如下 ...

  2. Shel脚本-初步入门之《03》

    Shel脚本-初步入门-Shell 脚本在 Linux 运维工作中的地位 3.Shell 脚本在 Linux 运维工作中的地位 Shell 脚本语言很适合用于处理纯文本类型的数据,而 Linux 系统 ...

  3. BeanShell实现加密解密功能

    一,在IDEA中写好加密的脚本 二,然后将整个包文件导出,生成jar包 三,将jar包文件放到jmeter的lib/ext目录下 然后在jmeter的BeanShell中引入该类,调用其中的加密方法 ...

  4. USACO Telephone Lines

    洛谷 P1948 [USACO08JAN]电话线Telephone Lines https://www.luogu.org/problem/P1948 JDOJ 2556: USACO 2008 Ja ...

  5. LG4035/BZOJ1013 「JSOI2008」球形空间产生器 高斯消元

    问题描述 LG4035 BZOJ1013 题解 设答案为\((p_1,p_2,p_3,...,p_n)\) 因为是一个球体,令其半径为\(r\),则有 \[\sum_{i=1}^{n}{(a_i-p_ ...

  6. myeclipse开发工具的简单使用

    一.使用eclipse.myeclipse开发JAVA程序 将程序开发环境和调试环境集合在一起,提高开发效率 1.创建java项目2.创建程序包3.编写JAVA源程序4.运行JAVA程序 二.程序移植 ...

  7. ESA2GJK1DH1K升级篇: 快速的移植升级程序到自己的项目(BootLoader程序制作)

    前言 此代码兼容STM32F103全系列 为避免添加上升级程序造成内存不足,请使用128KB Flash及其以上的型号 这篇文章是为了能够让大家快速移植我的升级模板程序到自己的项目 BootLoade ...

  8. CF1178F Short/Long Colorful Strip(DP)

    说起来,这题好像也不难-- 先考虑 F1 怎么做. 既然别的方法都不行不如试试\(f_{i,j}\) 表示在刚刚准备开始涂 \([i,j]\) 中最小编号的颜色之前,整个区间是同色的,且最后能做到 \ ...

  9. vue_02day

    目录 vue_02 表单指令: 条件指令: 循环指令: 前端数据库: 分隔符: 过滤器: 计算属性: 监听属性: vue编译不生效,闪烁 冒泡排序: vue_02 表单指令: <form act ...

  10. [LeetCode] 527. Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...