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)的更多相关文章

  1. Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream)

    Java基础-IO流对象之字节缓冲流(BufferedOutputStream与BufferedInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在我们学习字 ...

  2. Java基础-IO流对象之转换流(InputStreamReader与OutoutStreamWriter)

    Java基础-IO流对象之转换流(InputStreamReader与OutoutStreamWriter) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.转换流概述 我们之前 ...

  3. Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)

    Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...

  4. Java基础-IO流对象之随机访问文件(RandomAccessFile)

    Java基础-IO流对象之随机访问文件(RandomAccessFile) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.RandomAccessFile简介 此类的实例支持对 ...

  5. Java基础-IO流对象之内存操作流(ByteArrayOutputStream与ByteArrayInputStream)

    Java基础-IO流对象之内存操作流(ByteArrayOutputStream与ByteArrayInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.内存 ...

  6. Java基础-IO流对象之数据流(DataOutputStream与DataInputStream)

    Java基础-IO流对象之数据流(DataOutputStream与DataInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据流特点 操作基本数据类型 ...

  7. Java基础-IO流对象之打印流(PrintStream与PrintWriter)

    Java基础-IO流对象之打印流(PrintStream与PrintWriter) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.打印流的特性 打印对象有两个,即字节打印流(P ...

  8. Java基础-IO流对象之序列化(ObjectOutputStream)与反序列化(ObjectInputStream)

    Java基础-IO流对象之序列化(ObjectOutputStream)与反序列化(ObjectInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.对象的序 ...

  9. java基础-IO流对象之Properties集合

    java基础-IO流对象之Properties集合 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Properties集合的特点 Properties类表示了一个持久的属性集. ...

随机推荐

  1. c#学习路线及目录导航

    一 很久前的想法 转眼间,2018年已经过了四分之一,从我进入学校选择计算机专业到现在工作,已经过去了4年之久了.这一路走来经历了很多的曲折,对软件开发这个职业有了许多新的认识,我主要是从事NET领域 ...

  2. 校园跳蚤市场-Sprint计划

    一.现状 小组成员初步了解所做项目的大致内容,需要时间一步一步分析和规划. 二.部分需求索引卡 第一个阶段完成项目的其中一个模块(商品信息模块). 三.任务认领 产品负责人:林海信 Master:何武 ...

  3. 【贪心算法】POJ-3040 局部最优到全局最优

    一.题目 Description As a reward for record milk production, Farmer John has decided to start paying Bes ...

  4. SpringMVC入门学习案例笔记

    一.数据库环境用mysql,数据库建表语句如下: /* SQLyog v10.2 MySQL - 5.1.72-community : Database - mybatis ************* ...

  5. Keil MDK中的Code, RO-data , RW-data, ZI-data分别代表什么意思?(转)

    一 基础知识 字节  8位半字  16位字    32位 二 解惑 Code, RO-data,RW-data,ZI-data Code为程序代码部分RO-data 表示 程序定义的常量const t ...

  6. [Cnbeta]龙芯处理器性能怎么样

    龙芯处理器性能怎么样?下一代CPU同频性能可达英特尔90% 在高性能处理器领域,英特尔是天花顶一般的存在(先不算地位特殊的IBM公司),国内发展CPU处理器的公司很多,绝大多数实力跟英特尔相比都差很远 ...

  7. SQL SERVER ENTERPRISE EDITION-CORE VS SERVER+CAL – DEMO ON DIFFERENCES

    SQL SERVER ENTERPRISE EDITION-CORE VS SERVER+CAL – DEMO ON DIFFERENCES DHARMENDRA KESHARIAugust 03, ...

  8. @Primary 注解引出的问题

    @Primary 注解 刚看到这个,还以为是持久层的注解呢,以为和@Id差不多,一查才知道,这两个风马牛不相及,反倒和@Qualifier以及@Resource有点像了,但是相比而言,后面两个更加的灵 ...

  9. 【算法】—— 1到n中减少了一个数,顺序被打乱,找出缺失的数

    问题 有0-n这n+1个数,但是其中丢了一个数,请问如何找出丢了哪个数? 五种方法 1)用1+2+...+n减去当前输入数据的总和.时间复杂度:O(n) 空间复杂度:O(1) [容易溢出] 2)用12 ...

  10. zabbix自定义监控阿里云RDS服务

    zabbix自定义监控rds zabbix通过阿里云api 自动发现.监控阿里云RDS-Mysql数据库 注意事项  脚本会收集RDS别名,  不要默认别名 不要使用中文别名(zabbix不识别)  ...