[2017.11.26]作业14

来源:互联网 发布:mv视频制作软件 编辑:程序博客网 时间:2024/06/05 08:54

3:复制文本文件:有5种方式

import java.io.BufferedReader;  import java.io.BufferedWriter;  import java.io.FileReader;  import java.io.FileWriter;  import java.io.IOException;  public class Demo {      public static void main(String[] args) throws IOException {          String srcString = "c:\\a.txt";          String destString = "d:\\b.txt";          method1(srcString, destString);          method2(srcString, destString);          method3(srcString, destString);          method4(srcString, destString);          method5(srcString, destString);      }      // 字符缓冲流一次读写一个字符串      private static void method5(String srcString, String destString)  throws IOException {          BufferedReader br = new BufferedReader(new FileReader(srcString));          BufferedWriter bw = new BufferedWriter(new FileWriter(destString));          String line = null;          while ((line = br.readLine()) != null) {              bw.write(line);              bw.newLine();              bw.flush();          }          bw.close();          br.close();      }      // 字符缓冲流一次读写一个字符数组      private static void method4(String srcString, String destString)              throws IOException {          BufferedReader br = new BufferedReader(new FileReader(srcString));          BufferedWriter bw = new BufferedWriter(new FileWriter(destString));          char[] chs = new char[1024];          int len = 0;          while ((len = br.read(chs)) != -1) {              bw.write(chs, 0, len);          }          bw.close();          br.close();      }      // 字符缓冲流一次读写一个字符      private static void method3(String srcString, String destString) throws IOException {          BufferedReader br = new BufferedReader(new FileReader(srcString));          BufferedWriter bw = new BufferedWriter(new FileWriter(destString));          int ch = 0;          while ((ch = br.read()) != -1) {              bw.write(ch);          }          bw.close();          br.close();      }      // 基本字符流一次读写一个字符数组      private static void method2(String srcString, String destString) throws IOException {          FileReader fr = new FileReader(srcString);          FileWriter fw = new FileWriter(destString);          char[] chs = new char[1024];          int len = 0;          while ((len = fr.read(chs)) != -1) {              fw.write(chs, 0, len);          }          fw.close();          fr.close();      }      // 基本字符流一次读写一个字符      private static void method1(String srcString, String destString) throws IOException {          FileReader fr = new FileReader(srcString);          FileWriter fw = new FileWriter(destString);          int ch = 0;          while ((ch = fr.read()) != -1) {              fw.write(ch);          }          fw.close();          fr.close();      }  }  

4:复制图片:4种方式

import java.io.BufferedInputStream;  import java.io.BufferedOutputStream;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.IOException;  public class Demo2 {      public static void main(String[] args) throws IOException {          File srcFile = new File("c:\\a.jpg");          File destFile = new File("d:\\b.jpg");          method1(srcFile, destFile);          method2(srcFile, destFile);          method3(srcFile, destFile);          method4(srcFile, destFile);      }      //高效字节流一次读写一个字节数组      public static void method4(File srcFile, File destFile)  throws IOException {          BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));          BufferedOutputStream bos = new BufferedOutputStream(                  new FileOutputStream(destFile));          byte[] bys = new byte[1024];          int len = 0;          while ((len = bis.read(bys)) != -1) {              bos.write(bys, 0, len);              bos.flush();          }          bos.close();          bis.close();      }      //高效字节流一次读写一个字节      public static void method3(File srcFile, File destFile) throws IOException {          BufferedInputStream bis = new BufferedInputStream(new FileInputStream( srcFile));          BufferedOutputStream bos = new BufferedOutputStream(                  new FileOutputStream(destFile));          int len = 0;          while ((len = bis.read()) != -1) {              bos.write(len);          }          bos.close();          bis.close();      }      //基本字节流一次读写一个字节数组      public static void method2(File srcFile, File destFile)  throws IOException {          FileInputStream fis = new FileInputStream(srcFile);          FileOutputStream fos = new FileOutputStream(destFile);          byte[] bys = new byte[1024];          int len = 0;          while ((len = fis.read(bys)) != -1) {              fos.write(bys, 0, len);              fos.flush();          }          fos.close();          fis.close();      }      //基本字节流一次读写一个字节      public static void method1(File srcFile, File destFile) throws IOException {          FileInputStream fis = new FileInputStream(srcFile);          FileOutputStream fos = new FileOutputStream(destFile);          int len = 0;          while ((len = fis.read()) != -1) {              fos.write(len);          }          fos.close();          fis.close();      }  }  
原创粉丝点击