Java基础-IO流对象之字节流(Stream)
Java基础-IO流对象之字节流(Stream)
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
在前面我分享的笔记中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据。现在我们就要开始给文件中写数据,或者读取文件中的数据。什么是输入呢?我们这里的输入指的是将文件的内容加载到程序中的过程叫做输入,那上面叫做输出呢?就是将程序的内容持久化到硬盘上叫做输出。
一.字节输出流(outputStream)
java.io.OutputStream此抽象类是表示输出字节流的所有类的超类。
作用:从Java程序将内存中的数据写入到磁盘文件中。
1>.字节输出流FileOutputStream写字节
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ package cn.org.yinzhengjie.note5; import java.io.FileOutputStream;
import java.io.IOException; public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
//流对象的构造方法,可以根据路径创建文件,如果文件存在则直接清空文件内容!
FileOutputStream fos = new FileOutputStream("yinzhengjie.txt");
//往文件中写一个字节
fos.write(50);
fos.write(48);
fos.write(49);
fos.write(56);
//释放资源
fos.close();
}
}

2>.字节输出流FileOutputStream写字节数组
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ package cn.org.yinzhengjie.note5; import java.io.FileOutputStream;
import java.io.IOException; public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
//流对象的构造方法,可以根据路径创建文件,如果文件存在则直接清空文件内容!
FileOutputStream fos = new FileOutputStream("yinzhengjie.txt");
//写一个字节数组
byte[] bytes = {65,66,67,68,69,70};
fos.write(bytes);
//释放资源
fos.close();
}
}

3>.字节输出流FileOutputStream写字节数组的一部分
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ package cn.org.yinzhengjie.note5; import java.io.FileOutputStream;
import java.io.IOException; public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
//流对象的构造方法,可以根据路径创建文件,如果文件存在则直接清空文件内容!
FileOutputStream fos = new FileOutputStream("yinzhengjie.txt");
byte[] bytes = {65,66,67,68,69,70};
//写一个字节数组的一部分,需要传入数组对象,数组的开始索引,从开始索引开始计算需要写入的长度
fos.write(bytes,0,3);
//释放资源
fos.close();
}
}

4>.写入字节数组的简便方式(写入字符串)
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ package cn.org.yinzhengjie.note5; import java.io.FileOutputStream;
import java.io.IOException; public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
//流对象的构造方法,可以根据路径创建文件,如果文件存在则直接清空文件内容!
FileOutputStream fos = new FileOutputStream("yinzhengjie.txt");
//写入字符串数组
fos.write("yinzhengjie".getBytes());
fos.close();
}
}

5>.以追加的方式写入
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ package cn.org.yinzhengjie.note5; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
File file = new File("yinzhengjie.txt");
//以追加的方式写入数据,需要在FileOutputStream的构造方法中指定参数为true.
FileOutputStream fos = new FileOutputStream(file,true);
//写入换行符
fos.write("\r\n".getBytes());
fos.write("yinzhengjie\r\n".getBytes());
fos.close();
}
}

二.IO中的异常处理
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ package cn.org.yinzhengjie.note5; import java.io.FileOutputStream;
import java.io.IOException; public class FileOutputStreamDemo {
public static void main(String[] args) {
//try 外面声明变量,try里面建立对象,也就是将fos的作用域提示,这样就可以让finally作用域可以访问到。
FileOutputStream fos = null; try {
//注意,这里给的盘符如果不存在的话就会创建失败,此时fos的值就位null。
fos = new FileOutputStream("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.txt");
fos.write("yinzhengjie".getBytes()); } catch (IOException e) {
//如果硬件问题应该让用户重试,比如用户在写入数据的时候拔掉U盘。
System.out.println(e.getMessage());
throw new RuntimeException("文件写入失败,请重试!");
}finally {
try {
//在释放资源的时候需要对流对象进行判断是否为null,如果不是null。表示对象建立成功,需要关闭资源。
if(fos!=null) {
fos.close();
}
} catch (IOException e) {
throw new RuntimeException("关闭资源失败!");
}
}
}
}
三.字节输入流InputStream
java.io.InputStream此抽象类是表示输出字节流的所有类的超类。
作用:将磁盘文件文件内容加载到内存中来。
1>.按照一个字节进行读取
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ package cn.org.yinzhengjie.note5; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
File file = new File("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.txt");
FileInputStream fis = new FileInputStream(file);
int index = 0;
//依次读取一个字节,当读到文件末尾时index的值为-1,因此称为结束循环的条件。
while((index = fis.read()) != -1) {
System.out.print((char)index);
}
fis.close();
}
} /*
以上代码执行结果如下:
yinzhengjie
Java
2018
Bg Date
golang
Python
Linux
shell
*/
2>.按照一个字节数组进行读取数据
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ package cn.org.yinzhengjie.note5; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
File file = new File("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.txt");
FileInputStream fis = new FileInputStream(file);
//创建字节数组
byte[] buf = new byte[4096];
int index ;
//依次读取一个字节,当读到文件末尾时index的值为-1,因此称为结束循环的条件。
while((index = fis.read(buf)) != -1) {
//调用字符串的构造方法,将读取的数据转换成字符串
System.out.print(new String(buf,0,index));
}
fis.close();
}
} /*
以上代码执行结果如下:
yinzhengjie
Java
2018
Bg Date
golang
Python
Linux
shell
*/
四.小试牛刀
1>.字节流复制文件读取单个字节
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ package cn.org.yinzhengjie.note5; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class FileCopyDemo {
public static void main(String[] args) throws IOException { FileInputStream fis = null;
FileOutputStream fos = null; try {
File src = new File("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.txt");
File dest = new File("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.backup");
//建立两个流的对象,绑定源文件和目标文件
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
//字节输入流,读取一个字节,输出流写一个字节
int index ;
while((index = fis.read()) != -1) {
fos.write(index);
}
}catch(IOException e) {
System.out.println(e.getMessage());
throw new RuntimeException("文件复制失败");
}finally {
try {
if(fos != null) {
fos.close();
}
}catch(IOException e) {
throw new RuntimeException("是否资源失败");
}finally {
try {
if(fis != null ) {
fis.close();
}
}catch(IOException e) {
throw new RuntimeException("释放资源失败");
}
}
}
}
}

