zoukankan      html  css  js  c++  java
  • [JAVA]标准IO流操作

    import java.io.*;
    
    /**
     * @Description:
     * @projectName:JavaTest
     * @see:PACKAGE_NAME
     * @author:郑晓龙
     * @createTime:2019/5/2 22:41
     * @version:1.0
     */
    public class CopyFile {
        public static void main(String[] args) {
            copy("d:/abc.txt","d:/def.txt");
        }
    
        public static void copy(String source, String target) {
            // 1、创建源
            File src = new File(source);
            File dest = new File(target);
    
            // 2、选择流
            InputStream is = null;
            OutputStream os = null;
            try {
                is = new FileInputStream(src);
                os = new FileOutputStream(dest);
                
                // 3、操作流
                byte[] buf = new byte[1024];
                int len;
                while ((len = is.read(buf)) != -1) {
                    os.write(buf, 0, len);
                }
                os.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //4、关闭流(先打开的后关闭)
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    linux 内网环境搭建docker
    docker 容器迁移
    linux 按照进程号查看端口号
    docker 开机自启动
    0_campgrounds CRUD
    Delete Product
    new-Category-default category show
    Edit
    Mongoose with express-1
    Mongoose With Express _0: Basic Setup
  • 原文地址:https://www.cnblogs.com/zhengxl5566/p/10803800.html
Copyright © 2011-2022 走看看