java-io流-字符流

选择了安逸,就选择了平庸 -2018.10.30

字符流

字符流用于处理用ASCII字符集或Unicode(包含ASCII的国际字符集)表示的文本,可以用字符流来处理的文件有纯文本文件,HTML文档,和java源代码文件。用于读写这些流的类都是Reader和Writer的子类,对于所有的文本输入,都应使用字符流来处理,而不能直接使用字节流来处理。

字符的继承关系

字符继承关系图

读写文件

FileReader是InputStreamReader的子类,常用于从文件中读取字符流。FileWriter为OutputStreamWriter的子类,常用于向文件中写字符流。

文件中读写

构造方法

public FileReader(File file)  
public FileReader(String fileName) 

public FileWriter(File file) 
       根据给定的 File 对象构造一个 FileWriter 对象。 
public FileWriter(File file, boolean append) 
       根据给定的 File 对象构造一个 FileWriter 对象,append代表是否为追加。
public FileWriter(String fileName) 
       根据给定的文件名构造一个 FileWriter 对象。 
public FileWriter(String fileName, boolean append) 
       根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象。

1、在给定指定文件名或者指定的文件的情况下读写数据
2、FileNotFoundException -如果文件不存在,或者它是一个目录,而不是一个常规文件,抑或因为其他某些原因而无法打开进行读取
3、IOException - 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它

 private static Reader testReader = null;
    private static Writer testWriter = null;
    private static String readFilePath = "C:/Users/asus-pc/Desktop/javaProject/src/testIoChar/IoCharDemo.java";
	private static String writeFilePath = "C:/Users/asus-pc/Desktop/javaProject/src/testIoChar/IoCharDemo2.java";
    public static void main(String[] args) throws IOException {
		  File  file  = new File(readFilePath);
	       testReader = new FileReader(file);
          //testReade = new FileReader(filePath);
	       //两种构造方法都可以
	       testWriter = new FileWriter(writeFilePath);//重写文件
	       //testWriter = new FileWriter(writeFilePath,true);在文件尾追加数据
	       }

常用方法概述

从类 java.io.InputStreamReader 继承的方法 
close, getEncoding, read, read, ready 
  从类 java.io.Reader 继承的方法 
mark, markSupported, read, read, reset, skip 

常用方法与字节操作相同

package testIoChar;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class IoCharDemo {
    private static Reader testReader = null;
    private static Writer testWriter = null;
    private static String readFilePath = "C:/Users/asus-pc/Desktop/javaProject/src/testIoChar/IoCharDemo.java";
	private static String writeFilePath = "C:/Users/asus-pc/Desktop/javaProject/src/testIoChar/IoCharDemo2.java";
    public static void main(String[] args) throws IOException {
		  File  file  = new File(readFilePath);
	       testReader = new FileReader(file);
          //testReade = new FileReader(filePath);
	       //两种构造方法都可以
	       testWriter = new FileWriter(writeFilePath);
	       char[]ch = new char[(int)file.length()];
	       testReader.read(ch);
	       System.out.println(new String(ch));
	       testWriter.write(new String(ch));
	       testReader.close();
	       testWriter.close();
	       //如果没有关闭,会发现文件中没有数据,因为没有刷新流,数据只是写在了流里
	       //close()方法中调用了flush()。
    }
}

转换流(InputStreamReader,OutputWriter)

InputStreamReader

  • InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集
  • 每次调用 InputStreamReader 中的一个 read() 方法都会导致从底层输入流读取一个或多个字节。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节,使其超过满足当前读取操作所需的字节。
  • 为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader。例如:
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

方法概述

InputStreamReader(InputStream in) 
          创建一个使用默认字符集的 InputStreamReader。 
InputStreamReader(InputStream in, Charset cs) 
          创建使用给定字符集的 InputStreamReader。 
InputStreamReader(InputStream in, CharsetDecoder dec) 
          创建使用给定字符集解码器的 InputStreamReader。 
InputStreamReader(InputStream in, String charsetName) 
          创建使用指定字符集的 InputStreamReader。 
          charset的常见编码
          US-ASCII 7 位 ASCII 字符,也叫作 ISO646-US、Unicode 字符集的基本拉丁块 
		 ISO-8859-1   ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 
		UTF-8 8 位 UCS 转换格式 
		UTF-16BE 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序 
		UTF-16LE 16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序 
		UTF-16 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识 
          
普通方法概述
void close() 
          关闭该流并释放与之关联的所有资源。 
 String getEncoding() 
          返回此流使用的字符编码的名称。 
 int read() 
          读取单个字符。 
 int read(char[] cbuf, int offset, int length) 
          将字符读入数组中的某一部分。 
 boolean ready() 
          判断此流是否已经准备好用于读取。 

示例
将一个用UTF-8编码的文本文件用字节流读进来,观察之后,再用字符流转换该字节流,观察结果。(新建一个文本文档,命名为test.txt,另存为时,选择UTF-8格式保存。)

package IODemo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamReaderDemo {
   private static String FilePath ="C:/Users/asus-pc/Desktop/javaProject/Demo/src/IODemo/test.txt";
	private static InputStream iStream =null;
	public static void main(String[] args) {
	 	try {
	 		byte bytes[] =new byte[1024];
			iStream =new FileInputStream(FilePath);
			int data =-1;
		  iStream.read(bytes);
		  System.out.println(new String(bytes));
			
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
}

运行结果(可以看出是乱码)

扫描二维码关注公众号,回复: 4314776 查看本文章
锘胯繖鏄竴涓祴璇曟枃浠躲??

用转换流进行转换读写

package IODemo;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class InputStreamReaderDemo {
   private static String FilePath ="C:/Users/asus-pc/Desktop/javaProject/Demo/src/IODemo/test.txt";
	private static InputStream iStream =null;
	private static InputStreamReader iReader =null;
	public static void main(String[] args) {
	 	try {
	 		char ch[] =new char[1024];
			iStream =new FileInputStream(FilePath);
			iReader =new InputStreamReader(iStream,"UTF-8");
			BufferedReader bufferedReader =new BufferedReader(iReader);
			//此处可以添加缓冲流,也可以不添加
		  String data =null;
			while((data=bufferedReader.readLine())!=null)
			 System.out.println(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
}

运行结果

?这是一个测试文件

前面之所以多出一个?,是因为保存为txt文件时,有个字节顺序标记,出现在文本文件头部,Unicode编码标准中用于标识文件是采用哪种格式的编码。

猜你喜欢

转载自blog.csdn.net/kuangpeng1956/article/details/83507206