package bufferedWriter;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;public class Demo1 { public static void main(String[] args) { wirter(); } /** * 高效缓冲区 */ public static void wirter() { FileWriter fw = null; BufferedWriter bw = null; try { fw = new FileWriter("d.txt"); //创建高效缓冲区的对象 bw = new BufferedWriter(fw); //输出数据 for(int i=0 ; i<10; i ++) { bw.write("java太好学了,太简单了");// 换行,相当于\r\n bw.newLine(); } //清空缓冲区 bw.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(bw!=null) { try { //关闭缓冲区的流,会自动关闭FIlewriter bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}
package bufferedReader;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class Demo1 { public static void main(String[] args) { reader(); } public static void reader() { FileReader fr = null; BufferedReader br = null; try { fr = new FileReader("d.txt"); br = new BufferedReader(fr); int num = -1; while((num=br.read()) != -1) { System.out.print((char)num); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } finally { if(br != null) { try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}
package bufferedReader;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class Demo2 { public static void main(String[] args) { reader(); } public static void reader() { FileReader fr = null; BufferedReader br = null; try { fr = new FileReader("d.txt"); br = new BufferedReader(fr); char [] chs = new char[1024]; int num = -1; while((num=br.read(chs)) != -1) { System.out.print(new String(chs,0,num)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } finally { if(br != null) { try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}
package bufferedReader;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class Demo3 { public static void main(String[] args) { reader(); } public static void reader() { FileReader fr = null; BufferedReader br = null; try { fr = new FileReader("d.txt"); br = new BufferedReader(fr); String line = br.readLine(); //下一行读取数据的时候,如果没有数据的话,会得到空,null; //使用高效输入流可以读取一行数据 while((line = br.readLine()) != null) { System.out.println(line); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } finally { if(br != null) { try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}