字符流五种读写
1 package test03; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileNotFoundException; 7 import java.io.FileReader; 8 import java.io.FileWriter; 9 import java.io.IOException; 10 11 import javax.annotation.processing.Filer; 12 13 public class CopyfileDemo { 14 public static void main(String[] args) throws Exception { 15 File src = new File("a.txt"); 16 File dest = new File("b.txt"); 17 copy1(src,dest); 18 copy2(src,dest); 19 copy3(src,dest); 20 copy4(src,dest); 21 copy5(src,dest); 22 } 23 //缓冲区字符流 特殊方法 24 private static void copy5(File src, File dest) throws IOException { 25 // TODO Auto-generated method stub 26 BufferedReader br = new BufferedReader(new FileReader(src)); 27 BufferedWriter bw = new BufferedWriter(new FileWriter(dest)); 28 29 String line = null; 30 while((line = br.readLine())!=null) {//一次读一行 31 bw.write(line); 32 bw.newLine();//换行 33 } 34 br.close(); 35 bw.close(); 36 37 } 38 //带缓冲区字符流一次一个字符数组 39 private static void copy4(File src, File dest) throws IOException { 40 // TODO Auto-generated method stub 41 BufferedReader br = new BufferedReader(new FileReader(src)); 42 BufferedWriter bw = new BufferedWriter(new FileWriter(dest)); 43 int len = 0; 44 char[] chs = new char[1024]; 45 while((len = br.read(chs))!=-1) { 46 bw.write(chs, 0, len); 47 } 48 br.close(); 49 bw.close(); 50 } 51 //缓冲区字符流 一次读一个字符 52 private static void copy3(File src, File dest) throws Exception { 53 // TODO Auto-generated method stub 54 BufferedReader br = new BufferedReader(new FileReader(src)); 55 BufferedWriter bw = new BufferedWriter(new FileWriter(dest)); 56 57 int b = 0; 58 while((b = br.read())!=-1) { 59 bw.write(b); 60 } 61 br.close(); 62 bw.close(); 63 64 65 } 66 //普通字符流 一次读写一个字符数组 67 private static void copy2(File src, File dest) throws IOException { 68 // TODO Auto-generated method stub 69 FileReader fr =new FileReader(src); 70 FileWriter fw = new FileWriter(dest); 71 72 int len = 0; 73 char [] chs = new char[1024]; 74 while((len = fr.read(chs))!=-1) { 75 fw.write(chs,0,len); 76 } 77 fr.close(); 78 fw.close(); 79 80 } 81 //普通字符流 一次读写一个字符 82 private static void copy1(File src, File dest) throws IOException { 83 // TODO Auto-generated method stub 84 FileReader fr = new FileReader(src); 85 FileWriter fw = new FileWriter(dest); 86 87 int b = 0; 88 while((b = fr.read())!=-1) { 89 fw.write(b); 90 } 91 92 fr.close(); 93 fw.close(); 94 95 96 } 97 }
特殊方法
BufferedReader
String |
readLine() 读取一个文本行。 |
1 public class BufferedReaderDemo2 { 2 public static void main(String[] args) throws IOException { 3 BufferedReader br = new BufferedReader(new FileReader("a.txt")); 4 5 //一次读取一行 6 /*String s = br.readLine(); 7 System.out.println(s);*/ 8 9 String line = null; 10 while((line = br.readLine())!=null) { 11 System.out.println(line); 12 } 13 br.close(); 14 } 15 }
BufferedWriter
void |
newLine() 写入一个行分隔符。 |
public static void main(String[] args) throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt")); bw.write(97); bw.write("helloword"); bw.write(" ");//换行符 bw.newLine();//换行 char[] ch = {'1','2','u','g','h','d'}; bw.write(ch); bw.newLine();//换行 bw.write("helloword"); bw.close(); }