Java基础教程——字符流

字符流

字节流服务文本文件时,可能出现中文乱码。因为一个中文字符可能占用多个字节。
针对于非英语系的国家和地区,提供了一套方便读写方式——字符流。

java.io.Reader
java.io.Writer

文件字符流

|-读文件:FileReader

java.io.FileReader fr = new FileReader("待读取的文件");// 构造时使用默认的字符编码

int 读取单个字符 = fr.read();

char[] cbuf = new char[1024];
int 读取到字符数组中 = fr.read(cbuf);

fr.close();

|-写文件: FileWriter

java.io.FileWriter fw = new FileWriter("要写的文件");

fw.write(71);// 写单个字符

char[] cbuf = {};
fw.write(cbuf);// 写字符数组

fw.write("写字符串");

fw.close();// 字符流和字节流不同,有内置缓冲区。如果不关闭,数据只是保存到缓冲区,未写入文件。

fw.flush();// 可以使用flush可以把缓冲区中的数据强制刷入文件(最后还是应该close)

前面字节流的例子,可以改为字符流测试运行。参考代码:

import java.io.*;
public class 字符流 {
	public static void main(String[] args) throws IOException {
		read();
		write();
	}
	static final int C_CONDITION = 1;
	public static void read() {
		try {
			File file = new File("testRead.dat"); // 创建文件对象
			// 【1】创建输入流对象,相当于打开文件
			FileReader fr = new FileReader(file);
			if (C_CONDITION == 1) {
				// 【2】.read():读取单个
				for (int i = 0; i < file.length(); i++) {
					int read = fr.read();
					char ch = (char) read;
					System.out.print(ch);
				}
				System.out.println();
			} else {
				// 【2】.read(char[] b):
				long len = file.length();
				char[] buf = new char[(int) len];
				fr.read(buf);
				System.out.println(buf);
			}
			// 【3】关闭流
			fr.close();
		} catch (FileNotFoundException fnfe) {
			System.out.println("文件打开失败。");
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
	static void write() {
		File file = new File("文件字节输出流.txt"); // 创建文件对象
		try {
			// (1)构造:通过文件对象创建文件输出流对象
			FileWriter fw = new FileWriter(file);
			// (2)写入文件
			// (2.1)write(int b)
			for (int i = 49; i < 97; i++) {
				fw.write(i);
				fw.write(' ');
			}
			// (2.2)write(char[] b)
			char[] buffer = "I Love Java,你呢?".toCharArray();
			fw.write(buffer);
			// (3)关闭流:如果不关闭,数据只是保存到缓冲区,并未保存到文件。可以使用flush把数据强制刷入文件。
			fw.flush();
			// fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Properties类

package java.util;
public class Properties extends Hashtable<Object,Object>

表示属性集。每个键及其对应值都是一个字符串。

该类应用广泛,比如获取系统属性:

Properties props = System.getProperties();

使用方法:

import java.io.*;
import java.util.Properties;
import java.util.Set;
public class TestProperties {
	public static void main(String[] args) throws IOException {
		// Properties类extends Hashtable,其实就是个map
		Properties prop = new Properties();
		// setProperty:调用的put方法,但参数只允许字符串
		prop.setProperty("金箍棒", "一万三千五百斤");
		prop.setProperty("九齿钉耙", "五千零四十八斤");
		prop.setProperty("降妖宝杖", "五千零四十八斤");
		// stringPropertyNames:调用了keySet()方法
		Set<String> set = prop.stringPropertyNames();
		for (String key : set) {
			// getProperty:调用了get方法
			String value = prop.getProperty(key);
			System.out.println(key + ":" + value);
		}
		// ----------------------------------------------
		// .store存储键值对到文件
		FileWriter fw = new FileWriter("prop.txt");
		// 参数1:字符流(也有字节流的重载版本,不能写中文)
		// 参数2:注释,Unicode编码,不要写中文
		prop.store(fw, "save data");
		// 关闭流
		fw.close();
		// ----------------------------------------------
		// .load从文件读取键值对(k-v)
		prop = new Properties();
		FileReader fr = new FileReader("prop.txt");
		prop.load(fr);
		System.out.println(prop);
		fr.close();
	}
}
posted @ 2019-07-13 02:32  虎老狮  阅读(189)  评论(0编辑  收藏  举报