zoukankan      html  css  js  c++  java
  • java把一个文件的内容复制到另外一个文件

    /**
     * java把一个文件的内容复制到另外一个文件
     */
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    public class FileInputOutputStreamTest {
     public static void main(String[] args) {
      File af = new File("a.txt");
      File bf = new File("b.txt");
      FileInputStream is = null;
      FileOutputStream os = null;
      if(!bf.exists()){
       try {
        bf.createNewFile();
       } catch (IOException e) {
        e.printStackTrace();
       }
      }
      try {
       is = new FileInputStream(af);
       os = new FileOutputStream(bf);
       byte b[] = new byte[1024];
       int len;
       try {
        len = is.read(b);
        while (len != -1) {
         os.write(b, 0, len);
         len = is.read(b);
        }
       } catch (IOException e) {
        e.printStackTrace();
       }
      } catch (FileNotFoundException e) {
       e.printStackTrace();
      }finally{
       try {
        if(is != null) is.close();
        if(os != null) os.close();
       } catch (IOException e) {
        e.printStackTrace();
       }
      }
     }

    }

  • 相关阅读:
    011-通过网络协议解析网络请求-DNS-ARP-TCPIP
    010-HTTP协议
    009-DNS域名解析系统
    008-ICMP协议(网络控制文协议)
    007-IP报文协议
    007-排序算法-堆排序
    006-排序算法-希尔排序
    007-Linux 查看端口
    005-排序算法-归并排序
    004-排序算法-选择排序
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/7650327.html
Copyright © 2011-2022 走看看