zoukankan      html  css  js  c++  java
  • IO流的高级应用

    IO流的高级应用:

    BufferedReader:

    从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取, 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了,默认缓冲的大小是8K,如果默认缓冲区的大小不足以满足需求时可以自定义缓冲的大小。

    public static void main(String[] args) throws IOException {
            Reader reader=new FileReader("D:\hello.txt");
            BufferedReader br=new BufferedReader(reader);
            String line=null;
            while((line=br.readLine())!=null) {
                System.out.println(line);
            }
            reader.close();
            br.close();
        }
    

    BufferedWriter:

    BufferedWriter: 将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。 可以指定缓冲区的大小,或者接受默认的大小。在大多数情况下,默认值就足够大了。

    public class Demo2 {
        public static void main(String[] args) throws IOException {
            Writer writer=new FileWriter("D:\test.txt");
            BufferedWriter bw=new BufferedWriter(writer);
            bw.write("hello");
            bw.newLine();
            bw.flush();
            bw.write("world!");
            bw.newLine();
            bw.flush();
            writer.close();
            bw.close();
            System.out.println("写入完成!");
        }
    }
    

    练习:BufferedReader和BufferedWriter实现文件的复制。

    public static void main(String[] args) {
            BufferedReader reader=null;
            BufferedWriter writer=null;
            try {
                reader=new BufferedReader(new FileReader("D:\hello.txt"));
                writer=new BufferedWriter(new FileWriter("D:\hello2.txt"));
                String line=null;
                while((line=reader.readLine())!=null) {
                    writer.write(line);
                    writer.newLine();
                    writer.flush();
                }
                System.out.println("复制完成!");
            } catch (Exception e) {
                throw new RuntimeException("出错了!");
            }finally {
                if(reader!=null) {
                    try {
                        reader.close();
                    }catch(Exception ex) {
                        ex.printStackTrace();
                    }finally {
                        reader=null;
                    }
                }
                if(writer!=null) {
                    try {
                        writer.close();
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }finally {
                        writer=null;
                    }
                }
            }
        }
    

    OutputStreamWriter:

    OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集(gbk)。

    public class Demo3 {
        public static void main(String[] args) throws IOException {
            //也是一个装饰者 中文windows平台的默认码表是gbk
            OutputStreamWriter osw=
                    new OutputStreamWriter(new FileOutputStream("D:\test2.txt"));
            osw.write("Hello Wrold!");
            osw.flush();
            osw.close();
            System.out.println("保存成功!");
        }
    }
    

    使用OutputStreamWriter在保存数据时可以指定数据存储的码表。

    public class Demo4 {
        public static void main(String[] args) throws IOException {
            OutputStream os=new FileOutputStream("D:\test3.txt");
            OutputStreamWriter osw=new OutputStreamWriter(os, "utf-8");
            osw.write("中");
            osw.flush();
            osw.close();
            System.out.println("创建完成!");
        }
    }
    

    InputStreamReader类:

    InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。

    public class Demo5 {
        public static void main(String[] args) throws IOException {
            InputStream is=new FileInputStream("D:\test2.txt");
            //如果在创建对象的时候没有指定它的解码方式则会使用平台默认的码表(GBK)
            InputStreamReader isr=new InputStreamReader(is);
            char[] cbuf=new char[100];
            int len=-1;
            while((len=isr.read(cbuf))!=-1) {
                System.out.println(new String(cbuf,0,len));
            }
            System.out.println("读取完成!");
            is.close();
            isr.close();
        }   
    }
    

    在读取文件时也可以设置解码的码表。

    public class Demo6 {
    
        public static void main(String[] args) throws IOException {
            InputStream is=new FileInputStream("D:\test3.txt");
            //如果在创建对象的时候没有指定它的解码方式则会使用平台默认的码表(GBK)
            InputStreamReader isr=new InputStreamReader(is,"utf-8");
            char[] cbuf=new char[100];
            int len=-1;
            while((len=isr.read(cbuf))!=-1) {
                System.out.println(new String(cbuf,0,len));
            }
            System.out.println("读取完成!");
            is.close();
            isr.close();
        }   
    }
    

    指定码表和缓冲区的文件复制:

    public class Demo7 {
        public static void main(String[] args) throws IOException {
            //完成文件的复制
            BufferedReader br=null;
            BufferedWriter bw=null;     
            try {
                br=new BufferedReader(new InputStreamReader(new FileInputStream("D:\test3.txt"),"utf-8"));
                bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\test4.txt"),"utf-8"));
                String line=null;
                while((line=br.readLine())!=null) {
                    bw.write(line);
                    bw.newLine();
                    bw.flush();
                }
                System.out.println("复制完成!");
            }catch(Exception ex) {
                throw new RuntimeException();
            }finally {
                if(bw!=null) {
                    try {
                        bw.close();
                    } catch (Exception e) {
                        throw new RuntimeException();
                    }finally {
                        bw=null;
                    }
                }
                if(br!=null) {
                    try {
                        br.close();
                    } catch (Exception e) {
                        throw new RuntimeException();
                    }finally {
                        br=null;
                    }
                }
            }
        }   
    }
    

    DataOutputStream:

    数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。

    public class Demo8 {
        public static void main(String[] args) throws IOException {
            DataOutputStream dos=
                    new DataOutputStream(
                    new FileOutputStream("D:\student.txt"));
            Scanner input=new Scanner(System.in);       
            System.out.println("请输入语文成绩:");
            int chinese=input.nextInt();
            dos.writeInt(chinese);
            System.out.println("请输入数据成绩:");
            int math=input.nextInt();
            dos.writeInt(math);
            System.out.println("请输入英语成绩:");
            int english=input.nextInt();
            dos.writeInt(english);
            dos.flush();
            dos.close();
            System.out.println("保存完成");
        }
    }
    

    DataInputStream:

    数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。

    DataInputStream和DataOutputStream要能够结合使用

    public class Demo9 {
        public static void main(String[] args) throws IOException {
            DataInputStream dis=
                    new DataInputStream(
                    new FileInputStream("D:\student.txt"));
            int chinese=dis.readInt();
            System.out.println("语文成绩:"+chinese);
            int math=dis.readInt();
            System.out.println("数学成绩:"+math);
            int english=dis.readInt();
            System.out.println("英语成绩:"+english);
        }
    } 
    

    IO流中的处理流:

    处理流是对原始流进行包装和装饰的一种IO流,隐藏了原始流底层实现的差异。使得数据的读取更加方便。

    处理流有以下几类:

    1.  字符缓冲流
    BufferedReader和BufferedWriter
    BufferedInputStream和BufferedOutputStream
    2.  转换流
    InputStreamReader和OutputStreamWriter
    3.  数据流
    DataOutputStream和DataInputStream
    4.  对象流
    ObjectOutputStream和ObjectInputStream
    

    序列化:如果一个对象所属类型实现了Serializable接口,则这个对象是可以以序列化方式进行存储,以序列化方式存储的数据,才可以以反序列化方式进行读取。

    ObjectOutputStream可以支持将java对象写出到目标文件。

    public class Demo12 {
        public static void main(String[] args) throws FileNotFoundException, IOException {
            Scanner input=new Scanner(System.in);
            System.out.println("请输入学生的基本信息");
            System.out.println("姓名:");
            String name=input.next();
            System.out.println("数学:");
            int math=input.nextInt();
            System.out.println("语文:");
            int chinese=input.nextInt();
            System.out.println("英语:");
            int english=input.nextInt();
    
            Student student=new Student(name,chinese,math,english);
    
            ObjectOutputStream oos=
                    new ObjectOutputStream(
                    new FileOutputStream("D:\student.txt"));
            //以序列化方式保存对象
            oos.writeObject(student);       
            oos.flush();
            oos.close();
            System.out.println("学生信息保存完成!");
        }
    }
    

    以反序列化方式来解析对象

    public class Demo13 {
        public static void main(String[] args) throws IOException, IOException, ClassNotFoundException {
            ObjectInputStream ois=
                    new ObjectInputStream(
                    new FileInputStream("D:\student.txt"));
            Object obj=ois.readObject();
            Student s=(Student) obj;
            System.out.println("姓名:"+s.name+"语文:"+s.chinese+"数学:"+s.math+"英语:"+s.english);
            ois.close();
        }
    }
    
  • 相关阅读:
    iphone精简教程
    自己搭建云盘 – 简单的PHP网盘程序
    内存泄漏(I)
    App 基本图片配置(I)
    Git 工作环境配置
    MVC(I)
    ReactNative APP基本框架搭建 基于 React Navigation
    UI绘制原理及卡顿 掉帧原因
    ES6中Json、String、Map、Object之间的转换
    Invariant Violation: requireNativeComponent: "RNCWKWebView" was not found in the UIManager.
  • 原文地址:https://www.cnblogs.com/mxybk/p/11347785.html
Copyright © 2011-2022 走看看