1 package demo; 2 import java.io.File; 3 import java.io.FileOutputStream; 4 import java.io.*; 5 6 public class FileOperate { 7 8 public static void main(String[] args) { 9 10 FileOutputStream out = null; 11 FileOutputStream outSTr = null; 12 BufferedOutputStream Buff=null; 13 FileWriter fw = null; 14 int count=50000;//写文件行数 15 16 try { 17 //****************************FileOutputStream保存************************************** 18 out = new FileOutputStream(new File("E:/add0.html")); 19 long begin = System.currentTimeMillis(); 20 for (int i = 0; i < count; i++) { 21 out.write("测试java 文件操作".getBytes()); 22 } 23 out.close(); 24 long end = System.currentTimeMillis(); 25 System.out.println("FileOutputStream执行耗时:" + (end - begin) + " 豪秒"); 26 27 //****************************BufferedOutputStream保存*********************************** 28 outSTr = new FileOutputStream(new File("E:/add1.html")); 29 Buff=new BufferedOutputStream(outSTr); 30 long begin0 = System.currentTimeMillis(); 31 for (int i = 0; i < count; i++) { 32 Buff.write("测试java 文件操作".getBytes()); 33 } 34 Buff.flush(); 35 Buff.close(); 36 long end0 = System.currentTimeMillis(); 37 System.out.println("BufferedOutputStream执行耗时:" + (end0 - begin0) + " 豪秒"); 38 39 //****************************fileWriter保存********************************************* 40 fw = new FileWriter("E:/add2.html"); 41 long begin3 = System.currentTimeMillis(); 42 for (int i = 0; i < count; i++) { 43 fw.write("测试java 文件操作"); 44 } 45 fw.close(); 46 long end3 = System.currentTimeMillis(); 47 System.out.println("FileWriter执行耗时:" + (end3 - begin3) + " 豪秒"); 48 49 //****************************outPutStreamWriter保存************************************** 50 FileOutputStream os = new FileOutputStream(new File("E:/add3.html")); 51 OutputStreamWriter osWriter = new OutputStreamWriter(os, "UTF-8"); 52 long begin4 = System.currentTimeMillis(); 53 for (int i = 0; i < count; i++) { 54 osWriter.write("测试java 文件操作"); 55 } 56 osWriter.close(); 57 long end4 = System.currentTimeMillis(); 58 os.close(); 59 System.out.println("outPutStreamWriter执行耗时:" + (end4 - begin4) + " 豪秒"); 60 61 FileWriter fileWritter = new FileWriter(new File("E:/add4.html")); 62 BufferedWriter bufferWritter = new BufferedWriter(fileWritter); 63 long begin5 = System.currentTimeMillis(); 64 for (int i = 0; i < count; i++) { 65 bufferWritter.write("测试java 文件操作"); 66 } 67 bufferWritter.close(); 68 long end5 = System.currentTimeMillis(); 69 System.out.println("BufferedWriter执行耗时:" + (end5 - begin5) + " 豪秒"); 70 } catch (Exception e) { 71 e.printStackTrace(); 72 } finally { 73 try { 74 fw.close(); 75 Buff.close(); 76 outSTr.close(); 77 out.close(); 78 } catch (Exception e) { 79 e.printStackTrace(); 80 } 81 } 82 } 83 84 }