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();
            
                    }
            }
  • 相关阅读:
    转!!javaMail使用网易163邮箱报535 Error: authentication failed
    银行卡验证(验证是否存在,卡号类型,归属行)
    Navicat已经成功连接,密码忘记的解决方法
    Inline&IAT Hook原理
    x64dbg尝鲜
    C# 通过Dynamic访问System.Text.Json对象
    dotnet5将asp.net webapi宿主到wpf
    Asp.Net5 MVC with Vue.js
    在 Visual Studio 中使用跟踪点将信息记录到“输出”窗口中
    WPF带阴影的无边框窗体
  • 原文地址:https://www.cnblogs.com/miaomeng/p/8779207.html
Copyright © 2011-2022 走看看