IO节点流

 

InputStream

public abstract int read():返回字符对应的相应字符集中的编码,读到文件末尾返回-1

public int read(byte[] b):返回数组中已经读取到的字符的个数

 

FileInputStream

读取项目下的文件:read()

    private static void demo1() {
        //演示read():每次读取一个字节
        File file = new File("demo.txt") ;
        InputStream is = null ;
        try {
            is = new FileInputStream(file) ;
            int data ;
            while((data = is.read())!=-1) {
                System.out.print((char)data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
View Code

 

读取项目下的文件:read(byte[] data)

    private static void demo2() {
        //演示read(byte[] flush):将数据读到数组(缓存)中,提供读取效率
        File file = new File("demo.txt") ;
        InputStream is = null ;
        try {
            is = new FileInputStream(file) ;
            byte[] flush = new byte[1024] ;
            int len ;
            while((len = is.read(flush))!=-1) {
                String data = new String(flush, 0, len) ;
                System.out.println(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
View Code

 

 

OutputStream

FileOutputStream

将一个字符串写入一个文件:public void write(byte b[], int off, int len)

    private static void demo3() {
        //将一个字符串写入一个文件
        File file = new File("dest.txt");
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
            String msg = "io is base in java";
            byte[] data = msg.getBytes();
            os.write(data, 0, data.length);
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
View Code

 

文件内容追加:FileOutputStream(file, true)

    private static void demo4() {
        //文件内容追加
        File file = new File("dest.txt") ;
        OutputStream os = null ;
        try {
            os = new FileOutputStream(file, true) ;//文件内容追加模式
            String msg = "\t!hello world!" ;
            byte[] data = msg.getBytes() ;
            os.write(data, 0, data.length);
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
View Code

 

 

文件拷贝

    public static void copyFile(String srcPath,String destPath) {
        File src = new File(srcPath) ;
        File dest = new File(destPath) ;
        InputStream is = null ;
        OutputStream os = null ;
        try {
            is = new FileInputStream(src) ;
            os = new FileOutputStream(dest) ;
            byte[] flush = new byte[1024] ;
            int len ;
            while((len=is.read(flush)) != -1) {
                os.write(flush, 0, len);
                os.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
View Code

 

文件夹拷贝

IO流关闭技巧:先打开的后关闭,一般是输入流先打开,所以输入流一般最后关闭

 

Reader

FileReader

读取文件内容:FileReader的read(char[] flush)

    private static void demo1() {
        //使用FileReader的read(char[] flush):读取文件内容
        File file = new File("demo.txt") ;
        Reader reader = null ;
        try {
            reader = new FileReader(file) ;
            char[] flush = new char[1024] ;
            int len ;
            while((len=reader.read(flush))!=-1) {
                String data = new String(flush, 0, len) ;
                System.out.println(data);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
View Code

 

 

Writer

FileWriter

写入文件:方式一:使用FileWriter的write(String s)

    private static void demo2() {
        //方式一:使用FileWriter的write(String s):写入文件
        File file = new File("dest.txt") ;
        Writer writer = null ;
        try {
            writer = new FileWriter(file) ;
            String msg = "你好,世界!" ;
            writer.write(msg);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
View Code

 

 

写入文件:方式二:使用FileWriter的append(String s)

    private static void demo3() {
        //方式二:使用FileWriter的append(String s):写入文件
        File file = new File("dest.txt") ;
        Writer writer = null ;
        try {
            writer = new FileWriter(file) ;
            writer.append("你好,").append("时间!") ;
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
View Code

 

 

写入文件:方式三:使用FileWriter的write(char[] data,int off,int len)

    private static void demo4() {
        //方式三:使用FileWriter的write(char[] data,int off,int len):写入文件
        File file = new File("dest.txt") ;
        Writer writer = null ;
        try {
            writer = new FileWriter(file) ;
            String msg = "你好,未来!" ;
            char[] data = msg.toCharArray() ;
            writer.write(data, 0, data.length);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
View Code

 

 

字符流:文件拷贝

    public static void copyFile(String srcPath,String destPath) {
        File src = new File(srcPath) ;
        File dest = new File(destPath) ;
        Reader reader = null ;
        Writer writer = null ;
        try {
            reader = new FileReader(src) ;
            writer = new FileWriter(dest) ;
            int len ;
            char[] flush = new char[1024] ;
            while((len=reader.read(flush)) != -1) {
                writer.write(flush, 0, len);
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
View Code

 

 

字节数组流

ByteArrayInputStream读取字节数组中的数据

    private static void demo1() {
        //ByteArrayInputStream读取字节数组中的数据
        byte[] data = "hello world,未来!".getBytes() ;
        InputStream is = null ;
        try {
            is = new ByteArrayInputStream(data) ;
            int len ;
            byte[] flush = new byte[1024] ;
            while((len=is.read(flush)) != -1) {
                String s = new String(flush, 0, len) ;
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
View Code

 

 

ByteArrayOutputStream将数据写入内存(数组)

    private static void demo2() {
        //ByteArrayOutputStream将数据写入内存(数组)
        ByteArrayOutputStream baos = null ;
        byte[] dest ;
        try {
            baos = new ByteArrayOutputStream() ;
            String s = "one year after,未来!" ;
            byte[] data = s.getBytes() ;
            //将数据写入内存中
            baos.write(data, 0, data.length);
            baos.flush();
            //获取数据
            dest = baos.toByteArray() ;
            System.out.println("length:"+dest.length);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
View Code

 

 

将图片读取到字节数组,然后将字节数组写到文件

    public static void main(String[] args) {
        byte[] data = fileToByte("1.png") ;
        System.out.println(data.length);
        byteToFile(data, "copy-byte.png");
    }
    
    public static byte[] fileToByte(String filePath) {
        //读取文件,利用ByteArrayOutputStream将数据写入到内存(数组)
        File file = new File(filePath) ;
        byte[] dest = null ;
        InputStream is = null ;
        ByteArrayOutputStream baos = null ;
        int len ;
        byte[] flush = new byte[1024] ;
        try {
            is = new FileInputStream(file) ;
            baos = new ByteArrayOutputStream() ;
            while((len=is.read(flush)) != -1) {
                baos.write(flush, 0, len);
            }
            baos.flush();
            return dest = baos.toByteArray() ;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return dest ; 
    }

    
    public static void byteToFile(byte[] src,String filePath) {
        File file = new File(filePath) ;
        InputStream is = null ;
        OutputStream os = null ;
        int len = -1 ;
        byte[] flush = new byte[1024] ;
        try {
            is = new ByteArrayInputStream(src) ;
            os = new FileOutputStream(file) ;
            while((len = is.read(flush)) != -1) {
                os.write(flush, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
View Code

 

 

 

 

zwy

posted @ 2019-02-16 20:16  zwyk  阅读(122)  评论(0)    收藏  举报