Java 中经常用到的 IO 流主要有: 字节流, 字符流, 缓冲流, 打印流. 还有一个比较特殊的对象流.
IO 流分为字节流和字符流, 而字符流 = 字节流 + 编码, 也可以分为输入流和输出流 InputStream 和 OutputStream.
字节流:
InputStream: 抽象类, 通过其子类 FileInputStream, 从文件中获取字节.
OutputStream: 抽象类, 通过其子类 FileOutputStream, 向文件写入字节.
字符流:
Reader: 抽象类, 通过其子类的子类 FileReader 从文件中获取字符.
Writer: 抽象类, 通过其子类的子类 FileWriter 向文件写入字符.
缓冲流:
缓冲流也称为处理流, 对文件或者其他目标频繁的操作, 效率低, 性能差. 缓冲流目的是提高程序读取和写出的性能. 缓冲流也分为字节缓冲流和字符缓冲流. 使用缓冲流的好处是能够更高效的读写信息, 原理是先将数据缓冲起来, 然后一起写入或者读取出来.
缓冲字符流: BufferedReader,BufferedWriter
缓冲字节流: BufferedInputStream,BufferedOutputStream
- public class OutputStreamWriterDemo {
- public static void main(String[]args)throws IOException {
- // 从文件中读取字符
- BufferedReader br=new BufferedReader(new FileReader("test.txt"));
- OutputStream os=System.out;
- String line;
- while ((line=br.readLine())!=null) {
- os.write(line.getBytes());
- os.write("\r\n".getBytes());
- }
- os.close();
- br.close();
- }
- }
- public class InputStreamReaderDemo {
- public static void main(String[]args) throws IOException{
- // 创建字节流对象
- InputStream is =System.in;
- // 转换成字符流
- Reader r=new InputStreamReader(is);
- // 将字符流写入文件
- FileWriter fw=new FileWriter("a.txt");
- char[]chs=new char[1024];
- int len;
- while((len=r.read(chs))!=-1) {
- fw.write(new String(chs,0,len));
- fw.flush();
- }
- // 释放资源
- fw.close();
- is.close();
- }
- }
打印流:
打印流是输出信息最方便的类, 注意包含字节打印流 PrintStream 和字符打印流: PrintWriter. 打印流提供了非常方便的打印功能, 可以打印任何类型的数据信息, 例如: 小数, 整数, 字符串. 用字节流或者字符流输出所有的数据会非常麻烦, 要 String-->byte[], 打印流中可以方便进行输出.
- public class PrintWriteDemo {
- public static void main(String[] args) throws IOException {
- /**
- * PrintStream
- * 只能输出, 不能输入
- * PrintWrite 用 println 实现换行, 能够自动刷新.
- * PW 支持两个直接对文件写操作的构造方法:
- * PrintWriter(File f) 传文件名
- * PrintWriter(String s) 传路径
- * PrintWriter 给人一种可以直接对文件进行操作的假象
- * PW 是一个高级流
- * 实际上 PW 包装了字节流, 字符流和字符缓冲流.
- * PW 负责自动行刷新
- * bw 负责提高效率
- * osw 负责读字符
- * fos 负责写字节
- * 最后 PW 能够按行很快地把一个字符串变成字节写在文件中
- * new PrintWriter(new FileWriter("c.txt"),true) 启动了自动刷新,
- * 即不用. close() 方法, 直接释放资源
- */
- PrintWriter pw = new PrintWriter(new FileWriter("c.txt"),true);
- pw.println("飞雪连天射白鹿");
- pw.println("金庸小说我都爱看");
- //pw.close();
- PrintWriter s=new PrintWriter("d.txt");
- s.println("hello");
- s.println("world");
- s.close();
- System.out.println("写出完毕!");
- }
- }
Properties 继承于 Hashtable, 表示一个持久的属性集. 属性列表中每个键及其对应值都是一个字符串. 经常与 IO 一起联合使用.
* Properties 与 IO 流功能结合:
- * void load(Reader reader) (可以接受任何流, BufferedWriter,FileWriter,PrintWriter)
- * void list(PrintWriter out) (只能接收打印流)
- * void store(Writer writer,String comments) (comment 为非重要参数, 甚至可以用 null)
- public class PropertiesDemo {
- public static void main(String []args) {
- Properties pro=new Properties();
- pro.put("1", "zhangsan");
- pro.put("2", "lisi");
- Set<Object>keys=pro.keySet();
- for (Object key:keys) {
- String value=(String) pro.get(key);
- System.out.println(key+"="+value);
- }
- System.out.println("---------------");
- Set<Map.Entry<Object,Object>>entrys=pro.entrySet();
- for (Map.Entry<Object,Object>entry:entrys) {
- Object key=entry.getKey();
- Object value=entry.getValue();
- System.out.println(key+":"+value);
- }
- }
- }
- public class PropertiesDemo1 {
- public static void main(String[]args) throws IOException {
- //method();
- //method1();
- method2();
- }
- private static void method2() throws IOException {
- // 创建属性流对象
- Properties pro=new Properties();
- pro.put("1", "zhangsan");
- pro.put("2", "lisi");
- // 创建输出流对象
- BufferedWriter wr=new BufferedWriter(new FileWriter("h.txt"));
- //void store(wruter,writer,String comments)
- pro.store(wr, "hello world");
- // 释放资源
- wr.close();
- }
- private static void method1() throws FileNotFoundException, IOException {
- Properties pro=new Properties();
- // 创建输入流
- FileReader fr=new FileReader("g.txt");
- pro.load(fr);
- System.out.println(pro);
- fr.close();
- }
- private static void method() throws FileNotFoundException {
- Properties pro=new Properties();
- // 映射关系
- pro.put("1", "zhangsan");
- pro.put("2", "lisi");
- // 映射关系
- //pro.setProperty("1", "zhangsan");
- //pro.setProperty("2","lisi");
- PrintWriter out =new PrintWriter("g.txt");
- pro.list(out);
- out.close();
- }
- }
对象流:
Java 中可以通过对象流将一个序列化的对象保存到硬盘中, 或者硬盘中读取一个对象. 对象流的存储和读取包含以下几点内容:
1, 所保存的对象必须实现 Serializable 接口.
2, 所保存的对象的属性也必须实现 Serializable 接口.
- public class Student implements Serializable{
- private static final long serialVersionUID = 5021773103026564312L;
- String name;
- int age;
- String gender;
- public Student(String name,int age) {
- 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;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- @Override
- public String toString() {
- return "Student [name=" + name + ", age=" + age + ", gender=" + gender + "]";
- }
- }
- /* 对象操作流:
- * 可以用于读写任意类型的对象
- * ObjectOutputStream
- * writeObject
- * ObjectOutputStream(OutStream out)
- * ObjectInputStream
- * readObject
- * ObjectInputStream(InputStream in)
- * 问题: 存在 java.io.InvalidClassException, 已解决
- * 原因: 该类的序列版本号与读出的类的描述版本不同;
- * 该类包含未知的数据类型
- * 该类没有可访问的无参数构造方法
- * */
- public class ObjectOutputStreamDemo {
- public static void main(String[]args) throws IOException, ClassNotFoundException{
- method2();
- }private static void method2() throws IOException{
- // 创建字节输出流对象
- FileOutputStream fos=new FileOutputStream("newFile.txt");
- // 创建对象输出流对象, 字节输出流作为参数传入
- ObjectOutputStream oos=new ObjectOutputStream(fos);
- //FileOutputStream fos=new FileOutputStream(new FileOutputStream("newFile.txt"));
- // 创建学生对象
- Student s=new Student("zhangsan",18);
- Student s1=new Student("wangwu",19);
- // 写学生对象
- oos.writeObject(s);
- oos.writeObject(s1);
- oos.close();
- }
- }
来源: http://www.bubuko.com/infodetail-3474564.html