1. 字符缓冲流
1.1 字符缓冲流构造方法
package com.itheima.buffered;import java.io.*;public class BufferedStreamDemo1 {/*字符缓冲流的基本使用注意: 缓冲流本身不具备读写功能, 只是对普通的流对象做包装构造方法:BufferedReader(Reader reader)BufferedWriter(Writer writer)*/public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("day14-code\\A.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("day14-code\\B.txt"));char[] chs = new char[1024];int len;while((len = br.read(chs)) != -1){bw.write(chs, 0, len);}br.close();bw.close();}private static void method() throws IOException {BufferedReader br = new BufferedReader(new FileReader("day14-code\\A.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("day14-code\\B.txt"));int i;while((i = br.read()) != -1){bw.write(i);}br.close();bw.close();}
}
1.2 字符缓冲流特有方法
package com.itheima.buffered;import java.io.*;public class BufferedStreamDemo2 {/*字符缓冲流的特有方法:public String readLine() 读取一行字符串, 读取到末尾返回 nullpublic void newLine() 写出换行符 (具有跨平台性)*/public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new FileReader("day14-code\\A.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("day14-code\\C.txt"));String line;while((line = br.readLine()) != null){bw.write(line);bw.newLine();}br.close();bw.close();}private static void method() throws IOException {BufferedReader br = new BufferedReader(new FileReader("day14-code\\A.txt"));String line;while((line = br.readLine()) != null){System.out.println(line);}br.close();}
}
1.3 案例1-文本排序
package com.itheima.buffered;import java.io.*;
import java.util.TreeSet;public class BufferedStreamTest1 {/*案例: 请对 D:\出师表.txt 文件进行排序操作*/public static void main(String[] args) throws IOException {// 创建TreeSet集合用于排序TreeSet<String> ts = new TreeSet<>();// 创建输入缓冲流用于读取源文件数据BufferedReader br = new BufferedReader(new FileReader("D:\\出师表.txt"));String len;while ((len = br.readLine()) != null){// 将读取到的每一行字符串存入集合ts.add(len);}br.close();// 创建输出缓冲流关联目标文件BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\出师表.txt"));// 遍历集合, 取出排序后的数据for (String content : ts) {// 写出数据bw.write(content);bw.newLine();}bw.close();}
}
排序前:
排序后:
1.4 案例2-读取学生信息
2. 转换流
package com.itheima.change;import java.io.*;public class ChangeStreamDemo {/*转换流按照指定的字符编码读写构造方法:InputStreamReader(InputStream in, String CharsetName)OutputStreamWriter(OutputStream in, String CharsetName)*/public static void main(String[] args) throws IOException {read();OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\Test.txt", true), "GBK");osw.write("哈哈");osw.close();System.out.println("----------------------");read();}private static void read() throws IOException {InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\Test.txt"), "gbk");int i;while ((i = isr.read()) != -1) {System.out.println((char) i);}isr.close();}}
注: 字节流转换为字符流在后面的网络编程中提到
3. 序列化流
序列化流读写对象序列化: 将对象写出到文件public ObjectOutputStream(OutputStream out)反序列化: 从文件中将对象读取到程序public ObjectInputStream(InputStream in)
3.1 序列化流的操作流程
3.2 案例-读取多个对象
方式一: 用try-catch...捕捉读流末尾异常
package com.itheima.serialization;import com.itheima.domain.Student;import java.io.*;public class SerializationTest {/*需求: 现有3个学生对象, 并将对象序列化到流中, 随后完成反序列化操作*/public static void main(String[] args) throws IOException, ClassNotFoundException {ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\D.txt"));while (true) {try {Object o = ois.readObject();System.out.println(o);} catch (EOFException e) {break;}}ois.close();}private static void writeObject() throws IOException {Student stu1 = new Student("张三", 23);Student stu2 = new Student("李四", 24);Student stu3 = new Student("王五", 25);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\D.txt"));oos.writeObject(stu1);oos.writeObject(stu2);oos.writeObject(stu3);oos.close();}
}
方式二: 用集合封装学生对象 (只读写一次集合, 避免异常处理)
package com.itheima.serialization;import com.itheima.domain.Student;import java.io.*;
import java.util.ArrayList;public class SerializationTest2 {/*需求: 现有3个学生对象, 并将对象序列化到流中, 随后完成反序列化操作*/public static void main(String[] args) throws IOException, ClassNotFoundException {readObject();}private static void readObject() throws IOException, ClassNotFoundException {ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day14-code\\stu.txt"));ArrayList<Student> list = (ArrayList<Student>) ois.readObject();for (Student stu : list) {System.out.println(stu);}ois.close();}private static void writeObject() throws IOException {Student stu1 = new Student("张三", 23);Student stu2 = new Student("李四", 24);Student stu3 = new Student("王五", 25);ArrayList<Student> list = new ArrayList<>();list.add(stu1);list.add(stu2);list.add(stu3);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day14-code\\stu.txt"));oos.writeObject(list);oos.close();}
}
4. 打印流
package com.itheima.print;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;public class PrintStreamDemo2 {/*PrintStream的基本使用1. 创建对象关联文件public PrintStream (OutputStream os)public PrintStream (File f)public PrintStream (File f, String csn)public PrintStream (String filepath)public PrintStream (String filepath, String csn)2. 写出方法write() : 写出一个字节, 不建议使用, 无法原样写入.print() : 原样写入数据, 无换行println() : 原样写入数据, 带有换行*/public static void main(String[] args) throws Exception {PrintStream ps = new PrintStream(new FileOutputStream("day14-code\\F.txt", true));ps.println("你好");ps.close();}private static void method2() throws FileNotFoundException, UnsupportedEncodingException {PrintStream ps = new PrintStream("D:\\Test.txt", "gbk");ps.println("大家好");ps.close();}private static void method1() throws FileNotFoundException {PrintStream ps = new PrintStream("day14-code\\E.txt");ps.print(97);ps.println(97);ps.println(true);ps.println(false);ps.println('a');ps.println("abc");ps.println(12.3);ps.close();}
}
package com.itheima.print;import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;public class PrintWriterDemo {/*PrintWriter的使用*/public static void main(String[] args) throws IOException {PrintWriter pw = new PrintWriter(new FileWriter("day14-code\\F.txt"), true);pw.println("你好");}
}
5. Properties 集合
5.1 Properties 作为集合使用
package com.itheima.properties;import java.util.Properties;
import java.util.Set;public class PropertiesDemo1 {/*Properties作为集合的使用Object setProperty (String key, String value) : 类似Map集合的put方法String getProperty (String key) : 类似Map集合的get方法Set<String> stringPropertyNames() : 类似Map是集合的keySet方法*/public static void main(String[] args) {Properties prop = new Properties();prop.setProperty("username", "admin");prop.setProperty("password", "1234");Set<String> keySet = prop.stringPropertyNames();for (String key : keySet) {System.out.println(key + "---" + prop.getProperty(key));}}private static void method(Properties prop) {String username = prop.getProperty("username");String password = prop.getProperty("password");System.out.println(username);System.out.println(password);System.out.println(prop);}
}
5.2 Properties 和 IO有关的方法
package com.itheima.properties;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;public class PropertiesDemo2 {/*Properties 和 IO 有关的方法void load(InputStream inStream) 从输入字节流读取属性列表(键和元素对)void load(Reader reader) 从输入字符流读取属性列表(键和元素对)void store(OutputStream out, String comments) 将集合的键值对写出到文件(字节流)void store(Writer writer, String comments) 将集合的键值对写出到文件(字符流)*/public static void main(String[] args) throws IOException {Properties prop = new Properties();FileInputStream fis = new FileInputStream("day14-code\\config.properties");prop.load(fis);fis.close();System.out.println(prop);}private static void method() throws IOException {Properties prop = new Properties();prop.setProperty("username", "admin");prop.setProperty("password", "1234");FileWriter fos = new FileWriter("day14-code\\config.properties");// 此处的comments表示注释, 以字符串类型输入, null时表不要注释prop.store(fos, null);fos.close();}
}