IO流
IO流
1、IO流概述
1.1 什么是IO
- i :input 输入(读取:从硬盘读取数据到内存中)
- o :output 输出(写入:把内存中的数据写入到硬盘中保存)
- 流:数据(字符/字节)1个字符=2个字节,1个字节=8个二进制位
注意:任意一个程序都必须进入到内存中来执行!
1.2 IO的流向说明图解

2、字节流
2.1 一切皆为字节
一切文本数据(文本、图片、视频等)在存储时,都是以二进制数字的形式保存,都是一个一个的字节,传输时也一样如此。因此,字节流可以传输任意文件流数据。在操作流的时候,我们要时刻明确,无论使用什么样的流对象,底层传输的始终为二进制数据。
2.2 字节输出流【OutputStream】
- java.io.OutputStream; 字节输出流
- public abstract class OutputStream 此抽象类是表示输出字节流的所有类的超类
- 共性方法:
- public void close(); 关闭此输出流并释放与此流相关联的任何系统资源
- public void flush(); 刷新此输出流并强制任何缓冲的输出字节被写出
- public abstract void write(int b); 将指定的字节写入此输出流
- public void write(byte[] b); 将b.length个字节从指定的字节数组写入此输出流
- public void write(byte[] b,int off,int len); 将字节数组中从偏移量off开始的len个字节写入此输出流
2.3 FileOutputStream类
java.io.FileOutputStream extends OutputStream
FileOutputStream:文件字节输出流
作用:把内存中的数据写入到硬盘的文件中。
2.3.1 构造方法
FileOutputStream(String name); 创建一个向具有指定名称的文件中写入数据的输出文件流
FIleOutputStream(File file); 创建一个向指定File对象表示的文件中写入数据的文件输出流
参数:写入数据的目的
String name:目的地是一个文件的路径
File file:目的地是一个文件
构造方法的作用:
a. 创建一个FileOutputStream对象
b. 会根据构造方法中传递的文件/文件路径,创建一个空的文件
c. 会把FileOutputStream对象指向创建好的文件
2.3.2 写出字节数据
写入数据的原理:(内存-->硬盘)
java程序 --> JVM --> OS(操作系统) --> OS调用写数据的方法 --> 把数据写入到文件中
字节输出流的使用步骤:
1. 创建一个FileOutputStream对象,构造方法中传递写入数据的目的地
2. 调用FileOutputStream对象中的write方法,把数据写入到文件中
3. 释放资源(流使用会占用一定的内存,使用完毕要把内存清空,保证程序的效率)
import java.io.FileOutputStream;
import java.io.IOException;
//一次写入单个字节的方法:
public class Demo1OutputStream {
public static void main(String[] args) throws IOException {
//1.创建一个FileOutputStream对象,传递写入数据的目的地:文件路径名
FileOutputStream fos = new FileOutputStream("day09\\io\\a.txt");
//2.调用FileOutputStream对象中的write(),把数据写入到文件中
fos.write(97); //一次写入1个字节
//3.释放资源
fos.close();
}
}
FileOutputStream写入单个字节原理图解:

import java.io.*;
import java.util.Arrays;
/*
一次写多个字节的方法:
- public void write(byte[] b); 将b.length个字节从指定的字节数组写入此输出流
- public void write(byte[] b,int off,int len); 从指定的字节数组写入len个字节,
从偏移量off开始输出到此输出流
*/
public class Demo2OutputStream {
public static void main(String[] args) throws IOException {
//1.创建一个FileOutputStream对象,构造方法中的参数传递:File file 文件
FileOutputStream fos = new FileOutputStream(new File("day09\\io\\b.txt"));
//2.调用write(),把内存中的数据写入到文件中
//在文件中显示100,写入3个字节
fos.write(49);
fos.write(48);
fos.write(48);
/*
public void write(byte[] b); 将b.length个字节从指定的字节数组写入此输出流
一次写多个字节:
如果写的第一个字节是正数(0~127),那么显示的时候就会查询ASCII表
如果写的第一个字节是负数,那么第一个字节会和第二个字节,两个字节组成一个中文显示,
查询系统默认码表(GBK)
*/
byte[] b = {65,66,67,68,69}; //ABCDE
//byte[] b = {-65,-66,-67,68,69}; //烤紻E
fos.write(b);
/*
public void write(byte[] b,int off,int len); 从指定的字节数组写入len个字节,
从偏移量off开始输出到此输出流
int off:数组的开始索引
int len:写入几个字节
*/
fos.write(b,1,2); //BC
/*
写入字符的方法:可以使用String类中的方法把字符串转换为字节数组。
byte[] getBytes() 把字符串转换为字符数组
*/
byte[] bytes = "你好".getBytes();
System.out.println(Arrays.toString(bytes)); //[-28, -67, -96, -27, -91, -67]
fos.write(bytes);
//3.关闭资源
fos.close();
}
}
2.3.3 数据追加续写和换行
/*
追加写/续写:使用两个参数的构造方法
FileOutputStream(String name, boolean append) 创建一个向具有指定 name 的文件中写入数据的输出文件流
FileOutputStream(File file, boolean append) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
参数:
String name,File file:写入数据的目的地
boolean append:追加写开关
true:创建对象不会覆盖源文件,继续在文件的末尾追加写数据
false:创建一个新文件,覆盖源文件
写换行:写换行符号
windows:\r\n
linux: /n
mac: /r
*/
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo3OutputStream {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("day09\\io\\c.txt",true);
for (int i = 1; i < 6; i++) {
fos.write("HelloWorld".getBytes());
fos.write("\r\n".getBytes());
}
fos.close();
}
}
2.4 字节输入流【InputStream】
java.io.InputStream: 字节输入流
此抽象类是表示字节输入流的所有类的超类。
定义了所有子类的共性方法:
public void close(); 关闭此输入流并释放与该流相关的所有系统资源
public abstract int read(); 从输入流中读取数据的一个字节
public int read(byte[] b); 从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中
2.5 FileInputStream类
java.io.FileInputStream extends InputStream
FileInputStream: 文件字节输入流
作用:把硬盘中的数据读取到内存中使用
2.5.1 构造方法
public FileInputStream(String name);
public FileInputStream(File file);
参数:读取文件的数据流
String name:文件的路径
FIle file:文件
构造方法的作用:
1.会创建一个FileInputStream对象
2.会把FileInputStream对象指定构造方法中要读取的文件
2.5.2 读取字节数据
读取数据的原理:(硬盘-->内存)
java程序 --> JVM --> OS --> OS调用读取数据的方法 -->读取文件
字节输入流的使用步骤:
1.创建一个FileInputStream对象,构造方法中绑定要读取的数据
2.使用FileInputStream对象中的read方法,读取文件
3.释放资源
2.5.2.1 读取单个字节
import java.io.FileInputStream;
import java.io.IOException;
//一次读取一个字节:
public class Demo1InputStream {
public static void main(String[] args) throws IOException {
//1.创建一个FileInputStream对象,构造方法中绑定要读取的数据
FileInputStream fos = new FileInputStream("day09\\io\\demo1OutputStream\\a.txt");
//2.int read()读取文件中的一个字节并返回,读取到文件的末尾返回-1
// System.out.println(fos.read());
// System.out.println(fos.read());
// System.out.println(fos.read());
// System.out.println(fos.read()); //已经到文件末尾,返回-1
/*
发现以上读取文件是一个重复的过程,所以可以使用循环优化
不知道文件中有多少字节,使用while循环
while循环结束条件,读取到-1时结束
*/
// while(true){
// int b = fos.read();
// if(b == -1)
// break;
// System.out.println(b);
// }
int len = 0;
while((len = fos.read()) != -1){
System.out.print((char)len);
}
//3.释放资源
fos.close();
}
}
2.5.2.2 原理图:

