Advanced application IO streams

 

IO advanced application flow:

BufferedReader:

Read from the character in the input text stream, buffering characters, enabling efficient reading of characters, lines and arrays, the size of the buffer can be specified, or a default size may be used. In most cases, the default value is large enough, the default buffer size is 8K, if the default buffer size is insufficient to meet demand can customize the size of the buffer.

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: Writes text character output stream, buffering characters so as to provide efficient writing of single characters, strings and arrays. You can specify the size of the buffer, or accept the default size. In most cases, the default value is large enough.

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("写入完成!");
    }
}

Exercise: BufferedReader and BufferedWriter achieve copying files.

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 is a bridge character streams to byte streams: using a specified charset character written to the stream into byte code. It uses the character set can be specified by name or explicitly given, otherwise it will accept the platform's default character set (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 code table can be specified using the data stored in the saving data.

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 byte character stream to flow bridge: it using a specified charset reads bytes and decodes them into characters. It uses the character set may be specified by the name or given explicitly, or may accept the platform's default character set.

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();
    }   
}

When reading the file can also be provided for decoding code table.

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();
    }   
}

File specifies the code table and buffer copy:

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:

Allows the application to output data streams in a suitable manner basic Java data types written to the output stream. Then, the application can use the data read into the input data stream.

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:

It allows an application to the data input stream to read the underlying basic Java data types from the input stream in a machine-independent manner. An application can use the data output stream stream data is written from the input data read later.

DataInputStream and can be used in conjunction 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 stream processing flow:

Process Flow is packed and one of the original decoration IO streams flow, hides the differences in the underlying implementation of the original stream. So that the read data is more convenient.

处理流有以下几类:

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();
    }
}

Guess you like

Origin www.cnblogs.com/mxybk/p/11347785.html