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();
            
                    }
            }
  • 相关阅读:
    [Leetcode] Convert Sorted List to Binary Search Tree
    [Leetcode] Sqrt(x)
    [Leetcode] Pow(x, n)
    [Leetcode] Balanced Binary Tree
    [Leetcode] Convert Sorted Array to Binary Search Tree
    [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
    [Leetcode] Remove Element
    [Leetcode] Letter Combinations of a Phone Number
    [Leetcode] Generate Parentheses
    [Leetcode] Valid Parentheses
  • 原文地址:https://www.cnblogs.com/miaomeng/p/8779207.html
Copyright © 2011-2022 走看看