2.5.2.3 读取多个字节
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
/*
字节输入流一次读取多个字节的方法:
int read(byte[] b); 从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中
明确两件事情:
1.方法的参数byte[]的作用?
起到缓冲作用,存储每次读取到的多个字节
数组的长度一般定义为1024(1kb)或者1024的整数倍
2.方法的返回值int是什么?
读取的有效字节个数。
String类的构造方法
String(byte[] bytes); 把字节数组转换成字符串
String(byte[] bytes,int offset,int length); 把字节数组的一部分转换为字符串 offset:开始索引 length:转换的字节个数
*/
public class Demo2InputStream { public static void main(String[] args) throws IOException {
//1.创建FileInputStream对象,构造方法中绑定要读取的数据
FileInputStream fos = new FileInputStream("day09\\io\\demo1OutputStream\\b.txt");
//2.使用FileInputStream中的read方法读取数据
//int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中
/*byte[] bytes = new byte[2];
int len = fos.read(bytes);
System.out.println(len); //2
//System.out.println(Arrays.toString(bytes)); //[65,66]
System.out.println(new String(bytes)); //AB
len = fos.read(bytes);
System.out.println(len); //2
System.out.println(new String(bytes)); //CD
len = fos.read(bytes);
System.out.println(len); //1
System.out.println(new String(bytes)); //ED
len = fos.read(bytes);
System.out.println(len); //-1
System.out.println(new String(bytes)); //ED
*/
/*
发现以上读取文件是一个重复的过程,所以可以使用循环优化
不知道文件中有多少字节,使用while循环
while循环结束条件,读取到-1时结束
*/
byte[] bytes = new byte[1024]; //存储读取到的多个字节
int len = 0;
while((len=fos.read(bytes)) != -1){
//String(byte[] bytes,int offset,int length); 把字节数组的一部分转换为字符串
System.out.println(new String(bytes,0,len)); //ABCDE
}
//3.释放资源
fos.close();
}
}
2.5.2.4 原理图:

2.6 字节流练习:图片复制
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
文件复制练习:一读一写
明确:
数据源:c:\\lyq.jpg
数据的目的地:d:\\lyq.jpg
文件复制的步骤:
1.创建一个字节输入流对象,构造方法中绑定要读取的数据源
2.创建一个字节输出流对象,构造方法中绑定要写入的目的地
3.使用字节输入流对象中的read方法,读取文件
4.使用字节输出流对象中的write方法,把读取的字节写入到目的地的文件中
5.释放资源
*/
public class Demo1CopyFile {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
//1.创建一个字节输入流对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("c:\\lyq.jpg");
//2.创建一个字节输出流对象,构造方法中绑定要写入数据的目的地
FileOutputStream fos = new FileOutputStream("d:\\lyq.jpg",true);
//3.使用字节输入流对象中的read方法,读取文件
byte[] bytes = new byte[1024];
int len = 0; //每次读取的有效字节个数
while((len = fis.read(bytes)) != -1){
//4.写入
fos.write(bytes,0,len);
}
//5.释放资源(先关写的,后关闭读的;如果写完了,肯定读取完了。)流的关闭原则:先开后关,后开先关。
fos.close();
fis.close();
long end = System.currentTimeMillis();
System.out.println("复制文件共耗时:" + (end - start) + "ms");
}
}
2.6.1 复制原理图解

