• 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();
            
                    }
            }
  • 相关阅读:
    【转载】LTE中RB、RE、CP、REG、CCE、子载波
    LTE中,DCI和UCI为什么要定义那么多格式
    LTE中的PDCCH介绍
    ARQ
    (转)MYSQL远程登录权限设置
    (转)忘记wamp-mysql数据库root用户密码重置方法
    phpwind部署问题
    在aliyun遇到一些问题
    (转)PHP5使用cookie时报错 cannot modify header information
    (转)WAMP多站点配置
  • 原文地址:https://www.cnblogs.com/miaomeng/p/8779207.html
Copyright © 2020-2023  润新知