zoukankan      html  css  js  c++  java
  • JAVA练习笔记---copyfile

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class Collection {
        public static void main(String[] args) throws FileNotFoundException, IOException{
            File file = new File("D:/","copy1223.txt");//创建一个新的文件
            InputStream input=new FileInputStream("D:/1223.txt");// 读入一个原有的文件
            byte[] b=new byte[2048];
            input.read(b);  //把文件读到B中
            input.close(); //必须关闭
            
            OutputStream output=new FileOutputStream("D:/copy1223.txt");
            output.write(b); //把B中的文件写入copy123中
            output.close(); //必须关闭
            
          }
        
    }




    --------------------------------------------
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package testSet;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     *
     * @author Administrator
     */
    public class FileCopy {
    
        public static void main(String[] args){
            File file = new File("D:/inst.exe");
            File target = new File("D:/copy"+file.getName());
            copyFile(file.getPath(),target.getPath());
        }
        public static void copyFile(String file, String targetfile) {
            FileInputStream fis = null;
            FileOutputStream fos = null;
            File source = new File(file);
            if (!source.exists()) {
                System.err.println("File not exists!");
                return;
            }
            try {
                fis = new FileInputStream(file);
                fos = new FileOutputStream(targetfile);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fis.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                    fos.flush();
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FileCopy.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(FileCopy.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    if (fis != null) {
    
                        fis.close();
                    }
                    if (fos != null) {
                        fos.close();
                    }
                } catch (IOException ex) {
                    Logger.getLogger(FileCopy.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
     

      

  • 相关阅读:
    让开发效率“飞起”的VS Code 插件
    转-webpack学习笔记--整体配置结构
    十二、vue中watch原理
    十一、vue生命周期诠释--带图
    十、vue mixins 的用法
    八、Web移动端Fixed布局的解决方案
    七、vue中v-for有时候对页面不会重新渲染,数组变化后如何到渲染页面
    六、vue如何缓存页面
    五、vue常用UI组件
    vue组件递归
  • 原文地址:https://www.cnblogs.com/zzblee/p/3850480.html
Copyright © 2011-2022 走看看