一.转换流
可以查指定的编码表,进行读写
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class Kll {
public static void main(String[] args) throws IOException {
getWriteUtf8();
getWriteGBK();
getReadUtf8();// 因为读的是GBK格式的文档,所以乱码
//getReadGBK();
}
// 转换流写UTF-8格式的文件
public static void getWriteUtf8() throws IOException {
// 文件字节流创建
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/happy.txt");
// 创建转换流
// 使用操作平台的默认编码格式写文件
OutputStreamWriter osw = new OutputStreamWriter(fos);
// 写
osw.write("分久必合合久必分");
// 关闭资源 规则:1.多个流嵌套(即流当参数传入)时,只需要关闭最外层的流;2.自己创建出来的流,自己关;3.从系统中获取的流,不需要自己关闭,系统自动处理.
osw.close();
}
// 转换流写GBK格式的文件
public static void getWriteGBK() throws IOException {
//
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/happynew.txt");
// 编码格式,大小写都可以
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
osw.write("凤栖梧");
osw.close();
}
// 读UTF-8 使用默认编码格式
public static void getReadUtf8() throws IOException {
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/happynew.txt");
InputStreamReader isr = new InputStreamReader(fis);
char[] c = new char[1024];
int num = 0;
while ((num = isr.read(c)) != -1) {
System.out.println(num);
String string = new String( c, 0, num);
System.out.println(string);
}
isr.close();
}
// 读GBK
public static void getReadGBK() throws IOException {
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/happy.txt");
InputStreamReader isr = new InputStreamReader(fis, "GBK");
char[] b = new char[1024];
int num = 0;
while ((num = isr.read(b)) != -1) {
System.out.println(num);
String string = new String( b, 0, num);
System.out.println(string);
}
isr.close();
}
}
二.高效流(缓冲流)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Kll {
public static void main(String[] args) throws IOException {
//fun1();
//fun2();
// 模板设计模式,测试
}
// 用字节流复制文件方法
private static void fun2() throws FileNotFoundException, IOException {
// 缓冲流读取
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/happynew.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] bs = new byte[1024];
int len = 0;
while ((len = bis.read(bs)) != -1) {
System.out.println(new String(bs, 0 , len));
}
bis.close();
}
private static void fun1() throws FileNotFoundException, IOException {
// 创建字节输入流
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/happynew.txt");
// 创建一个缓冲流
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 写入
bos.write("kll".getBytes());
// 关闭资源
bos.close();
}
}
流的总结:
- 字节流 字符流 转换流 高效流
- 明确操作的文件是源文件还是目标文件?
- 源文件 读文件 输入流 InputStream Reader
- 目标文件 写文件 输出流 OutputStream Write
- 明确你要操作是什么文件?
- 文本类 字符流
- 音频视频图片类 字节流
- 明确你要操作的文件在什么位置?
- 硬盘(现学的)
- 网络
- 明确你要使用什么特殊功能?
- 转换流(按码表读写)
- 高效流(读写快)
- 对象流(将对象持久化)
- 打印流(原样输出)
三.高效字节流
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* 高效字符流
* BufferedWrite
* BufferedReader
*/
public class Demo04 {
public static void main(String[] args) throws IOException {
// fun1();
// fun2();
// 使用高效字符流复制txt文件
// 创建字符流输出流
FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test11/endsd.txt");
// 创建高效字节流输出流
BufferedWriter bw = new BufferedWriter(fw);
// 读 输入流
FileReader fr = new FileReader("/Users/lanou/Desktop/Test11/end.txt");
// 高效字节流
BufferedReader br = new BufferedReader(fr);
String string = "";
while ((string = br.readLine()) != null) {
bw.write(string);
// 注意:复制文件时,需要加入换行
// 复制文件会多一个换行
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
private static void fun2() throws FileNotFoundException, IOException {
// 读 输入流
FileReader fr = new FileReader("/Users/lanou/Desktop/Test11/end.txt");
// 高效字节流
BufferedReader br = new BufferedReader(fr);
String line = null;
// 读取一行字符串方法,读到末尾,返回null
while ((line = br.readLine()) != null) {
// 注意:该方法不能读取到换行符
System.out.println(line);
}
br.close();
}
private static void fun1() throws IOException {
// 创建字符流输出流
FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test11/end.txt");
// 创建高效字节流输出流
BufferedWriter bw = new BufferedWriter(fw);
// 写
bw.write("牧童遥指杏花村");
// 换行
bw.newLine();
bw.write("相面不相识");
bw.newLine();
// 刷新
bw.flush();
// 关闭
bw.close();
}
}
四.对象流
- 序列化流
- 将对象直接写入到文件中
- ObjectOutputStream
反序列化流
- ObjectInputStream
阻止对象的成员变量进行序列化的方法
- 1.静态变量,不会被写入文件
- 2.使用关键字Transient(瞬态) 标识成员变量
- 解决序列化ID冲突的方案
- 将序列化号写死
private static final long serialVersionUID = -250634430103989467L;
对象类:
import java.io.Serializable;
public class Person implements Serializable{
/**
* 解决序列化ID冲突的方案
* 将序列化号写死
*
*/
private static final long serialVersionUID = -250634430103989467L;
/*
* 阻止对象的成员变量进行序列化
* 1.静态变量,不会被写入文件
* 2.使用关键字Transient(瞬态) 标识成员变量
*/
private String name;
private int age;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "[name=" + name + ", age=" + age + "]";
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Demo07 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// dcom.lanou3g.Person;
//local class incompatible:
// stream classdesc serialVersionUID = -250634430103989467,
// local class serialVersionUID = 570644891644424717
// 本地的序列化ID和要读取的文件的序列化ID不一致
//writerObject();
readObject();
}
public static void readObject() throws FileNotFoundException, IOException, ClassNotFoundException {
// 读取文件
FileInputStream fis1 = new FileInputStream("/Users/lanou/Desktop/Test11/person.txt");
ObjectInputStream ois = new ObjectInputStream(fis1);
// ClassNotFoundException 找不到这个类
// 将文件中的对象进行反序列化需要依赖存入对象的.class文件
Object object = ois.readObject();
object = ois.readObject();
System.out.println(object);
ois.close();
}
public static void writerObject() throws FileNotFoundException, IOException {
// 将对象写入文件
FileOutputStream fos1 = new FileOutputStream("/Users/lanou/Desktop/Test11/person.txt");
ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
// 写入
// NotSerializableException 要保存的对象的类未实现序列化接口Serialzable.
// Serialzable 序列化接口是一个标识性接口
// 标识这个类的对象 可以进行序列化
oos1.writeObject(new Person("孔令蕾", 25));
//oos1.writeObject(new Person("kll", 12));
// 关闭流
oos1.close();
}
}
五.properties双列集合
Properties(双列集合,是HashTable的子类,.没有泛型)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class Demo06 {
public static void main(String[] args) throws IOException {
//fun1();
// 将集合写入文件
FileOutputStream fs = new FileOutputStream("/Users/lanou/Desktop/Test11/newwl.properties");
Properties p = new Properties();
// 保存键值对
p.setProperty("kll", "name");
p.setProperty("23", "age");
p.setProperty("nv", "gender");
// 写入到文件中
// 参数2:传入的注释(推荐英文)
p.store(fs, "fb");
fs.close();
}
private static void fun1() throws FileNotFoundException, IOException {
// 将文件直接读入集合中
FileInputStream fs = new FileInputStream("/Users/lanou/Desktop/Test11/wl.properties");
// 创建集合
Properties pt = new Properties();
// 直接调用加载文件的方法
pt.load(fs);
// 遍历集合,打印
/*for (Object obj : pt.keySet()) {
System.out.println((String)obj + " " + pt.getProperty((String)obj));
}*/
// 迭代器遍历
// 获取迭代器
Enumeration<?> na = pt.propertyNames();
while (na.hasMoreElements()) {
// 取出每一个元素
String key = (String)na.nextElement();
System.out.println(key + "=" + pt.getProperty(key));
}
fs.close();
}
}
六.流的自动关闭
import java.io.FileReader;
import java.io.InputStreamReader;
/*
* AutoCloseable 所有流都实现了该接口
* jdk10.7 流的自动关闭
*/
class MyClose implements AutoCloseable{
@Override
public void close() throws Exception {
System.out.println("被调用");
}
}
public class Demo05 {
public static void main(String[] args) {
try (
//MyClose myClose = new MyClose();
// 流都实现了AutoCloseable这个接口,当程序结束时,该流对象也会调用自己本类中重写的close方法
// 注意:不实现该接口,不能在该小括号内使用.
FileReader fr = new FileReader("");
){
} catch (Exception e) {
// TODO: handle exception
}
}
}