节点流和处理流
-
节点流可以从一个特定的数据源读写数据,如FileReader,FileWtriter
- 底层流,直接跟数据源相接
-
处理流(包装流)是“连接”在已存在的流(节点流或处理流)之上,为程序提供更为强大的读写功能,也更加灵活,如BufferReader,BufferWriter
-
包装节点流,既可以消除不同节点流的实现差异,也可以提供更方便的方法来完成输入输出
-
使用了修饰器设计模式,不会直接与数据源相连
性能的提高:主要以增加缓冲的方式来提高输入输出的效率
操作的便捷:处理流可能提供了一系列便捷的方法来一次输入输出大批量的数据,使用更加灵活方便
-
处理流
- BufferReader和BufferWriter
/**
*使用BufferReader读取文本文件,并显示在控制台上
*/
public class BufferedReader_ {
public static void main(String[] args) throws Exception {
String filePath = "e:\\a.java";
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferReader.close();
}
}
/**
*使用BufferWriter将“你好呀”,写进文件中
*/
public class BufferedWriter_ {
public static void main(String[] args) throws IOException {
String filePath = "e:\\ok.txt";
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
bufferedWriter.write("hello!");
bufferedWriter.newLine();
bufferedWriter.write("hello2!");
bufferedWriter.newLine();
bufferedWriter.write("hello3!");
bufferedWriter.newLine();
bufferedWriter.close();
}
}
/**
*综合使用BufferReader和BufferWriter完成文本文件拷贝
*/
public calss BufferedCopy_{
public static void main(String[] args) {
String srcFilePath = "e:\\a.java";
String destFilePath = "e:\\a2.java";
BufferedReader br = null;
BufferedWriter bw = null;
String line;
try {
br = new BufferedReader(new FileReader(srcFilePath));
bw = new BufferedWriter(new FileWriter(destFilePath));
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
System.out.println("拷贝完毕...");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null) {
br.close();
}
if(bw != null) {
bw.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
-
BufferedInputStream和BufferedOutputStream
-
BufferedInputStream
字节流,在创建BufferedInptStream时,会创建一个内部缓冲区数组
-
BufferedOutputStream
字节流,实现缓冲的输出流,可以将多个字节写入底层输出流中,而不必对每次字节写入调用层系统
/** *使用BufferedInputStream和BufferedOutputStream *使用他们,完成二进制文件拷贝 */ public class BufferedCopy02 { public static void main(String[] args) { String srcFilePath = "e:\\a.java"; String destFilePath = "e:\\a3.java"; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(new FileInputStream(srcFilePath)); bos = new BufferedOutputStream(new FileOutputStream(destFilePath)); byte[] buff = new byte[1024]; int readLen = 0; while ((readLen = bis.read(buff)) != -1) { bos.write(buff, 0, readLen); } System.out.println("文件拷贝完毕~~~"); } catch (IOException e) { e.printStackTrace(); } finally { try { if(bis != null) { bis.close(); } if(bos != null) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
-
-
对象流ObjectInputStream和ObjectOutputStream
-
序列化:在保存数据时,保存数据的值和数据类型(ObjectOutputStream提供)
-
反序列化:恢复数据时,恢复数据的值和数据类型(ObjectInputStream提供)
-
让某个对象支持序列化机制必须使其可序列化,则该类必须满足:
Serializable //这是一个标记接口,没有方法,一般用这个
Externnalizable //该接口有方法需要实现
/** *使用ObjectOutputStream完成序列化 */ public class ObjectOutStream_ { public static void main(String[] args) throws Exception { String filePath = "e:\\data.dat"; ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath)); oos.writeInt(100); oos.writeBoolean(true); oos.writeChar('a'); oos.writeDouble(9.5); oos.writeUTF("你好呀"); oos.writeObject(new Dog("抹茶", 10, "中国", "白色")); oos.close(); System.out.println("数据保存完毕(序列化形式)"); } }
/** *使用ObjectInputStream读取data.dat并反序列化恢复数据 */ ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\\data.dat")); System.out.println(ois.readInt()); System.out.println(ois.readBoolean()); System.out.println(ois.readChar()); System.out.println(ois.readDouble()); System.out.println(ois.readUTF()); System.out.println(ois.readObject()); System.out.println(ois.readObject()); System.out.println(ois.readObject()); ois.close(); System.out.println("以反序列化的方式读取(恢复)ok~");
-
注意事项和细节说明:
- 书写顺序要一致
- 要求序列化或反序列化对象,需要 实现 Serializable
- 序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性
- 序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员
- 序列化对象时,要求里面属性的类型也需要实现序列化接口
- 序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实现了序列化
-
标准输入输出流
-
System.in标准输入 类型:InputStream
-
System.out标准输出 类型:PrintSteam
传统方法System.out.println(“”);是使用了out对象将数据输出到显示器
传统方法Scanner是从标准输入键盘接收数据
-
-
转换流InputStreamReader和OutputStreamWriter
1.InputStreamReader:Reader的子类,可以将InputStream(字节流)包
装成(转换)Reader(字符流)
2.OutputStreamWriter:Writer的子类,实现将OutputStream(字节流)
包装成Writer(字符流)
3.当处理纯文本数据时,使用字符流效率更高,并且可以有效解决中文
问题,所以建议将字节流转换成字符流
4.可以在使用时指定编码格式(比如 utf-8,gbk,gb2312,ISO8859-1等)/** *使用InputStreamReader转换流解决中文乱码问题 *将字节流FileputStream转成字符流InputStreamReader,指定编码gbk/utf-8 */ public class InputStreamReader_ { public static void main(String[] args) throws IOException { String filePath = "e:\\a.txt"; BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "gbk")); String s = br.readLine(); System.out.println("读取内容=" + s); br.close(); } }
/** *将字节流FileOutputStream包装成字符流OutputStreamWriter将文件进行写入(按照gbk格式,可以指定其他,比如utf-8) */ OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:\\a.txt"), "gbk"); osw.write("hello~"); osw.close(); System.out.println("保存成功~");
打印流PrintStream和PrintWriter
- 打印流只有输出流,没有输入流
- PrintStream和PrintWriter
/**
*演示PrintWriter
*/
public class PrintWriter_ {
public static void main(String[] args) throws IOException {
PrintWriter printWriter = new PrintWriter(new FileWriter("e:\\f2.txt"));
import java.io.IOException;
import java.io.PrintStream;
public class PrintStream_ {
public static void main(String[] args) throws IOException {
PrintStream out = System.out;
out.print("john, hello");
out.write("你好".getBytes());
out.close();
System.setOut(new PrintStream("e:\\f1.txt"));
System.out.println("hello~");
}
}
printWriter.print("hi, 北京你好~~~~");
printWriter.close();
}
}
Properties类
-
专门用于读写配置文件的集合类
配置文件的格式:
键=值 -
注意:键值对不需要有空格,值不需要用引号括起来。默认类型是String
-
Properties的常见方法
- load:加载配置文件的键值对到Properties对象
- list:将数据显示到指定设备
- getProperty(key):根据键获取值
- setProperty(key.value):设置键值对到Properties对象
- store:将Properties中的键值对存储到配置文件,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码
- http://tool.chinaz.com/tools/unicode.aspx unicode码查询工具
public class Properties02 { public static void main(String[] args) throws IOException { Properties properties = new Properties(); properties.load(new FileReader("src\\mysql.properties")); properties.list(System.out); String user = properties.getProperty("user"); String pwd = properties.getProperty("pwd"); System.out.println("用户名=" + user); System.out.println("密码是=" + pwd); } } blic class Properties03 { public static void main(String[] args) throws IOException { Properties properties = new Properties(); Properties 父类是 Hashtable , 底层就是 Hashtable 核心方法 properties.setProperty("charset", "utf8"); properties.setProperty("user", "汤姆");//注意保存时,是中文的 unicode 码值 properties.setProperty("pwd", "888888"); properties.store(new FileOutputStream("src\\mysql2.properties"), null); System.out.println("保存配置文件成功~"); } }