当前位置 博文首页 > 邱天的henry的博客:把d盘的a.txt文件复制到E盘跟目录下(java的

    邱天的henry的博客:把d盘的a.txt文件复制到E盘跟目录下(java的

    作者:[db:作者] 时间:2021-07-19 13:17

    public static void main(String[] args) {
            //将变量定义在外部方便后面关闭资源
            InputStream fis=null;
            OutputStream fos=null;
            try {
                fis = new FileInputStream("D:\\a.txt");
                fos = new FileOutputStream("E:\\a.txt"); //如果e盘没有这个文件夹会自动创建
                //循环读取
                int n;
                while ((n=fis.read())!=-1){  //文件读取完毕,read返回-1
                    fos.write((char)n);    //读取一个字节就往d盘新建的a.txt文件中写入
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {      //关闭资源
                if (fis !=null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fos !=null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    cs