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类表示了一个持久的属性集. ...
随机推荐
- java第二次实验报告20135231
Java实验报告二:Java面向对象程序设计 20135231 何佳 实验要求: 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉 ...
- spring冲刺计划
会议召开时间表 日期 时间 内容 05/09 21:00-22:00 讨论题目(未果) 05/10 21:00-21:30 确定题目(网络助手) 05/13 21:00-21:45 讨论软件页面设计 ...
- struts2 Action生命周期
Struts2.0中的对象既然都是线程安全的,都不是单例模式,那么它究竟何时创建,何时销毁呢? 这个和struts2.0中的配置有关,我们来看struts.properties ### if spec ...
- python实现树莓派开机自动发送IP到指定邮箱
#!/usr/bin/python # -*- coding:UTF-8 -*- #测试发送邮件163邮箱发送到qq邮箱 import smtplib from email.mime.text imp ...
- 结对作业(1.0版)(bug1已修复)
import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing ...
- BZOJ 3132(上帝造题的七分钟-树状数组求和+2D逆求和数组)
3132: 上帝造题的七分钟 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 46 Solved: 18[Submit][Status][Discus ...
- 更新ubuntu的源
什么是Ubuntu的软件源? 我们在使用Debian或者Ubuntu的apt-get工具来安装需要的软件时,其实就是从服务器获取需要安装的软件并把它安装在本地计算机的过程.所谓的软件源,就是我们获取软 ...
- 通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)
步骤: 1.运行--〉cmd:打开cmd命令框 2.在命令行里定位到InstallUtil.exe所在的位置 InstallUtil.exe 默认的安装位置是在C:/Windows/Microsoft ...
- 微信小程序组件 自定义多选
<view class='back'></view> <view class="container"> <!-- 睡眠记录 --> ...
- web.config文件详解[转]
一).Web.Config是以XML文件规范存储,配置文件分为以下格式1.配置节处理程序声明特点: 位于配置文件的顶部,包含在<configSections>标志中.2.特定应用程序配置特 ...