3、字符流
3.1 字符输入流【Reader】
java.io.Reader:字符输入流。是字符输入流最顶层父类,定义了一些共性的成员方法,是一个抽象类
共性的方法:
int read(); 读取单个字符并返回
int read(char[] cbuf); 一次读取多个字符,将字符读入数组。
void close(); 关闭该流并释放与之关联的所有资源
3.2 FileReader类
java.io.FileReader extends InputStreamReader extends Reader
FileReader:文件字符输入流
作用:把硬盘中的数据以字符的方式读取到内存中
构造方法:
public FileReader(String fileName);
public FileReader(File file);
参数:读取文件的数据源
String fileName:文件的路径名
File file:一个文件
FileReader构造方法的作用:
1.创建一个FileReader对象
2.把FileReader对象指向要读取的文件
字符输入流的使用步骤:
1.创建FileReader对象,构造方法中绑定要读取的数据源
2.使用FileReader对象中的read方法,读取文件
3.释放资源
读取字符数据:
import java.io.FileReader;
import java.io.IOException;
public class Demo2Reader {
public static void main(String[] args) throws IOException {
//1.创建一个FileReader对象:构造方法中绑定要读取的数据源
FileReader fr = new FileReader("day09\\io\\demo1OutputStream\\c.txt");
//2.int read(); 读取单个字符
/*int len = 0; //读取的有效字符个数
while((len = fr.read()) != -1) {
System.out.print((char) len);
}*/
//int read(char[] cbuf); 一次读取多个字符,将字符读入数组。
char[] chars = new char[1024];
int len = 0; //记录每次读取的有效字符个数
while((len = fr.read(chars)) != -1){
/*
String类的构造方法:
String(char[] value); 把字符数组转换为字符串
String(char[] value,int offset,int count); 把字符数组的一部分转换为字符串
offset:开始索引 count:转换的个数(偏移量)
*/
System.out.print(new String(chars,0,len));
}
//3.释放资源
fr.close();
}
}
3.3 字符输出流【Writer】
java.io.Writer:字符输出流。是字符输出流最顶层父类,定义了一些共性的成员方法,是一个抽象类
共性方法:
abstract void close() 关闭此流,但要先刷新它。
abstract void flush() 刷新该流的缓冲。
void write(int c) 写入单个字符。
void write(char[] cbuf) 写入字符数组。
abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分。off:开始索 引 len:写的字符个数
void write(String str) 写入字符串。
void write(String str, int off, int len) 写入字符串的某一部分。
3.4 FileWriter类
java.io.FileWriter extends OutputStreamWriter extends Writer
FileWriter:文件字符输出流
作用:把内存中字符数据写入到文件中
构造方法:
FileWriter(File file); 根据给定的File对象,构造一个FileWriter对象。
FileWriter(String fileName); 根据给定的文件名构造一个FIleWriter对象。
参数:写入数据的目的地
String fileName: 文件的路径
File file:文件
构造方法的作用:
1.会创建一个FileWriter对象
2.会根据构造方法中传递的文件/文件的路径,创建文件
3.把FileWriter对象指向创建好的文件
字符输出流的使用步骤:
1.创建FileWriter对象,构造方法中绑定要写入数据的目的地
2.使用FileWriter对象中的write方法,把数据写入到内存缓冲区中(字符转换为字节的过程)
3.使用FileWriter对象中的flush方法,把内存缓冲区中的数据,刷新到文件中
4.释放资源(会先把内存缓冲区中的数据刷新到文件中)
基本写出数据
import java.io.FileWriter;
import java.io.IOException;
public class Demo1Writer {
public static void main(String[] args) throws IOException {
//1.创建FileWriter对象,构造方法中绑定要写入数据的目的地
FileWriter fw = new FileWriter("day09\\io\\demo1OutputStream\\d.txt");
//2.void write(int c) 写入单个字符。
// 使用FileWriter对象中的write方法,把数据写入到内存缓冲区中(字符转换为字节的过程)
fw.write(97);
//3.void flush() 刷新该流的缓冲. 把内存缓冲区中的数据,刷新到文件中
fw.flush();
//4.释放资源
fw.close();
}
}
import java.io.FileWriter;
import java.io.IOException;
/*
字符输出流写数据的其他方法:
void write(char[] cbuf) 写入字符数组。
abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分。off:开始索引 len:写的字符个数
void write(String str) 写入字符串。
void write(String str, int off, int len) 写入字符串的某一部分。
*/
public class Demo3Writer {
public static void main(String[] args) throws IOException {
//1.创建FileWriter对象:构造方法中绑定要写入的数据源
FileWriter fw = new FileWriter("day09\\io\\demo1OutputStream\\e.txt");
//2.调用FileWriter对象中的write方法,把数据写入到内存缓冲区中
//void write(char[] cbuf) 写入字符数组
char[] cs = {'a','b','c','d','e'};
fw.write(cs);
//void write(char[] cbuf,int off,int len) 写入字符数组的某一部分
fw.write(cs,1,3);
//void write(String str) 写入字符串
fw.write("你好");
//void write(String str, int off, int len) 写入字符串的某一部分。
fw.write("杜兰特",0,2);
//4.释放资源
fw.close();
}
}
关闭和刷新
import java.io.FileWriter;
import java.io.IOException;
/*
flush方法和close方法的区别:
- flush:刷新缓冲区,流对象可以继续使用。
- close:先刷新缓冲区, 然后通知系统释放资源,流对象不可以再被使用了。
*/
public class Demo2CloseAndFlush {
public static void main(String[] args) throws IOException {
//1.创建FileWriter对象,构造方法中绑定要写入数据的目的地
FileWriter fw = new FileWriter("day09\\io\\demo1OutputStream\\d.txt");
//2.使用FileWriter对象中的write方法,把数据写入到内存缓冲区中(字符转换为字节的过程)
//void write(int c) 写入单个字符。
fw.write(97);
//3.使用FileWriter对象中的flush方法,把内存缓冲区中的数据,刷新到文件中
//3.void flush() 刷新该流的缓冲.
fw.flush();
//刷新之后流可以继续使用
fw.write(98);
//4.释放资源
fw.close();
//close()之后流已经关闭,已经从内存中消失,就不能够再使用。
fw.write(99); //IOException: Stream closed
}
}
续写和换行
import java.io.FileWriter;
import java.io.IOException;
/*
续写和换行:
续写/追加写:使用两个参数的构造方法
FileWriter(String fileName,boolean append);
FileWriter(File file,boolean append);
参数:
String fileName,File file:写入数据的目的地
boolean append:续写开关 true:不会创建新的文件覆盖源文件,可以续写;false:创建新的文件覆盖源文件
换行:换行符号
windows:\r\n
linux:\n
mac:\r
*/
public class Demo4Writer {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("day09\\io\\demo1OutputStream\\f.txt",true);
for (int i = 0; i < 3; i++) {
fw.write("詹姆斯\r\n");
}
fw.close();
}
}
4、IO异常处理
4.1 JDK7之前
import java.io.FileWriter;
import java.io.IOException;
/*
在jdk1.7之前使用 try-catch-finally 处理流中的异常
格式:
try{
可能会出现异常的代码;
}catch(异常类变量 变量名){
异常的处理逻辑;
}finally{
一定会执行的代码;
释放资源
}
*/
public class Demo1TryCatch {
public static void main(String[] args) {
//提高fw变量的作用域,让finally可以使用
FileWriter fw = null;
try {
//可能会出现异常的代码
fw = new FileWriter("w:\\day09\\io\\demo1OutputStream\\f.txt", true);
for (int i = 0; i < 3; i++) {
fw.write("徐紫薇\r\n");
}
// fw.close();
}catch(IOException e){
//异常的处理逻辑
System.out.println(e);
}finally {
//一定会执行的代码
/*
如果创建对象失败,fw的默认值就是null,null是不能调用方法的,会抛出NullPointerException,
此处需加一个判断,不是null再把资源释放。
*/
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
4.2 JDK7新特性
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
JDK7新特性:
在try的后面可以增加一个(),在括号中可以定义流对象,那么这个流对象的作用域就在try中有效,
try中的代码执行完毕,会自动把流对象释放,不用写finally。
格式:
try(定义流对象,定义流对象...){
可能会发生异常的代码;
}catch(异常类变量 变量名){
异常的处理逻辑;
}
*/
public class Demo2JDK7 {
public static void main(String[] args) {
try(//1.创建一个字节输入流对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("c:\\lyq.jpg");
//2.创建一个字节输出流对象,构造方法中绑定要写入数据的目的地
FileOutputStream fos = new FileOutputStream("d:\\lyq.jpg");){
//可能会产生异常的代码
//3.使用字节输入流对象中的read方法,读取文件
byte[] bytes = new byte[1024];
int len = 0; //每次读取的有效字节个数
while((len = fis.read(bytes)) != -1){
//4.写入
fos.write(bytes,0,len);
}
}catch(IOException e){
//异常的处理逻辑
System.out.println(e);
}
}
}
4.3 JDK9
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
JDK9新特性:(比jdk7新特性还要麻烦一些!了解。)
try的前边可以定义流对象,
在try后边的()中可以直接引入流对象的名称(变量名),
try代码执行完毕后,流对象也可以释放掉,不用写Finally
格式:
A a = new A();
B b = new B();
try(a,b){
可能会产生异常的代码块
}catch(异常类变量 变量名){
异常的处理逻辑;
}
*/
public class Demo3JDK9 {
public static void main(String[] args) throws IOException {
//1.创建一个字节输入流对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("c:\\lyq.jpg");
//2.创建一个字节输出流对象,构造方法中绑定要写入数据的目的地
FileOutputStream fos = new FileOutputStream("d:\\lyq.jpg");
/*try(fis;fos){
//3.使用字节输入流对象中的read方法,读取文件
byte[] bytes = new byte[1024];
int len = 0; //每次读取的有效字节个数
while((len = fis.read(bytes)) != -1){
//4.写入
fos.write(bytes,0,len);
}
}catch(IOException e){
System.out.println(e);
}*/
//fos.write(97);//Stream closed
}
}
5、属性集【Properties】
java.util.Properties集合 extends Hashtable<K,V> implements Map<K,V>接口
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。
Properties 集合是唯一一个和IO流有关的集合:
可以使用Properties集合中的store():把集合中的临时数据,持久化写入到硬盘中存储。
可以使用Properties集合中的load():把硬盘中保存的文件(键值对),读取到集合中使用。
属性列表中每个键及其值都是一个字符串:
Properties集合是一个双列集合,key和value默认都是字符串。
/*
①使用Properties集合存储数据,遍历取出Properties集合中的数据:
Properties集合有一些操作字符串的特有方法:
Object setProperty(String key,String value); 调用Hashtable的put方法
String getProperty(String key); 通过key找到value值,相当于Map集合中的get()
Set<String> stringPropertyNames(); 返回此属性列表中的键集,其中该键及其对应值是字符串,
相当于Map集合中的keySet()
*/
public class Demo1Properties{
public static void main(String[] args) {
//创建Properties集合对象
Properties prop = new Properties();
//使用setProperty()往集合中添加数据
prop.setProperty("杜兰特","35");
prop.setProperty("哈登","13");
prop.setProperty("保罗乔治","24");
//遍历:
//使用stringPropertyNames()把Properties集合中的键取出,存储到一个set集合中
Set<String> set = prop.stringPropertyNames();
//遍历set集合,取出Properties集合的每一个键
for(String key:set){
//通过key找value:getProperty();
String value = prop.getProperty(key);
System.out.println(key + "=" + value);
}
}
}
/*
②使用Properties集合中的store():把集合中的临时数据,持久化写入到硬盘中存储:
void store(OutputStream out,String comments);
void store(Writer writer,String comments);
参数:
OutputStream out:字节输出流,不能写入中文: 会出现乱码
Writer writer:字符输出流,可以写中文
String comments:注释,用来解释说明保存的文件是做什么用的。
不能使用中文,会产生乱码,默认是Unicode编码
一般使用"空字符串"
使用步骤:
1.创建Properties集合对象,添加数据
2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
3.使用Properties集合中的store(),把集合中的临时数据,持久化写入到硬盘中存储
4.释放资源
*/
public class Demo2Properties{
public static void main(String[] args) {
//1.创建Properties集合对象
Properties prop = new Properties();
//使用setProperty()往集合中添加数据
prop.setProperty("杜兰特","35");
prop.setProperty("Harden","13");
prop.setProperty("PG","24");
//jdk7新特性:会自动释放资源,不用写finally。
try(//2.创建文件字节输出流对象,构造方法中绑定要输出的目的地
FileOutputStream fos = new FileOutputStream("day09\\io\\demo7Properties\\a.txt");
//创建文件字符输出流对象,构造方法中绑定要输出的目的地
FileWriter fw = new FileWriter("day09\\io\\demo7Properties\\b.txt");){
//3.调用Properties集合中的store(): 把集合中的临时数据,持久化写入到硬盘中存储
prop.store(fos,"save data");
prop.store(fw,"NBA超级球星");
}catch(IOException e){
//异常的处理逻辑
System.out.println(e);
}
}
}
/*
③使用Properties集合中的load():把硬盘中保存的文件(键值对),读取到集合中使用:
void load(InputStream inStream);
void load(Reader reader);
参数:
InputStream inStream:字节输入流,不能读取含有中文的键值对
Reader reader:字符输入流,能读取含有中文的键值对
使用步骤:
1.创建Properties集合对象
2.使用Properties集合对象中的load()读取保存键值对的文件
3.遍历Properties集合
注意:
1.存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其它符号)
2.存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取
3.存储键值对的文件中,键与值默认都是字符串,不用再加引号
*/
public class Demo3Properties {
public static void main(String[] args) throws IOException {
//1.创建Properties集合对象
Properties pro = new Properties();
//2.使用Properties集合对象中的load()读取保存键值对的文件
pro.load(new FileReader("day09\\io\\demo7Properties\\b.txt"));
//3.遍历Properties集合
Set<String> set = pro.stringPropertyNames();
for(String key:set){
//通过key找value
String value = pro.getProperty(key);
System.out.println(key + "=" + value);
}
}
}
6、缓冲流
6.1 概述
缓冲流:也叫高效流;是对4个基本FileXxx流的增强,按照数据类型分为:
- 字节缓冲流:BufferedInputStream , BufferedOutputStream
- 字符缓冲流:BufferedReader , BufferedWriter
基本原理:是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写效率。
6.2 字节缓冲流
6.2.1 字节缓冲输出流
java.io.BufferedOutputStream extends OutputStream
BufferedOutputStream:字节缓冲输出流
继承自父类的共性成员方法:
- public void close(); 关闭此输出流并释放与此流相关联的任何系统资源
- public void flush(); 刷新此输出流并强制任何缓冲的输出字节被写出
- public abstract write(int b); 将指定的字节输出流
- public void write(byte[] b); 将b.length个字节从指定的字节数组写入此输出流
- public void write(byte[] b,int off,int len); 从指定的字节数组中写入len个字节,从偏移量off开 始输出到此输出流
构造方法:
BufferedOutputStream(OutputStream out); 创建一个新的缓冲输出流,以将数据写入指定的底层输出流
BufferedOutputStream(OutputStream out,int size); 创建一个新的缓冲输出流,以将具有指定缓冲区大 小的数据写入指定的底层输出流
参数:
OutputStream out:字节输出流
我们可以传递FileOutputStream,缓冲流会给FileOutputStream增加一个缓冲区,提高 FileOutputStream的写入效率
int size:指定缓冲流内部缓冲区的大小,不指定默认。
使用步骤:(重点)
1.创建FileOutputStream对象,构造方法中绑定要输出的目的地
2.创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream对象 的写入效率
3.使用BufferedOutputStream对象中的write方法,把数据写入到内部缓冲区中
4.使用BufferedOutputStream对象中的flush方法,把内存缓冲区中的数据,刷新到文件中
5.释放资源(会先调用flush方法刷新数据,第4步可以省略)
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class demo1BufferOutputStream {
public static void main(String[] args) throws IOException {
//1.创建FileOutputStream对象,构造方法中绑定要输出的目的地
FileOutputStream fos = new FileOutputStream("day09\\io\\demo8BufferedStream\\x.txt");
//2.创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象
BufferedOutputStream bos = new BufferedOutputStream(fos);
//3.使用BufferedOutputStream对象中的write方法,把数据写入到内部缓冲区中
bos.write("Java核心技术".getBytes());
//4.使用BufferedOutputStream对象中的flush方法,把内存缓冲区中的数据,刷新到文件中
bos.flush();
//5.释放资源
bos.close(); //关闭缓冲流会自动把字节流也关掉。
}
}
6.2.2 字节缓冲输入流
java.io.BufferedInputStream extends InputStream;
BufferedInputStream: 字节缓冲输入流
继承自父类的成员方法:
- public void close(); 关闭此输入流并释放与该流关联的所有系统资源
- public abstract int read(); 从输入流中读取数据的下一字节
- public int read(byte[] b); 从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中
构造方法:
BufferedInputStream(InputStream in); 创建一个BufferedInputStream并保存其参数,即输入流in,以 便将来使用
BufferedInputStream(InputStream in,int size); 创建具有指定缓冲区大小的BufferedInputStream 并保存其参数,即输入流in
参数:
InputStream in:字节输入流
int size:指定缓冲区内部缓冲区的大小,不指定默认。
使用步骤:(重点)
1.创建FileInputStream对象,构造方法中绑定要读取的数据源
2.创建BufferedInputStream对象,构造方法中传递FileInputStream对象,提高FileInputStream对象的读 取效率
3.使用BufferedInputStream对象中的read方法,读取文件
4.释放资源
public class demo2BufferedInputStream {
public static void main(String[] args) throws IOException {
//1.创建FileInputStream对象,构造方法中绑定要读取的数据源
FileInputStream fis = new FileInputStream("day09\\io\\demo8BufferedStream\\x.txt");
//2.创建BufferedInputStream对象,构造方法中传递FileInputStream对象
BufferedInputStream bis = new BufferedInputStream(fis);
//3.使用BufferedInputStream对象中的read方法,读取文件
//int read(); 从输入流中读取下一个字节
// int len = 0; //读取的有效字节个数
// while((len = bis.read()) != -1){
// System.out.println((char)len);
// }
//int read(byte[] b); 从输入流中读取一定数量的字节,并将其存储在缓冲区数组中。
int len = 0;
byte[] bytes = new byte[1024];
while((len = bis.read(bytes)) != -1){
System.out.println(new String(bytes,0,len));
}
//4.释放资源
bis.close();
}
}
6.2.3 效率测试
import java.io.*;
/*
文件复制练习:一读一写 (使用缓冲流并进行效率测试)
明确:
数据源:c:\\lyq.jpg
数据的目的地:d:\\lyq.jpg
文件复制的步骤:
1.创建字节缓冲输入流对象,构造方法中传递字节输入流
2.创建字节缓冲输出流对象,构造方法中传递字节输出流
3.使用字节缓冲输入流对象中的read(),读取文件
4.使用字节缓冲输出流对象中的write(),把读取的数据写入到内部缓冲区中
5.释放资源(会先刷线缓冲区中的数据,刷新到文件中)
使用缓冲流和基本流复制同一文件:
缓冲流:(使用缓冲流的效率要比基本流高很多!)
共耗时:101ms (一次读取一个字节,写入一个字节)
共耗时:8ms (使用缓冲数组读取多个字节,写入多个字节)
基本流:
共耗时:9907ms (每次读取一个字节,写入一个字节)
共耗时:25ms (每次读取多个字节,写入多个字节)
*/
public class Demo3CopyFile {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
//1.创建字节缓冲输入流对象,构造方法中传递字节输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c:\\lyq.jpg"));
//2.创建字节缓冲输出流对象,构造方法中传递字节输出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\lyq.jpg"));
//3.使用字节缓冲输入流对象中的read(),读取文件
//一次读取一个字节写入一个字节的方式:
/*int len = 0;
while((len = bis.read()) != -1){
//4.写入
bos.write(len);
}*/
//使用数组缓冲读取多个字节,写入多个字节:
int len = 0;
byte[] bytes = new byte[1024];
while((len = bis.read(bytes)) != -1){
//4.写入
bos.write(bytes,0,len);
}
//5.释放资源
bos.close();
bis.close();
long end = System.currentTimeMillis();
System.out.println("复制文件共耗时:" + (end - start) + "ms");
}
}
6.3 字符缓冲流
6.3.1 字符缓冲输出流
java.io.BufferedWriter extends Writer
BufferedWriter: 字符缓冲输出流
继承自父类的共性成员方法:
abstract void close() 关闭此流,但要先刷新它。
abstract void flush() 刷新该流的缓冲。
void write(int c) 写入单个字符。
void write(char[] cbuf) 写入字符数组。
abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分。off:开始索引 len:写的字符个数
void write(String str) 写入字符串。
void write(String str, int off, int len) 写入字符串的某一部分。
构造方法:
BufferedWriter(Writer out)
创建一个使用默认大小输出缓冲区的缓冲字符输出流。
BufferedWriter(Writer out, int sz)
创建一个使用给定大小输出缓冲区的新缓冲字符输出流。
参数:
Writer out:字符输出流
我们可以传递FileWriter,缓冲流会给FileWriter增加一个缓冲区,提高FileWriter的写入效率
int size:指定缓冲区大小,不写默认大小。
特有的成员方法:
void newLine(); 写入一个行分隔符。会根据不同的操作系统,获取不同的行分隔符
换行:换行符号
windows:\r\n
linux:/n
mac:/r
使用步骤:
1.创建BufferedWriter对象,构造方法中传递字符输出流
2.调用BufferedWriter中的write(),把数据写入到内存缓冲区中
3.调用BufferedWriter中的flush(),把内存缓冲区中的数据,刷新到文件中
4.释放资源
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Demo4BufferWriter {
public static void main(String[] args) throws IOException {
//1.创建BufferedWriter对象,构造方法中传递字符输出流
BufferedWriter bw = new BufferedWriter(new FileWriter("day09\\io\\demo8BufferedStream\\z.txt"));
//2.调用write(),把数据写入到内存缓冲区
for (int i = 0; i < 6; i++) {
bw.write("亚洲杜兰特");
//bw.write("\r\n");
bw.newLine();
}
//3.调用flush(),把内存缓冲区中的数据,刷新到文件中
bw.flush();
//4.释放资源
bw.close();
}
}
6.3.2 字符缓冲输入流
java.io.BufferedReader extends Reader
BufferedReader: 字符缓冲输入流
继承自父类的共性成员方法:
int read(); 读取单个字符并返回。
int read(char[] cbuf); 一次读取多个字符,将字符读入数组。
void close(); 关闭该流并释放与之关联的所有资源。
构造方法:
BufferedReader(Reader in)
创建一个使用默认大小输入缓冲区的缓冲字符输入流。
BufferedReader(Reader in, int sz)
创建一个使用指定大小输入缓冲区的缓冲字符输入流。
参数:
Reader in:字符输入流
我们可以传递FileReader,缓冲流会给FileReader增加一个缓冲区,提高FileReader读取效率
int sz:指定缓冲区的大小,不写默认大小
特有的成员方法:
String readLine(); 读取一个文本行。读取一行数据
行的终止符号:通过下列字符之一即可认为某行已终止:换行('\n'),回车('\r'),或回车后直接跟着换行('\r\n')
返回值:
包含该行内容的字符串,不包含任何终止符,如果已到达流末尾,则返回null。
使用步骤:
1.创建字符缓冲流对象,构造方法中传递字符输入流
2.调用字符缓冲流对象中的方法read/readLine,读取文本数据
3.释放资源
public class Demo5BufferReader {
public static void main(String[] args) throws IOException {
//1.
BufferedReader br = new BufferedReader(new FileReader("day09\\io\\demo8BufferedStream\\z.txt"));
//2.
/*int len = 0; //记录读取的有效字符个数
while((len = br.read()) != -1) { //每次读取一个字符
System.out.println((char)len);
}*/
/*int len = 0;
char[] chars = new char[1024];
while((len = br.read(chars)) != -1){ //每次读取多个字符
System.out.println(new String(chars,0,len));
}*/
String str;
while((str = br.readLine()) != null){ //每次读取一行
System.out.println(str);
}
//br.readLine();
//3.释放资源
br.close();
}
}
7、转换流
7.1 字符编码和字符集
字符编码(Character Encoding):就是一套自然语言的字符与二进制数之间的对应规则。
- 编码:字符(能看懂得) ---> 字节(看不懂的)
- 解码:字节(看不懂的) ---> 字符(能看懂的)
编码表:生活中文字和计算机中二进制的对应规则。
字符集(Charset):也叫编码表,是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等。
GBk:中文码表,使用两个字节存储一个中文
UTF-8:国际标准码表,使用三个字节存储一个中文
7.2 编码引出的问题?
编码引出的问题:
FileReader可以读取IDEA默认编码格式(UTF-8)的文件
FileReader读取系统默认编码(中文GBK)会产生乱码 ��ð�
7.3 OutputStreamReader类
java.io.OutputStreamWriter extends Writer;
OutputStreamWriter: 字符流通向字节流的桥梁;可使用指定的charset将要写入流中的字符编码成字节。
(编码:把能看懂得编程看不懂的)
继承来自父类的共性成员方法:
abstract void close() 关闭此流,但要先刷新它。
abstract void flush() 刷新该流的缓冲。
void write(int c) 写入单个字符。
void write(char[] cbuf) 写入字符数组。
abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分。off:开始索引 len:写的字符个数
void write(String str) 写入字符串。
void write(String str, int off, int len) 写入字符串的某一部分。
构造方法:
OutputStreamWriter(OutputStream out); 创建使用默认字符编码的OutputStreamWriter
OutputStreamWriter(OutputStream out,String charsetName); 创建使用指定字符集的OutputStreamWriter
参数:
OutputStream out:字节输出流,可以用来写转换之后的字节到文件中
String charsetName:指定的编码表名称,不区分大小写,可以是utf-8/UTF-8,gbk/GBK...不指定默认使用UTF-8
使用步骤:
1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
2.使用OutputStreamWriter对象中的write(),把字符转换成字节存储在缓冲区中
3.使用OutputStreamWriter对象中的flush(),把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
4.释放资源
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Demo2OutputStreamWriter {
public static void main(String[] args) throws IOException {
//write_utf_8();
write_gbk();
}
/*
使用转换流OutputStreamWriter写GBK格式的文件
*/
private static void write_gbk() throws IOException {
//1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定编码表名称
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("day09\\io\\gbk.txt"),"gbk");
//2.使用OutputStreamWriter对象中的write(),把字符转换为字节存储在缓冲区中
osw.write("你是谁?");
//3.使用OutputStreamWriter对象中的flush(),把缓冲区的字节刷新到文件中
osw.flush();
//4.释放资源
osw.close();
}
/*
使用转换流OutputStreamWriter写UTF-8格式的文件
*/
private static void write_utf_8() throws IOException {
//1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定编码表名称
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("day09\\io\\utf-8.txt"));
//2.使用OutputStreamWriter对象中的write(),把字符转换为字节存储在缓冲区中
osw.write("你是谁?");
//3.使用OutputStreamWriter对象中的flush(),把缓冲区的字节刷新到文件中
osw.flush();
//4.释放资源
osw.close();
}
}
7.4 InputStreamWriter类
java.io.InputStreamReader extends Reader;
InputStreamReader: 字节流通向字符流的桥梁;它使用指定的charset读取字节并将其解码为字符。(解码:把看不懂的变成能看懂的)
继承来自父类的共性成员方法:
int read(); 读取单个字符并返回
int read(char[] cbuf); 一次读取多个字符,将字符读入数组。
void close(); 关闭该流并释放与之关联的所有资源
构造方法:
InputStreamReader(InputStream in); 创建一个使用默认字符集的InputStreamReader
InputStreamReader(InputStream in,String charsetName); 创建使用指定字符集的InputStreamReader
参数:
InputStream in:字节输入流,用来读取文件中保存的字节
String charsetName:指定的编码表名称,不区分大小写,可以是utf-8/UTF-8,gbk/GBK...不指定默认使用UTF-8
使用步骤:
1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
2.使用InputStreamReader对象中的read(),读取文件
3.释放资源
注意事项:
构造方法中的编码表名称要和文件的编码相同,否则会出现乱码
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Demo3InputStreamReader {
public static void main(String[] args) throws IOException {
//read_utf_8();
read_gbk();
}
/*
使用InputStreamReader读取GBK格式的文件
*/
private static void read_gbk() throws IOException {
//1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
//InputStreamReader isr = new InputStreamReader(new FileInputStream("day09\\io\\gbk.txt"),"UTF-8"); //����˭��
InputStreamReader isr = new InputStreamReader(new FileInputStream("day09\\io\\gbk.txt"),"GBK");
//2.调用InputStreamReader对象中的read(),读取文件
//每次读取单个字节
/*int len = 0;
while((len = isr.read()) != -1){
System.out.println((char)len);
}*/
//每次读取多个字节
int len = 0;
char[] chars = new char[1024];
while((len = isr.read(chars)) != -1){
System.out.println(new String(chars,0,len));
}
//3.释放资源
isr.close();
}
/*
使用InputStreamReader读取UTF-8格式的文件
*/
private static void read_utf_8() throws IOException {
//1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
InputStreamReader isr = new InputStreamReader(new FileInputStream("day09\\io\\utf-8.txt"),"utf-8");
//2.调用InputStreamReader对象中的read(),读取文件
//每次读取单个字节
/*int len = 0;
while((len = isr.read()) != -1){
System.out.println((char)len);
}*/
//每次读取多个字节
int len = 0;
char[] chars = new char[1024];
while((len = isr.read(chars)) != -1){
System.out.println(new String(chars,0,len));
}
//3.释放资源
isr.close();
}
}
8、序列化
8.1 概述