2>.字节流复制文件读取字节数组
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/ package cn.org.yinzhengjie.note5; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class FileCopyDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null; try {
File src = new File("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.txt");
File dest = new File("D:\\10.Java\\JavaSE\\eclipse\\Myprogram\\workspace\\Day8\\yinzhengjie.backup2");
//建立两个流的对象,绑定源文件和目标文件
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
//定义字节数组,缓冲数据
byte[] buf = new byte[4096];
//读取数组,写入数组
int index;
while((index = fis.read(buf)) != -1) {
fos.write(buf,0,index);
}
}catch(IOException e) {
System.out.println(e.getMessage());
throw new RuntimeException("文件复制失败");
}finally {
try {
if(fos != null) {
fos.close();
}
}catch(IOException e) {
throw new RuntimeException("是否资源失败");
}finally {
try {
if(fis != null ) {
fis.close();
}
}catch(IOException e) {
throw new RuntimeException("释放资源失败");
}
}
}
}
}

Java基础-IO流对象之字节流(Stream)的更多相关文章
- Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream)
Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在我们学习字 ...
- Java基础-IO流对象之转换流(InputStreamReader与OutoutStreamWriter)
Java基础-IO流对象之转换流(InputStreamReader与OutoutStreamWriter) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.转换流概述 我们之前 ...
- Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)
Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...
- Java基础-IO流对象之随机访问文件(RandomAccessFile)
Java基础-IO流对象之随机访问文件(RandomAccessFile) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.RandomAccessFile简介 此类的实例支持对 ...
- Java基础-IO流对象之内存操作流(ByteArrayOutputStream与ByteArrayInputStream)
Java基础-IO流对象之内存操作流(ByteArrayOutputStream与ByteArrayInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.内存 ...
- Java基础-IO流对象之数据流(DataOutputStream与DataInputStream)
Java基础-IO流对象之数据流(DataOutputStream与DataInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据流特点 操作基本数据类型 ...
- Java基础-IO流对象之打印流(PrintStream与PrintWriter)
Java基础-IO流对象之打印流(PrintStream与PrintWriter) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.打印流的特性 打印对象有两个,即字节打印流(P ...
- Java基础-IO流对象之序列化(ObjectOutputStream)与反序列化(ObjectInputStream)
Java基础-IO流对象之序列化(ObjectOutputStream)与反序列化(ObjectInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.对象的序 ...
- java基础-IO流对象之Properties集合
java基础-IO流对象之Properties集合 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Properties集合的特点 Properties类表示了一个持久的属性集. ...
随机推荐
- NEWBEE软件团队 人员分配情况及分数获得方式
人员分配: PM:李桐 王骜 dev:王骜 刘垚鹏 安康 林旭鹏 黄新越 test:黄伟龙 李桐 马佐霖 黄新越 注:黄新越为女生,不方便平时的交流,所以任务分配较为灵活,特分在两个组里. 评分 ...
- OO第四阶段总结
一.测试与正确性论证的区别 从哲学的角度来说,正确性论证与测试的关系就像理论与实践的关系一样. 使用测试的方法检验程序正确性确实是一个非常方便可行且广泛运用的方法.可以通过几个简单或复杂的测试样例,迅 ...
- 软工1816 · Beta冲刺(3/7)
团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 协助后端完成历史记录接口.美食排行榜接口 完成食堂平面图的绘制 确定web端业 ...
- Matlab图像匹配问题
已知一个任意形状,查找在大图像中最接近的形状位置. 输入:一个小图形状和一张大图 输出:最接近的形状在大图中的位置 假设: (1)已知形状与目标形状有一定的形变. (2)形状与大图像均为二值图像,图中 ...
- React鼠标事件
说明:假设有一个用户名片,当鼠标滑到上面,显示用户详细信息,且用户详情卡片位置随鼠标位置改变而改变. UI框架:Material-ui 实现思路: 1.一个用户简介组件A(用于展示用户列表): 2.一 ...
- Android如何避免OOM总结
转自:https://blog.csdn.net/ljx19900116/article/details/50037627 前面介绍了一些基础的内存管理机制以及OOM的基础知识,那么在实践操作当中,有 ...
- centos升级内核(rpm方式)
#rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 如果失败的话多试几次,感觉网络不是很好#rpm -ivh http://www. ...
- mysql 添加行号
首先准备演示数 DROP TABLE IF EXISTS `computer_stu`; CREATE TABLE `computer_stu` ( `id` ) NOT NULL, `name` ) ...
- 【Python】python 2 map() reduce()
利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字.输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']. ...
- 5W2H方法
5W2H分析方法也叫七问分析法,是二战中美国陆军兵器修理部首创.简单.方便.易于理解.使用,富有启发意义,被广泛应用于企业管理和技术活动,对于决策和执行性的措施也非常有帮助,有助于弥补考虑问题的疏漏 ...