java学习笔记——IO流

一、IO 流的分类:
按数据不同:字节流、字符流(字符流用于操作文本文件 .txt .java 字节流用于操作非文本文件 .avi .jpg .mp3)
按流向不同:输入流、输出流(以程序为主体)
按角色不同:节点流、处理流

二、IO 流的结构:
抽象基类                    节点流                            缓冲流(处理流的一种)
InputStream            FileInputStream              BufferedInputStream
OutputStream         FileOutputStream            BufferedOutputStream (flush()-强制清空缓冲区)
Reader                    FileReader                       BufferedReader (readLine())

Writer                      FileWriter                        BufferedWriter (newLine())

java学习笔记——IO流




    public static void fileCopy(String src, String dest){
        //1. 创建 FileInputStream 实例,同时打开指定文件
        FileInputStream fis = null;
        //2. 创建 FileOutputStream 实例,同时打开指定文件
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(src);
            
            fos = new FileOutputStream(dest);
            
            //3. 读取指定文件的内容
            byte[] b = new byte[1024];//{1-----}
            int len = 0;
            
            while((len = fis.read(b)) != -1){
                //4. 将读取的内容写到目标文件
                fos.write(b, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5. 关闭流
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    



    //使用缓冲流完成非文本文件的复制
    @Test
    public void test1(){
        //3. 创建 BufferedInputStream 实例,包装对应节点流
        BufferedInputStream bis = null;
        //4. 创建 BufferedOutputSTream 实例,包装对应节点流
        BufferedOutputStream bos = null;
        try {
            //1. 创建 FileInputStream 实例,同时打开指定文件
            FileInputStream fis = new FileInputStream("C:\\Users\\LEE\\Desktop\\1.jpg");
            
            //2. 创建 FileOutputStream 实例,同时打开指定文件
            FileOutputStream fos = new FileOutputStream("C:\\Users\\LEE\\Desktop\\2.jpg");
            
            bis = new BufferedInputStream(fis);
            
            bos = new BufferedOutputStream(fos);
            
            //5. 读取指定文件的内容
            byte[] b = new byte[1024];
            int len = 0;
            
            while((len = bis.read(b)) != -1){
                //6. 将读取的内容,写到目标文件
                bos.write(b, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //7. 关闭流
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }

 三、对象流:ObjectInputStream & ObjectOutputStream
 序列化:将内存中的对象永久的以二进制形式保存到磁盘中
 ①创建 FileOutputStream 对象,同时打开指定文件
 ②(可选)创建缓冲流对象,包装对应节点流,用于提高效率
 ③创建对象流,包装对应的缓冲流,完成序列化
 ④通过 writeXxx() 方法完成序列化操作
 ⑤关闭流
 ⑥需要序列化对象的类要实现 java.io.Serializable 接口
 ⑦提供一个*** private static final long serialVersionUID = 1243465778L;

 注意:static 和  transient 修饰的属性不能被序列化


 反序列化:将磁盘中保存的对象读取

     //对象的反序列化
    @Test
    public void test4(){
        ObjectInputStream ois = null;
        try {
            FileInputStream fis = new FileInputStream("person.txt");
            BufferedInputStream bis = new BufferedInputStream(fis);
            ois = new ObjectInputStream(bis);
            
            Person p1 = (Person) ois.readObject();
            Person p2 = (Person) ois.readObject();
            Person p3 = (Person) ois.readObject();
            
            System.out.println(p1);
            System.out.println(p2);
            System.out.println(p3);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
    
    //对象的序列化
    @Test
    public void test3(){
        Person p1 = new Person("张", 18, "哈哈");
        Person p2 = new Person("李四", 20, "哈哈");
        Person p3 = new Person("王五", 45, "哈哈");
        
        ObjectOutputStream oos = null;
        try {
            FileOutputStream fos = new FileOutputStream("person.txt");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            oos = new ObjectOutputStream(bos);
            
            oos.writeObject(p1);
            oos.writeObject(p2);
            oos.writeObject(p3);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }

 四、
 【控制台 IO】
 System.in : “标准”的输入流
 System.out : “标准”的输出流 System.setOut(PrintStream p) 改变 println() 默认的输出位置
 System.err : “标准”的错误输出流

 【打印流】PrintStream &  PrintWriter

java学习笔记——IO流java学习笔记——IO流


 五、
 【重要】
 【转换流】InputStreamReader & OutputStreamWriter
  编码:字符串 -> 字节数组
  解码:字节数组  -> 字符串
 
    //解码
    @Test
    public void test6() throws IOException{
        FileInputStream fis = new FileInputStream("hello.txt");
        InputStreamReader isr = new InputStreamReader(fis, "ISO8859-1");
        BufferedReader br = new BufferedReader(isr);
        
        String str = null;
        while((str = br.readLine()) != null){
            System.out.println("--" + str);
        }
        
        br.close();
    }
    
    //编码
    @Test
    public void test5() throws IOException{
        String str = "skldjflksdjflsdjlfjsdf中国人,哈哈哈";
        
        FileOutputStream fos = new FileOutputStream("hello.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
        BufferedWriter bw = new BufferedWriter(osw);
        
        bw.write(str);
        
        bw.close();

    }

java学习笔记——IO流


 六、其他 IO 流
 数据流: DataInputStream & DataOutputStream
 随机存取文件流:RandomAccessFile

 七、 java.io.File 类:用于描述文件或目录。可用于完成一些基本操作,如:新建、删除、重命名等。
                    若需要操作文件的内容,File 对象将无能为力。需要使用IO流。
                    因此,通常将 File 对象与 IO 流配合使用

 访问文件名:
getName()
getPath()
getAbsoluteFile()
getAbsolutePath()
getParent()
renameTo(File newName)

文件检测
exists()
canWrite()
canRead()
isFile()
isDirectory()

获取常规文件信息
lastModified()
length()

文件操作相关
createNewFile()
delete()

目录操作相关
mkDir()
mkDirs()
list()
listFiles()