java笔记八:IO流之字符流与字符缓冲流
java中字符流主要都是继承于Reader和Writer两个抽象类。用于对字符文本的读写操作。
一、转换类流
1 package com.iotest; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 import java.io.OutputStreamWriter; 9 10 public class ConverseStreamDemo { 11 12 public static void main(String[] args) { 13 String s = "F:\\shar\\test\\test3.txt"; 14 try { 15 //将字节流转为字符流 16 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(s)); 17 osw.write("中国北京"); 18 System.out.println(osw.getEncoding()); 19 osw.close(); 20 osw = new OutputStreamWriter(new FileOutputStream(s,true),"GB2312"); 21 osw.write("中国北京"); 22 System.out.println(osw.getEncoding()); 23 osw.close(); 24 } catch (FileNotFoundException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 } catch (IOException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 } 31 try { 32 //将字节流转为字符流 33 InputStreamReader isr = new InputStreamReader(new FileInputStream(s),"GB2312"); 34 int c; 35 while((c=isr.read()) != -1){ 36 System.out.print((char)c); 37 } 38 System.out.println(); 39 isr.close(); 40 } catch (FileNotFoundException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } catch (IOException e) { 44 // TODO Auto-generated catch block 45 e.printStackTrace(); 46 } 47 } 48 49 }
二、FileReader和FileWriter
1 package com.iotest; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileReader; 5 import java.io.FileWriter; 6 import java.io.IOException; 7 /* 8 * 使用flush()将流数据刷到目标里,这时流还存活着,还可以继续使用该流进行别的操作。 9 * close()虽然也有flush()的效果但是这时流已经死了,你如果想继续使用流的话就必须在此建立流 10 */ 11 public class MutiplicationTable { 12 13 public static void main(String[] args) throws IOException { 14 String s = "F:\\shar\\test\\test5.txt"; 15 FileWriter fw = new FileWriter(s); 16 FileReader fr = new FileReader(s); 17 for (int i = 1; i <= 9; i++) { 18 for (int j = 1; j <= i; j++) { 19 String str = i + "*" + j + "=" + i*j + " "; 20 fw.write(str); 21 } 22 fw.write("\r\n"); 23 /*flush是指强行将输出流中的数据写到文件里面去。如果写到这里的话就是一行一行的写入文件 24 *最好每写完一部分就刷新一次,如果最后刷新的话可能会造成数据的丢失 25 */ 26 fw.flush(); 27 } 28 //如果又不写flush()又不写close(),则不会写入任何内容到文本里。只是写到了缓冲区 29 //fw.flush(); 写到这里的话就是所有的内容一起写进文件 30 //fw.close(); close之前会调用flush() 31 //读字符输入流的数据 32 int c; 33 while ((c=fr.read())!=-1) { 34 System.out.print((char)c); 35 } 36 } 37 38 }
三、BufferedReader和BufferedWriter
1 package com.iotest; 2 3 import java.io.BufferedOutputStream; 4 import java.io.BufferedWriter; 5 import java.io.FileOutputStream; 6 import java.io.FileWriter; 7 import java.io.IOException; 8 9 public class PrimeNumber { 10 BufferedWriter bw = null; 11 String fileName = "F:\\shar\\test\\test7.txt"; 12 //判断是否是质数 13 public boolean isPrime(int n){ 14 for(int i=2;i<=n/2;i++){ 15 if(n%i == 0){ 16 return false; 17 } 18 } 19 return true; 20 } 21 void printPrime(int m) throws IOException{ 22 //将字节流转缓冲流 23 bw = new BufferedWriter(new FileWriter(fileName)); 24 int j = 0; 25 for (int i = 2; i < m; i++) { 26 if(isPrime(i)){ 27 j++; 28 String s = String.valueOf(i); 29 String s1 = s + " "; 30 bw.write(s1); //写入文本文件 31 if(j==10){ 32 j = 0; 33 bw.newLine(); //写入一个行分隔符 34 bw.flush(); //强制刷新 35 } 36 } 37 } 38 39 bw.close(); 40 } 41 public static void main(String[] args) throws IOException { 42 PrimeNumber pn = new PrimeNumber(); 43 pn.printPrime(100); 44 } 45 46 }
四、PrintWriter
1 package com.iotest; 2 3 import java.io.FileWriter; 4 import java.io.IOException; 5 import java.io.PrintWriter; 6 7 public class PrintWriterDemo { 8 9 public static void main(String[] args) throws IOException { 10 int A = 0; //千位 11 int B = 0; //百位 12 int C = 0; //十位 13 int num = 0; 14 String fileName = "F:\\shar\\test\\test8.txt"; 15 FileWriter fw = new FileWriter(fileName); //字符输出流 16 PrintWriter pw = new PrintWriter(fw); //将字节输出流转为PrintWriter 17 for (int i = 1000; i < 10000; i++) { 18 A = i/1000; 19 B = i/100 % 10; 20 C = i/10 % 10; 21 if(i%11==0 && A == B+C){ 22 pw.print(i+" "); 23 if(++num % 7 == 0){ 24 pw.println(); //写入回车换行 25 pw.flush(); //强制刷新流 26 } 27 } 28 } 29 fw.close(); 30 31 } 32 33 }
五、标准IO
1 package com.iotest; 2 3 import java.io.BufferedReader; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.io.PrintWriter; 8 import java.util.Scanner; 9 10 public class PackStardIO { 11 12 public static void main(String[] args) throws IOException { 13 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 PrintWriter o = new PrintWriter(System.out); //包装标准输出 15 String s; 16 while((s=br.readLine())!=null && s.length()!=0){ 17 o.println(s.toUpperCase()); 18 //o.flush(); //一定要强制刷新让其输出来,否则不会输出 19 } 20 } 21 22 }
1 package com.iotest; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.BufferedReader; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.InputStreamReader; 11 import java.io.PrintStream; 12 13 public class StandardIORedirect { 14 15 public static void main(String[] args) throws IOException { 16 PrintStream console = System.out; 17 BufferedInputStream in = new BufferedInputStream(new FileInputStream("C:\\Documents and Settings\\01\\桌面\\Test\\src\\com\\iotest\\StandardIORedirect.java")); 18 PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("F:\\shar\\test\\test9.txt"))); 19 System.setIn(in); //对标准输入流重定向 20 System.setOut(out); 21 System.setErr(out); 22 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 23 String s; 24 while((s=br.readLine())!=null){ //从BufferedReader中读取一行数据 25 System.out.println(s); 26 } 27 out.close(); 28 System.setOut(console); 29 } 30 31 }
六、随即访问文件类
1 package com.iotest; 2 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.io.RandomAccessFile; 6 7 public class RandomRW { 8 public static void main(String[] args) throws IOException { 9 RandomAccessFile raf = new RandomAccessFile("F:\\shar\\test\\test10.dat","rw"); 10 final int DOUBLE_SIZE = 8; 11 for (int i = 0; i < 10; i++) { 12 raf.writeDouble(i); 13 System.out.print(" " + (double)i); 14 } 15 System.out.println(); 16 raf.close(); 17 RandomAccessFile raf1 = new RandomAccessFile("F:\\shar\\test\\test10.dat","rw"); 18 raf1.seek(3*DOUBLE_SIZE); //找到某个位置 19 raf1.writeDouble(300); //往该位置插入数据 20 raf1.seek(5*DOUBLE_SIZE); 21 raf1.writeDouble(500); 22 raf1.close(); 23 RandomAccessFile raf2 = new RandomAccessFile("F:\\shar\\test\\test10.dat","r"); 24 for (int i = 0; i < 10; i++) { 25 System.out.print(" "+raf2.readDouble()); 26 } 27 raf2.close(); 28 } 29 30 }
我喜欢,驾驭着代码在风驰电掣中创造完美!我喜欢,操纵着代码在随必所欲中体验生活!我喜欢,书写着代码在时代浪潮中完成经典!每一段新的代码在我手中诞生对我来说就象观看刹那花开的感动!