zoukankan      html  css  js  c++  java
  • Java中在磁盘上复制文件

    • 使用字节流实现
              public static void main(String[] args) throws IOException {
      		InputStream in = new FileInputStream(new File("F:\test.py"));
      		byte[] bts = new byte[1024];
      		OutputStream out = new FileOutputStream(new File("D:\test\test.py"));
      		int len = 0;
      		while((len = in.read(bts))!=-1) {
      			out.write(bts, 0, len);
      		}
      		
      		out.close();
      		in.close();
      	}
      

        

    • 使用字符流实现
      • char数组作为缓存
                public static void main(String[] args) throws IOException {
        		FileReader r = new FileReader("F:\test.py");
        		FileWriter w = new FileWriter("D:\test\test2.py");
        		char[] buf = new char[1024];
        		int c ;
        		while((c=r.read(buf))!=-1) {
        			w.write(buf,0,c);
        		}
        		r.close();
        		w.close();
        	}
        

          

      • 不使用数组做缓存,直接复制
                public static void main(String[] args) throws IOException {
        		FileReader r = new FileReader("F:\test.py");
        		FileWriter w = new FileWriter("D:\test\test2.py");
        //		char[] buf = new char[1024];
        		int c ;
        		while((c=r.read())!=-1) {
        			w.write(c);
        		}
        		r.close();
        		w.close();
        	}    
        

          

  • 相关阅读:
    22. Generate Parentheses
    21. Merge Two Sorted Lists
    20. Valid Parentheses
    19. Remove Nth Node From End of List
    18. 4Sum
    JDK7新特性
    类Enum
    装饰设计模式
    模板设计模式
    反射
  • 原文地址:https://www.cnblogs.com/qf123/p/8534417.html
Copyright © 2011-2022 走看看