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类表示了一个持久的属性集. ...
随机推荐
- Sprint7
进展:根据昨天查到的资料,今天开始编写闹钟部分的代码,主要实现了闹钟添加事件显示时间主界面.
- StringBuffer 与 StringBuilder类的使用
/*如果需要频繁修改字符串 的内容,建议使用字符串缓冲 类(StringBuffer). StringBuffer 其实就是一个存储字符 的容器. 笔试题目:使用Stringbuffer无 参的构造函 ...
- 作业3//Calculator::1
计算器 作业博客 github 1.扯淡 代码其实是在十几号时打的,花了半晚上加半个下午.但是懒得打随笔,所以到现在才完成. 我的课程里没找到queue,是百度照着瞎打的. 2.总结 不大理解要求,S ...
- 《TCP/IP 详解 卷1:协议》第 8 章:Internet 控制报文协议
路由器是 Internet 的重要组成部分,严密监视 Internet 的操作.IP 协议未给发送失败的 IP 数据包提供一种错误处理,也没有给端系统提供直接的方法来发现错误.为了解决这一不足之处,I ...
- 【CSAPP笔记】9. 汇编语言——缓冲区溢出
x86-64 Linux 内存结构 先来看看一个程序在内存中是如何组织的.Linux 为每个进程维持了一段单独的虚拟地址空间.(进程是计算机科学中很深刻.很成功的一个概念.当我们在运行一个程序时,会得 ...
- SQL Server 中几个有用的特殊函数
在SQL Server 的使用过程中,发现几个很有用,但不太常用(或细节不太清楚)的函数(存储过程): isnumeric,isdate,patindex,newid,collate,sp_execu ...
- [日常工作] SQLSERVER 数据库出问题..搜索到的有用的网页信息
Finding a table name from a page ID By: Paul Randal Posted on: September 25, 2014 1:42 am (Check o ...
- [转帖]脑残式网络编程入门(一):跟着动画来学TCP三次握手和四次挥手
脑残式网络编程入门(一):跟着动画来学TCP三次握手和四次挥手 http://www.52im.net/thread-1729-1-1.html 1.引言 网络编程中TCP协议的三次握手和 ...
- matlab for 运算的提速
[1]主要思想:matlab是按列存储,定义s(nums,1)比定义s(1,nums)要快哦 需要重复query的元素看看能不能再for之前就定义好 经典案 ...
- Caffe使用step by step:r-cnn目标检测代码
深度学习算法火起来之后,基于深度学习各种模型都如雨后春笋一般在各个领域广泛应用. 由于想把深度学习算法应用在在视频目标检测方向,得到一个较好的结果.由于视频数据的复杂性,因此使用深度学习算法在视频中的 ...