Java: IO 字节流
FileReader是InputStreamReader的子类,InputStreamReader是Reader的子类。Reader系列是输入字符流。
1.一个字符一个字符的读取
1 import java.io.*; 2 3 public class FileReaderDemo { 4 5 public static void main(String[] args) { 6 FileReader fr = null; 7 try{ 8 fr = new FileReader("FileWriterDemo.txt"); 9 int ch =0; 10 while((ch = fr.read())!=-1){ 11 System.out.print((char)ch); 12 } 13 } 14 catch(IOException e){ 15 System.out.println(e.toString()); 16 } 17 finally{ 18 try{ 19 if(fr!=null) 20 fr.close(); 21 } 22 catch(IOException e){ 23 System.out.println(e.toString()); 24 } 25 26 } 27 28 } 29 30 }
2.使用字符数组临时存储读取的字符,数组长度可以自定义
1 import java.io.FileReader; 2 import java.io.IOException; 3 4 public class FileReaderDemo2 { 5 6 public static void main(String[] args) { 7 FileReader fr = null; 8 try{ 9 fr = new FileReader("FileWriterDemo.txt"); 10 //定义字符数组用于存储读到的字符 11 char[] buf = new char[1024]; 12 int aLength = fr.read(buf); 13 for(int x = 0;x< aLength;x++) 14 { 15 System.out.print(buf[x]); 16 } 17 18 } 19 catch(IOException e){ 20 System.out.println(e.toString()); 21 } 22 finally{ 23 try{ 24 if(fr!=null) 25 fr.close(); 26 } 27 catch(IOException e){ 28 System.out.println(e.getStackTrace()); 29 } 30 } 31 32 } 33 34 }
为了提高读取效率,加入BufferedReader(Reader in)缓冲技术,该类可以使用readLine方法,一次读取一行。
1 import java.io.*; 2 3 public class BufferedReaderDemo { 4 5 public static void main(String[] args) { 6 //创建一个读取流对象 7 FileReader fr = null; 8 try{ 9 fr = new FileReader("xinwen.txt"); 10 //加入缓冲技术,提高读取效率 11 BufferedReader bufr = new BufferedReader(fr); 12 13 String line = null; 14 while((line=bufr.readLine())!=null){ 15 System.out.println(line); 16 } 17 bufr.close(); 18 } 19 catch(IOException e){ 20 e.printStackTrace(); 21 } 22 finally{ 23 try{ 24 if(fr!=null) 25 fr.close(); 26 } 27 catch(IOException e){ 28 e.printStackTrace(); 29 } 30 } 31 } 32 }
FileWriter是OutputStreamWriter的子类,而OutputStreamWriter是Writer的子类。Writer系列是输出字符流。
1 import java.io.*; 2 3 public class FileWriterDemo { 4 5 public static void main(String[] args)throws IOException { 6 FileWriter fw = null; 7 try { 8 fw = new FileWriter("FileWriterDemo.txt",true); 9 fw.write("2015"); 10 fw.write("\r\n"); 11 fw.write("一起努力!"); 12 13 fw.flush(); 14 } 15 catch(IOException e){ 16 System.out.println(e.toString()); 17 } 18 finally{ 19 try{ 20 if(fw!=null) 21 fw.close(); 22 } 23 catch(IOException e){ 24 System.out.println(e.toString()); 25 } 26 } 27 } 28 29 }
为了提阿写入效率,使用BufferedWriter(Writer wr)缓冲技术,该类中的newLine方法可以输出一个换行符,而且是跨平台的。
把输入、输出结合起来,模拟复制文件。方法一,不使用缓冲技术
1 import java.io.*; 2 3 public class FileCopyDemo { 4 5 public static void main(String[] args) { 6 7 //copy1(); 8 copy2(); 9 } 10 //读一个字符就存一个 11 public static void copy1(){ 12 //创建目的文件 13 FileWriter fw = null; 14 //与已有文件关联 15 FileReader fr = null; 16 17 try{ 18 fw = new FileWriter("/home/owen/news_copy.txt"); 19 fr = new FileReader("news.txt"); 20 int ch = 0; 21 while((ch=fr.read())!=-1){ 22 fw.write(ch); 23 } 24 } 25 catch(IOException e){ 26 System.out.println(e.getStackTrace()); 27 } 28 finally{ 29 try{ 30 if(fw!=null) 31 fw.close(); 32 if(fr!=null) 33 fr.close(); 34 } 35 catch(IOException e){ 36 System.out.println(e.getStackTrace()); 37 } 38 } 39 } 40 public static void copy2(){ 41 FileWriter fw = null; 42 FileReader fr = null; 43 try{ 44 fw = new FileWriter("news_copy2.txt"); 45 fr = new FileReader("news.txt"); 46 47 char[] buf = new char[1024]; 48 int len = 0; 49 while((len = fr.read(buf))!=-1){ 50 fw.write(buf,0,len); 51 } 52 } 53 catch(IOException e){ 54 throw new RuntimeException("读写失败"); 55 } 56 finally{ 57 if(fr!=null){ 58 try{ 59 fr.close(); 60 } 61 catch(IOException e){ 62 System.out.println(e.getStackTrace()); 63 } 64 } 65 if(fw!=null){ 66 try{ 67 fw.close(); 68 } 69 catch(IOException e){ 70 System.out.println(e.getStackTrace()); 71 } 72 } 73 } 74 } 75 76 }
方法二,使用缓冲技术
1 import java.io.*; 2 3 public class CopyByBuffered { 4 5 public static void main(String[] args) { 6 BufferedReader bufr = null; 7 BufferedWriter bufw = null; 8 try{ 9 bufr = new BufferedReader(new FileReader("BufferedReaderDemo.java")); 10 bufw = new BufferedWriter(new FileWriter("BufferedReaderDemo_copy.txt")); 11 12 String line = null; 13 while((line = bufr.readLine())!=null){ 14 bufw.write(line); 15 bufw.flush(); 16 bufw.newLine(); 17 } 18 } 19 catch(IOException e){ 20 throw new RuntimeException("复制失败!"); 21 } 22 finally{ 23 try{ 24 if(bufr!=null) 25 bufr.close(); 26 } 27 catch(IOException e){ 28 throw new RuntimeException("Read file mission failed!"); 29 } 30 try{ 31 if(bufw!=null) 32 bufw.close(); 33 } 34 catch(IOException e){ 35 throw new RuntimeException("Write file mission failed!"); 36 } 37 } 38 39 } 40 41 }