zoukankan      html  css  js  c++  java
  • io

    字节流: FileOutputStream

    public static void main(String[] args) throws IOException {
            
            //输入输出是相对程序来说 第二个参数为true是追加,不给或false是重新写
            FileOutputStream fos = new FileOutputStream(new File("a.txt"),true);
            fos.write("helloword".getBytes());
            fos.flush();
            fos.close();
        }

    FileInputStream:

    FileInputStream fin = new FileInputStream("a.txt");
            byte[] bs = new byte[1024];
            fin.read(bs);
            //关闭流
            fin.close();
            //创建一个字符串输出字节数组
            System.out.println(new String(bs));
        }

    字符流;

    public static void main(String[] args) {
            //创建读取字符流
            FileReader fr = null;
            try {
                fr = new FileReader("aa.txt");
                char[] cs = new char[1024];
                fr.read(cs);
                System.out.println((cs));
            } catch (IOException e) {
                    e.printStackTrace();
            }finally {
                if(fr != null){
                    try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    FileWriter fw = null;
            try {
                fw = new FileWriter(new File("aa.txt"));
                fw.write("Helloword this is");
                fw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(fw != null){
                    try {
                        fw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            

    文件拷贝:

    public static void main(String[] args) {
            InputStream is = null;
            OutputStream os = null;
            
            try {
                is = new FileInputStream("ting.jpg");
                os = new FileOutputStream("thingcop.jpg");
                /*//获得文件总共长度
                byte[] count = new byte[is.available()];
                is.read(count);
                os.write(count);
                os.flush();*/
                
                //一个一个字节拷贝
                int count;
                while((count = is.read()) != -1){
                    os.write(count);
                    os.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(is != null){
                        is.close();
                    }
                    if(os != null){
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

    高效缓冲字节流:

    public static void main(String[] args) {
            
            BufferedOutputStream bos = null;
            OutputStream os = null;
            
            try {
                os = new FileOutputStream("a.txt");
                bos = new BufferedOutputStream(os);
                bos.write("门前大桥下,啦啦啦啦".getBytes());
                bos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(bos != null){
                        bos.close();
                    }
                    if(os != null){
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    ublic static void main(String[] args) {
            
            //创建高效缓冲区流
            BufferedInputStream bis = null;
            FileInputStream fis = null;
            
            try {
                fis = new FileInputStream("ting.jpg");
                bis = new BufferedInputStream(fis);
                byte[] count = new byte[bis.available()];
                bis.read(count);
                System.out.println(new String(count));
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                    try {
                        if(bis != null){
                        bis.close();
                        }
                        if(fis != null){
                            fis.close();
                        }
                        }catch (IOException e) {
                        e.printStackTrace();
            
                    }
            }
  • 相关阅读:
    Github 上 36 个最实用的 Vue 开源库
    C 语言快速入门,21 个小项目足矣!「不走弯路就是捷径」
    18个挑战项目带你快速入门深度学习
    Linux 运维入门到跑路书单推荐
    Python 网络爬虫的常用库汇总
    45 个常用Linux 命令,让你轻松玩转Linux!
    [新手必备]Python 基础入门必学知识点笔记
    快速入门 Python 数据分析实用指南
    18位不重复订单号
    相对路径转绝对路径
  • 原文地址:https://www.cnblogs.com/miaomeng/p/8779207.html
Copyright © 2011-2022 走看看