目录
一、节点流和处理硫
1.1 节点流1.2 处理流 1.3 低级流和高级流理解二、输入输出流
2.1 输入流2.2 输出流 三、文件流(低级流)四、缓冲流(高级流)五、对象流
5.1 对象序列化5.2 对象持久化5.3 反序列化 六、 Reader和Writer
6.1 Reader的常用方法6.2 Writer的常用方法6.3 常用的Reader和Writer
6.3.1 InputStreamReader和OutputStreamWriter6.3.2 BufferedWriter和BufferedReader6.3.3 PrintWriter
注意: 不要在main函数上throws异常,这里为了方便,我就这样写了。
一、节点流和处理硫
1.1 节点流
也称为低级流,可以从或向一个特定的地方(节点读写数据)
1.2 处理流
也称为高级流,对一个已存在的连接和封装,通过所封装的流的功能调用实现数据读写。
处理流的构造方法总要带一个其他流对象做参数。一个流对象经过其他流的多次包装,成为流的链接。
1.3 低级流和高级流理解
读写,可以没有高级流,但是一定要有低级流(数据源是明确的)
二、输入输出流
2.1 输入流
InputStream是所有字节输入流的父类,定义了基础的读取方法,常用方法有: 1. int read() 读取一个字节,以int形式返回,该int值的”低八位”有效,若返回值为-1表示EOF 2. int read(byte[] d) 尝试最多读取给定数据length个字节并存入该数组,返回值为实际读取到的字节量
2.2 输出流
OutputStream是所有字节输出流的父类,其定义了基础的写出方法,常用的方法有: 1. void write(int d) 写出一个字节,写的是给定的int的”低八位” 2. void write(byte[] d) 将给定的字节数组中的所有字节全部写出
三、文件流(低级流)
public class FileStreamDemo {
/**
* 讲了文件流输出用法
* @throws IOException
*/
public static void fileOutPut()
throws IOException{
FileOutputStream fos =
new FileOutputStream(
"fos.txt",
true);
String str =
"我是圆滚滚";
byte[] data = str.getBytes(
"utf-8");
fos.write(data);
fos.close();
}
/**
* 讲了文件流输入用法
* @throws IOException
*/
public static void fileInput()
throws IOException{
FileInputStream fiStream =
new FileInputStream(
"fos.txt");
byte[] data =
new byte[
100];
int len = fiStream.read(data);
String str =
new String(data,
0, len,
"utf-8");
System.out.println(str);
}
public static void main(String[] args)
throws IOException {
fileInput();
}
}
四、缓冲流(高级流)
/**
* 这个类主要是讲了缓冲流的使用
* 缓冲流其实内部也是定义一个buffer数组(一定大小的缓冲池)的方式来提高读写效率,将慢速的硬盘IO,变为缓存中的数据读取
* @author Gupan
*
*/
public class CacheStreamDemo {
public static void main(String[] args)
throws IOException {
FileInputStream fiStream =
new FileInputStream(
"fos.txt");
BufferedInputStream bis =
new BufferedInputStream(fiStream);
FileOutputStream foStream =
new FileOutputStream(
"fos.txt");
BufferedOutputStream bos =
new BufferedOutputStream(foStream);
int d = -
1;
while((d = bis.read()) != -
1) {
bos.write(d);
}
System.out.println(
"复制完毕");
bos.flush();
bis.close();
bos.close();
}
}
五、对象流
5.1 对象序列化
将一个对象转换为一组字节的过程
5.2 对象持久化
将该对象的字节写入到硬盘,做长久保存
5.3 反序列化
/**
* 这个类主要讲了对象流的用法
* 对象流的作用是方便读写java中的对象
*
* 对象输出流:
* 可以将给定的对象转换为一组字节后写出
* 对象输入流:
*
*
* @author Gupan
*
*/
public class ObjectStreamDemo implements Serializable{
/**
*
*/
private static final long serialVersionUID =
1L;
private String name;
private int age;
private String gender;
private List <String> otherInfo;
public ObjectStreamDemo() {
}
public ObjectStreamDemo(String name,
int age, String gender,List <String> otherInfo) {
this.name = name;
this.age = age;
this.gender = gender;
this.otherInfo = otherInfo;
}
@Override
public String
toString() {
return this.name +
"," +
this.age +
"," +
this.gender +
"," +
this.otherInfo;
}
public String
getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(
int age) {
this.age = age;
}
public String
getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public List<String>
getOtherInfo() {
return otherInfo;
}
public void setOtherInfo(List<String> otherInfo) {
this.otherInfo = otherInfo;
}
public static void main(String[] args)
throws IOException, ClassNotFoundException {
ObjectStreamDemo p =
new ObjectStreamDemo();
p.setName(
"圆滚滚");
p.setAge(
18);
p.setGender(
"男");
List <String> otherInfo =
new ArrayList<String> ();
otherInfo.add(
"是一个学生");
otherInfo.add(
"2018年毕业");
p.setOtherInfo(otherInfo);
FileOutputStream fos =
new FileOutputStream(
"info.txt");
ObjectOutputStream oos =
new ObjectOutputStream(fos);
oos.writeObject(p);
oos.flush();
oos.close();
FileInputStream fiStream =
new FileInputStream(
"info.txt");
ObjectInputStream ois =
new ObjectInputStream(fiStream);
ObjectStreamDemo pRe = (ObjectStreamDemo) ois.readObject();
System.out.println(pRe);
ois.close();
}
}
六、 Reader和Writer
字符输入流和字符输出流的顶级父类,字符流是以字符为单位读写数据的。一次处理一个unicode。字符流的底层仍然是基本的字节流,所有的字符流都是高级流
6.1 Reader的常用方法
int read()
读取一个字符,返回的int值“低16”位有效
int read(char[] chs) 从该流中读取一个字符数组的length个字符并存入该数组,返回值为实际读取到的字符量
6.2 Writer的常用方法
void write(int c)
写出一个字符,写出给定int值“低16位”表示的字符
void write(char [] chs) 将给定的字符数组中所有字符写出void write(String str) 将给定的字符串写出void write(char[] chs, int offset, int len) 将给定的字符数组从offset出开始连续的len铬字符写出
6.3 常用的Reader和Writer
完成字符流向字节流的转换,通过这两个字符流转换以后,其他的字符流才可以处理 1. InputStreamReader 代表字符输入流,可以按照指定的字符集从流中按照编码将字节数据转换为字符并读取 2. OutputStreamWriter 代表字符输出流,使用该流可以设置字符集,并按照指定的字符集将字符转换为对应的字节后用过该流写出
public class ReaderAndWriterDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos =
new FileOutputStream(
"osw.txt");
OutputStreamWriter osw =
new OutputStreamWriter(fos,
"utf-8");
osw.write(
"圆滚滚在向前滚");
osw.close();
FileInputStream fis =
new FileInputStream(
"osw.txt");
InputStreamReader isr =
new InputStreamReader(fis,
"utf-8");
int d = -
1;
while ((d = isr.read()) != -
1) {
System.
out.print((
char)d);
}
}
}
6.3.2 BufferedWriter和BufferedReader
特点
按行读写字符串
6.3.3 PrintWriter
PrintWriter
具有自动行刷新缓冲字符输出流,在创建时,他一定会在内存创建BufferedWriter作为缓冲功能的叠加
/**
* 这个类主要讲了缓冲字符流的用法
*
* @author Think
*
*/
public class PrintWriterDemo {
public static void main(String[] args)
throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter pw =
new PrintWriter(
"pw.txt",
"utf-8");
pw.println(
"圆滚滚在向前滚");
pw.println(
"滚得越来越远了");
pw.close();
FileOutputStream fos =
new FileOutputStream(
"pw.txt",
true);
pw =
new PrintWriter(fos);
pw.println(
"滚得又远了一点");
pw.close();
}
}