Java IO-3 转换流和缓冲流
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java IO-3 转换流和缓冲流相关的知识,希望对你有一定的参考价值。
1.转换流
OutputStreamWriter和InputStreamReader
1 package demo04; 2 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 7 public class InputStreamReaderDemo { 8 public static void main(String[] args) throws IOException { 9 FileInputStream in = new FileInputStream("c:\\gbk.txt"); 10 InputStreamReader r = new InputStreamReader(in); 11 char[] ch = new char[1024]; 12 int len = r.read(ch); 13 System.out.println(new String(ch, 0, len)); 14 r.close(); 15 } 16 }
1 package demo04; 2 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.OutputStreamWriter; 6 7 public class OutputStreamWriterDemo { 8 public static void main(String[] args) throws IOException { 9 FileOutputStream out = new FileOutputStream("c:\\utf8.txt"); 10 //默认GBK编码 占4个字节 11 //OutputStreamWriter os = new OutputStreamWriter(out); 12 //UTF-8编码 占6个字节 13 OutputStreamWriter os = new OutputStreamWriter(out, "UTF-8"); 14 os.write("你好"); 15 os.close(); 16 } 17 }
2.字节缓冲流
分为BufferedInputStream和BufferedOutputStream, 它们的构造方法分别接受InputStream和OutputStream类型的参数作为被包装对象, 在读写数据时提供缓冲功能
1 package demo04; 2 3 import java.io.BufferedInputStream; 4 import java.io.FileInputStream; 5 import java.io.IOException; 6 7 public class BufferedInputStreamDemo { 8 public static void main(String[] args) throws IOException { 9 BufferedInputStream ins = new BufferedInputStream(new FileInputStream("c:\\buffer.txt")); 10 byte[] bytes = new byte[20]; 11 int len = 0; 12 while ((len = ins.read(bytes)) != -1 ) { 13 System.out.println(new String(bytes, 0, len)); 14 } 15 ins.close(); 16 } 17 }
1 package demo04; 2 3 import java.io.BufferedOutputStream; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 7 public class BufferedOutputStreamDemo { 8 public static void main(String[] args) throws IOException { 9 FileOutputStream out = new FileOutputStream("c:\\buffer.txt"); 10 BufferedOutputStream buf = new BufferedOutputStream(out); 11 buf.write(100); 12 buf.close(); 13 } 14 }
3.字符缓冲流
分为BufferedWriter和BufferedReader
1 package demo04; 2 3 import java.io.BufferedReader; 4 import java.io.FileReader; 5 import java.io.IOException; 6 7 public class BufferedReaderDemo { 8 public static void main(String[] args) throws IOException { 9 BufferedReader bfr = new BufferedReader(new FileReader("c:\\reader.txt")); 10 String line = null; 11 while ((line = bfr.readLine()) != null) { 12 System.out.println(line); 13 } 14 bfr.close(); 15 } 16 }
1 package demo04; 2 3 import java.io.BufferedWriter; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 7 public class BufferedWriterDemo { 8 public static void main(String[] args) throws IOException { 9 FileWriter fw = new FileWriter("c:\\buffer.txt"); 10 BufferedWriter bfw = new BufferedWriter(fw); 11 bfw.write(100); 12 bfw.flush(); 13 14 bfw.write("你好".toCharArray()); 15 bfw.flush(); 16 17 //换行 与平台无关 18 bfw.newLine(); 19 bfw.write("你好".toCharArray()); 20 bfw.flush(); 21 22 bfw.close(); 23 } 24 }
以上是关于Java IO-3 转换流和缓冲流的主要内容,如果未能解决你的问题,请参考以下文章