• 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();
            
                    }
            }
  • 相关阅读:
    Qt程序使用Win32 API发送ZPL指令与斑马打印机通信
    Eclipse构建Maven项目
    编码风格 缩进和空白
    Linux下Tomcat重新启动
    linux下tomcat服务的相关命令
    第一天
    Day1NLP_机器翻译
    Day4_attention is all you need 论文阅读下篇
    Day_7tensorflow 实战
    Day5_python学习
  • 原文地址:https://www.cnblogs.com/miaomeng/p/8779207.html
Copyright © 2020-2023  润新知