java 核心编程——IO流之字符流
Posted 柳暗花明睡一觉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 核心编程——IO流之字符流相关的知识,希望对你有一定的参考价值。
1.字符流
1.1 所有文件的存储都是字节的存储,我们日常在磁盘上保存的其实并不是文件的字符,而是先把字符转换成字节,再把这些字节储存到磁盘。在读取文件时,也是一个字节一个字节的读取,然后生成字节的序列。
1.2 字节流可以处理任何对象,但是字符流呢,、只用来处理字符或者字符串。可以这样说,字节流呢,提供了最基本IO功能,可以处理任何IO操作,但是有一点,不可以直接处理Unicode字符,为什么呢,因为Unicode字符是一个单元为2个字节的字符,而字节流的处理单元为1个字节,字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符形成的。如果使用字节流直接处理Unicode字符的话,由于各种编码实现的不同,就很容易出现乱码。
1.3实例上,我们日常处理的很多数据,都是文本,所以我们在字节流之外呢,又提出了字符流的概念,直接按照Java虚拟机的encode来处理,也就是在输出流和输入流之间 进行字符集的转换。
2.java字符流实现结构
1.输入字符流
2.输出字符流
3.字符流常用类
3.1 字符数组输入输出流(CharArrayReader和CharArrayWriter)
package se.io; import com.sun.org.apache.xpath.internal.operations.String; import java.io.CharArrayReader; import java.io.CharArrayWriter; import java.io.IOException; public class CharArrayTest { public static void main(String[] args) { char[] chars = new char[3]; chars[0] = 100; chars[1] = 101; chars[2] = 102; CharArrayReader charArrayReader = new CharArrayReader(chars); try { char[] buf = new char[1024]; charArrayReader.read(buf); CharArrayWriter charArrayWriter = new CharArrayWriter(); charArrayWriter.write(buf); System.out.println(charArrayWriter.toString()); charArrayWriter.close(); charArrayReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
3.2文件输入输出流(FileWriter和FileReader)
package se.io; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileCharTest { public static void main(String[] args) { try { FileReader fileReader = new FileReader("E:\\\\test\\\\data.txt"); char[] chars = new char[1024]; int i = fileReader.read(chars); FileWriter fileWriter = new FileWriter("E:\\\\test\\\\filewriter.txt"); fileWriter.write(chars,0,i); fileWriter.close(); fileReader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
3.3管道输入输出流(PipedReader 和 PipedWriter)
package se.io; import java.io.IOException; import java.io.PipedReader; import java.io.PipedWriter; public class PipedChar { public static void main(String[] args) { SenderChar senderChar = new SenderChar(); PipedWriter pipedWriter = senderChar.getWriter(); ReceiverChar receiverChar = new ReceiverChar(); PipedReader pipedReader =receiverChar.getReader(); try { pipedWriter.connect(pipedReader); senderChar.start(); receiverChar.start(); } catch (IOException e) { e.printStackTrace(); } } } //创建发送者类 class SenderChar extends Thread{ private PipedWriter writer = new PipedWriter(); public PipedWriter getWriter() { return writer; } public void run(){ String s = new String("hello,world"); try { writer.write(s); writer.close(); } catch (IOException e) { e.printStackTrace(); } } } //创建接受者 class ReceiverChar extends Thread{ private PipedReader reader = new PipedReader(); public PipedReader getReader() { return reader; } public void run(){ String s= null; char[] chars = new char[1024]; try { int i = reader.read(chars); s = new String(chars,0,i); System.out.println(s); reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
3.4缓存输入输出流(BufferedReader 和BufferedWriter)
package se.io; import java.io.*; public class BufferWriterTest { public static void main(String[] args) { try { //构建文件输入输出字符流,如果指定文件不存在,则自动生成。 FileReader fileReader = new FileReader("E:\\\\test\\\\data.txt"); FileWriter fileWriter = new FileWriter("E:\\\\test\\\\data2.txt"); //构建过滤缓存流 BufferedReader bufferedReader = new BufferedReader(fileReader); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); //缓存数组 char[] chars = new char[1024]; int offset = 0 ; //读取字符 while(bufferedReader.ready()) { offset = bufferedReader.read(chars); } //输出字符 bufferedWriter.write(chars,0,offset); //关闭流 bufferedWriter.close(); bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
3.5格式化输出流(PrinterWriter)
package se.io; import java.io.FileWriter; import java.io.FilterWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; public class PrintWriterTest { public static void main(String[] args) { try { //创建文件输出流 FileWriter fileWriter = new FileWriter("E:\\\\test\\\\data3.txt"); //创建格式化对象输出流 PrintWriter printWriter = new PrintWriter(fileWriter); //写入数据 printWriter.printf("%1$tY年%1$tm月%1$td日", new Date()); printWriter.print(123); printWriter.print(12.32); //关闭流 printWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }
以上是关于java 核心编程——IO流之字符流的主要内容,如果未能解决你的问题,请参考以下文章
javaio流之字节流转为字符流:java.io.OutputStreamWriter和java.io.InputStreamReader