JavaFile I/O流

Java 流(Stream)、文件(File)和IO

Java.io 包几乎包含了所有操作输入、输出需要的类。所有这些流类代表了输入源和输出目标。

Java.io 包中的流支持很多种格式,比如:基本类型、对象、本地化字符集等等。

一个流可以理解为一个数据的序列。输入流表示从一个源读取数据,输出流表示向一个目标写数据。

Java 为 I/O 提供了强大的而灵活的支持,使其更广泛地应用到文件传输和网络编程中。

读写文件

如前所述,一个流被定义为一个数据序列。输入流用于从源读取数据,输出流用于向目标写数据。

下图是一个描述输入流和输出流的类层次图。

/**
         * 第一套:字节流,实现对字节 数据 的读取操作
         */
        InputStream fis =null;
        try {
             fis = new FileInputStream("d:/我的青春我做主.txt");
        //实现读取操作
        int data;//储存读到的字节
        while ((data=fis.read())!=-1) {
            System.out.print((char)data);
        }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally {        
                try {
                        if (fis!=null) {    
                        fis.close();
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }            
        }
/**
         * 实现对字节的写入内存的操作
         */
        //1.创建字节输出流
         OutputStream fis = null;
         try {
            fis = new FileOutputStream(new File("d:/Hello.txt"));
            String str = "呵呵呵哒";
            byte[] words = str.getBytes();
            fis.write(words, 0, words.length);
            System.out.println("写入成功!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fis!=null) {
                    
                    fis.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
/**
         * FileReader和BufferReader读取文件
         * 字符编码:ASCII码,0~127,8位二进制数01010101,一个字节,
         * 16位二进制数 0101010101010101 0~65535
         * Unicode ,采用字节流读取中文信息会乱码,所以采用字符字符流来读取中文信息。
         * 第二套:字符流对文件实现读取和写入的操作
         * 1.输入流
         * 基类:Reader
         * FileReader
         * 构造
         * FileReader(File file),(String name)
         * 常用方法:
         * int reader().读取一个字符,返回字符编码
         * int reader(char[] b,off,length)
         * 
         */
        //1.创建一个字符流对象,读取文本文件
        Reader frReader = null;
        StringBuffer frBuffer =null;
        try {
            frReader = new FileReader("d:/我的青春我做主.txt");
            //2.读取文本文件
            /*int word;
            while ((word=frReader.read())!=-1) {
                System.out.print((char)word);
                
            }*/
            char[] words = new char[1024];//存储读取到的字符
             frBuffer = new StringBuffer();
            int len =frReader.read(words);//读取到字符数组中
            while (len!=-1) {
                frBuffer.append(words);
                len = frReader.read(words);
                
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (frReader!=null) {    
                    frReader.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println(frBuffer.toString());
        
        /**
         * 字符流,写入操作
         */
        FileWriter fwFileWriter = null;
        try {
            //写入文件,可追加
            fwFileWriter = new FileWriter("d:NIO.txt",true);
            fwFileWriter.write("lalallal======llala");
        } catch (IOException e1) {            
            e1.printStackTrace();
        }finally{        
            try {
                fwFileWriter.close();
            } catch (IOException e) {                
                e.printStackTrace();
            }
        }
/**
         * 第三套:使用BufferReader,单缓冲区的数据读取和写入(字符输入流)
         * 读取
         */
        FileReader frFileReader = null;
        BufferedReader bWriter = null;
        try {
            frFileReader = new FileReader("D:NIO.txt");
            bWriter = new BufferedReader(frFileReader);
            //读取一行数据
            String Line = bWriter.readLine();
            while(Line!=null){
                System.out.println(Line);
                Line=bWriter.readLine();
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bWriter.close();
                frFileReader.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        /**
         * 写入
         */
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw= new FileWriter("D:AI.txt",true);
            bw = new BufferedWriter(fw);
            bw.write("故乡的偶遇缘分");
            bw.newLine();
            bw.write("lalal");
            bw.flush();//刷新
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (bw!=null) {
                    
                    bw.close();
                }
            } catch (IOException e) {
                
                e.printStackTrace();
            }
        }
/**
         * 第四套:用字节流类DataOutputStream写二进制文件,之前只能读取文本文件,写入图片和视频甚至压缩文件和文档
         * DataInputStream读取
         */
        DataInputStream dis =null;
        OutputStream os=null;
         DataOutputStream ooStream=null;
         InputStream is=null;
        try {
            //读取视频文件的路径
            is = new FileInputStream("D:\\英雄时刻\1424719291.1.avi");
            //copy任何的文件 
             dis = new DataInputStream(is);
             os = new FileOutputStream("E:\\英雄时刻\1424719291.1.avi");
             ooStream = new DataOutputStream(os);
            byte[] bytes = new byte[1024];//一次转1k的数据
            int data=0;
            try {
                while((data=dis.read(bytes))!=-1){
                    ooStream.write(bytes,0,data);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                ooStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                dis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
/**
         * 序列化和反序列化
         * 序列化是将对象状态转换为可保持或传输的格式的过程。
         * 与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。*
         * 
         * 序列化
         */
        List<Dog> list = new ArrayList<Dog>();
        list.add(new Dog("鲍鱼",18));
        list.add(new Dog("鲤鱼",16));
        try {
            FileOutputStream fos = new FileOutputStream("save.bin");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(list);
            fos.close();
            oos.close();
            System.out.println("序列化成功!");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(IOException e1){
            e1.printStackTrace();
        }
        /**
         * 反序列化
         */
        FileInputStream fis = null;
        ObjectInputStream oiStream=null;
        try {
            fis = new FileInputStream("save.bin");
             oiStream=new ObjectInputStream(fis);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        try {
            List<Dog> list2 = (List<Dog>)oiStream.readObject();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (Dog dog: list) {
            System.out.println(dog.getName());
        }

猜你喜欢

转载自www.cnblogs.com/java-123/p/9067809.html