最近刚刚学习了java的io流,今天抽空把io流里面的BufferedReader,BufferedWriter,FileInputStream,FileOutputStream,PrintWriter的基本读写方法总结了一趟
package com.lh.iostream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class iostream_text { /* * 用打印流PrintWriter來写入数据 * * */ public void printstream(StringBuffer str) throws IOException { String content = String.valueOf(str); File file = new File("src/lab02.txt"); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); PrintWriter prw = new PrintWriter(fw); long start = System.currentTimeMillis(); prw.write(content); prw.flush(); // 因为数据读取的时候,一般都是先将数据读取到内存里面,然后再是写入到文本里面的, // 当数据读取完毕的时候并不意味着内容就完全写入到了文本里面了 // 此时文本内容还有可能会留在了缓存区里面,因此我们需要对缓存里面的数据进行flush(); System.out.println("---------printwriter is ok!---------"); long end = System.currentTimeMillis(); long total = end - start; System.out.println("the total time is:" + total + "ms"); prw.close(); fw.close(); } /* * 用BufferedReader来写入数据 * * */ public void bufferedwriter(StringBuffer str) throws IOException { String content = String.valueOf(str); File file = new File("src/lab03.txt"); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bfw = new BufferedWriter(fw); long start = System.currentTimeMillis(); bfw.write(content); bfw.flush(); // 因为数据读取的时候,一般都是先将数据读取到内存里面,然后再是写入到文本里面的, // 当数据读取完毕的时候并不意味着内容就完全写入到了文本里面了 // 此时文本内容还有可能会留在了缓存区里面,因此我们需要对缓存里面的数据进行flush(); System.out.println("---------BufferedReader is ok!---------"); long end = System.currentTimeMillis(); long total = end - start; System.out.println("the total time is:" + total + "ms"); bfw.close(); fw.close(); } /* * 用打印流fileoutputwriter來写入数据 * * */ public void fileoutputwriter(StringBuffer str) throws IOException { String content = String.valueOf(str); File file = new File("src/lab04.txt"); if (!file.exists()) { file.createNewFile(); } byte Contentbyte[]=content.getBytes(); FileOutputStream out=new FileOutputStream(file); long start = System.currentTimeMillis(); out.write(Contentbyte,0,Contentbyte.length); System.out.println("----------FileOutputStream is ok!---------"); long end = System.currentTimeMillis(); long total = end - start; System.out.println("the total time is:" + total + "ms"); out.close(); } public void bufferedreader(File file) throws IOException { BufferedReader bfr=new BufferedReader(new FileReader(file)); String content=null; long start = System.currentTimeMillis(); while(bfr.readLine() != null) { content=content+(bfr.readLine()+'\n'); } System.out.println("----------BufferedReader is ok!---------"); long end = System.currentTimeMillis(); long total = end - start; System.out.println("the total time is:" + total + "ms"); bfr.close(); } public void fileinputstream(File file) throws IOException { FileInputStream in=new FileInputStream(file); long start = System.currentTimeMillis(); byte Content_byte[]=new byte[(int) file.length()]; in.read(Content_byte); System.out.println("----------fileinputstream is ok!---------"); long end = System.currentTimeMillis(); long total = end - start; System.out.println("the total time is:" + total + "ms"); in.close(); } public static void main(String[] sda) throws IOException { iostream_text io=new iostream_text(); StringBuffer str_text=new StringBuffer(); for (int i = 0; i < 4000; i++) { str_text = str_text.append("s"); //str_text所能容纳的字符串长度和jvm自身的内存有关 } File file =new File("src/lab01.txt"); io.printstream(str_text); io.bufferedwriter(str_text); io.bufferedreader(file); io.fileoutputwriter(str_text); io.fileinputstream(file); } }