8.2 ObjectOutputStream类
-
java.io.ObjectOutputStream extends OutputStream;
-
ObjectOutputStream:对象的序列化流。把对象以流的方式写入到文件中保存。
-
Constructor:
public ObjectOutputStream(OutputStream out); 创建写入指定OutputStream的ObjectOutputStream
-
特有的成员方法:
public final void writeObject(Object obj); 将指定的对象写入ObjectOutputStream
-
使用步骤:
a.创建ObjectOutputStream对象,构造方法中传递字节输出流
b.调用ObjectOutputStream对象中的writeObject(),把对象写入文件中
c.释放资源
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Demo1ObjectOutputStream {
public static void main(String[] args) throws IOException {
//1.
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day09\\io\\demo10ObjectStream\\nbaPlayers.txt"));
//2.
oos.writeObject(new NbaPlayers("杜兰特",35)); //NotSerializableException!
//3.释放资源
oos.close();
}
}
//自定义NbaPlayers类:
import java.io.Serializable;
/*
序列化和反序列化的时候:会抛出NotSerializableException:没有序列化异常。
类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
Serializable接口也叫标记型接口:
要进行序列化和反序列化的类必须实现Serializable接口,就会给类添加一个标记,
当进行序列化和反序列化的时候,就会检测类上是否有这个标记
有:就可以序列化和反序列化
无:就会抛出 NotSerializableException异常!
static:静态关键字
静态优先于非静态加载到内存中(静态优先于对象进入到内存中)
被static修饰的成员变量不能被序列化,序列化的都是对象。
private static int Numbers;
oos.writeObject(new NbaPlayers("杜兰特",35));
Object obj = ois.readObject();
NbaPlayers{name='杜兰特', numbers=0}
transient:瞬态关键字
被transient修饰的成员变量,不能被序列化。
*/
class NbaPlayers implements Serializable {
/*
解决:InvalidClassException异常的方法?
在Serializable接口规定:可序列化类可以通过声明serialVersionUID的字段(该字段必须是static、final的long型字段)
显示声明其自己的serialVersionUID。
static final long serialVersionUID = 42L;
*/
private static final long serialVersionUID = 26L;
private String name;
//球衣号码
//private int numbers;
//private static int numbers; //被static修饰的成员变量,不能被序列化。
//private transient int numbers; //被transient修饰的成员变量,不能被序列化。
public int numbers; //序列化后,更改了其访问权限private-->public,class文件也随之改变。抛出:InvalidClassException!
public NbaPlayers(){
}
public NbaPlayers(String name,int numbers){
this.name = name;
this.numbers = numbers;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getNumbers() {
return numbers;
}
public void setNumbers(int numbers) {
this.numbers = numbers;
}
//重写toString()
/*public String toString(){
return "[NbaPlays: name=" + name + ", numbers=" + numbers + "]";
}*/
@Override
public String toString() {
return "NbaPlayers{" +
"name='" + name + '\'' +
", numbers=" + numbers +
'}';
}
}
8.3 ObjectInputStream类
-
java.io.ObjectInputStream extends InputStream;
-
ObjectInputStream:对象的反序列化流。把文件中保存的对象以流的方式读取出来使用。
-
Constructor:
public ObjectInputStream(InputStream in); 创建从指定InputStream读取的ObjectInputStream
-
特有的成员方法:
public final Object readObject(Object obj); 从ObjectInputStream读取对象
-
使用步骤:
a.创建ObjectInputStream对象,构造方法中传递字节输入流
b.调用ObjectInputStream对象中的readObject(),读取保存对象的文件
c.释放资源
d.使用读取出来的对象(打印)
注意:readObject()声明抛出了ClassNotFoundException(class文件找不到异常) 当不存在对象的class文件时抛出此异常! 反序列化的前提: 1.类必须实现Serializable接口 2.必须存在类对应的class文件
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class Demo2ObjectInputStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//1.创建ObjectInputStream对象,构造方法中传递字节输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day09\\io\\demo10ObjectStream\\nbaPlayers.txt"));
//2.调用ObjectInputStream对象中的readObject(),读取保存对象的文件
Object obj = ois.readObject();
//3.释放资源
ois.close();
//4.打印对象
System.out.println(obj);
NbaPlayers np = (NbaPlayers)obj;
System.out.println(np.getName() + np.getNumbers());
}
}
9、打印流
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
/*
java.io.PrintStream类:打印流
为其它输出流添加了功能,使它们能够方便地打印各种数据值表示形式
PrintStream特点:
1.只负责数据的输出,不负责数据的读取
2.与其它输出流不同,PrintStream永远不会抛出IOException!
3.特有的方法:print,println
void print(任意类型的值);
void println(任意类型的值并换行);
Constructor:
public PrintStream(File file); 输出目的地是一个文件
public PrintStream(OutputStream out); 输出目的地的是一个字节输出流
public PrintStream(String fileName); 输出目的地是一个文件路径
PrintStream extends OutputStream;
继承自父类的成员方法:
- public void close(); 关闭此输出流并释放与此流相关联的所有系统资源
- public void flush(); 刷新此输出流并强制任何缓冲的输出字节流被写出
- public abstract void write(int b); 将指定的单个字节写入此输出流
- public void write(byte[] b); 将b.length个字节从指定的字节数组写入此输出流
- public void write(byte[] b,int off,int len); 从指定的字节数组写入len个字节,从偏移量off开始输出到此输出流
注意:
如果使用继承自父类的write方法写数据,那么查看数据的时候会查询编码表:97-->a
如果使用自己特有的print/println方法写数据,写的数据原样输出:97-->97
*/
public class Demo1PrintStream {
public static void main(String[] args) throws FileNotFoundException {
//创建PrintStream对象,构造方法中绑定要输出的目的地
PrintStream ps = new PrintStream(new FileOutputStream("day09\\io\\print.txt"));
//如果使用继承自父类的write方法写数据,那么查看数据的时候会查询编码表:97-->a
ps.write(97);
//如果使用自己特有的print/println方法写数据,写的数据原样输出:97-->97
ps.println(97);
ps.println(8.28);
ps.println('s');
ps.println("xzw");
ps.println(true);
//释放资源
ps.close();
}
}
import java.io.FileNotFoundException;
import java.io.PrintStream;
/*
可以改变输出语句的目的地(打印流的语句)
输出语句,默认在控制台输出
使用System.setOut(),改变输出语句的目的地,改为参数中传递的打印流的目的地:
public static void setOut(PrintStream out);
重新分配“标准”输出流。
*/
public class Demo2PrintStream {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("我在控制台输出");
PrintStream ps = new PrintStream("day09\\io\\目的地是打印流.txt");
System.setOut(ps); //把输出语句的目的地改变为打印流的目的地
System.out.println("我在打印流的目的地输出");
}
}
浙公网安备 33010602011771号