天天看点

IO流_ Properties (配置文件类), 编码与解码, 转换流(InputStreamRead)——Java基础

1.  Properties (配置文件类)

 Properties (配置文件类) : 主要用于生产配置文件与读取配置文件的信息

 *     表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串  

 *   public class Properties extends Hashtable <Object,Object> 

1.   配置文件按的信息一旦使用了中文,那么使用store方法生成配置文件的时候只能使用字符流解决。

      如果使用字节流生成配置文件的话,默认使用的是 iso8859-1码表进行编码存储,会出现乱码。

 2.  如果Properties配置文件中的内容发生改变,一定要重新使用Properties。 store()方法生成配置文件,否则配置文件信息不会发生改变。  

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class test_1 {	
	public static void main(String[] args) throws IOException, Exception   {					   		
		creatProperties();	
	//	readproperty();
	}
// 读取配置文件的信息 	
	public static void readproperty() throws FileNotFoundException, IOException {
		Properties properties = new Properties();		
		properties.load(new FileReader("J:/person.properties"));
		
		Set<Entry<Object, Object>> entrys = properties.entrySet();
		for(Entry<Object, Object> entry : entrys) {
			System.out.println("键:" + entry.getKey() +  "    值:" + entry.getValue());				
		}		
		properties.setProperty("付祖贤", "0101");    //  修改某人的密码
		properties.store(new FileWriter("J:/person.properties"), "fzx");  // 修改后的信息 再生成配置文件	
	}
	
// 创建配置文件的信息
	public static void creatProperties() throws FileNotFoundException, IOException {
		
		Properties properties = new Properties();   // 创建 Properties
		properties.setProperty("付祖贤", "1");
		properties.setProperty("祖贤", "2");
		properties.setProperty("贤", "3");

	//	properties.put(123, 123);   //  会发生错误,强转失败 !!  Integer cannot be cast to .String
		/*Set<Entry<Object, Object>> entrys = properties.entrySet();
		for(Entry<Object, Object> entry : entrys) {
			System.out.println("键:" + entry.getKey() +  "    值:" + entry.getValue());				
		}*/
		
	//	properties.store(new FileOutputStream("J:/person.properties"), "nvxia");    // "8859_1" 码表		
		properties.store(new FileWriter("J:/person.properties"), "fzx");	
	}
}           
2.  编码与解码

编码: 把看得懂的字符----》看不懂码值 

 *     getBytes(Charset charset) 使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组    

解码: 把码值查找对应的字符,

 *       String(byte[] bytes, String charsetName)  通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。

注意: 以后编码与解码都使用统一的码表,否则容易出现乱码。

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class test_1 {	
	public static void main(String[] args) throws UnsupportedEncodingException  {
	  /*String str = "a中国";
	    byte[] buf = str.getBytes("utf-8");   //  平台默认的编码表是gbk编码表。 编码
	    System.out.println(Arrays.toString(buf));
		
		str = new String(buf, "utf-8");    //  默认使用 gbk解码
		System.out.println("解码后的字符串: " + str);*/
				
	/*	String str = "a中国";   //  [-2, -1, 0, 97, 78, 45, 86, -3]    -2,-1 是标志而已
		byte[] buf = str.getBytes("utf-16");  // 编码和解码的时候指定为unicode,实际上是 utf-16
		System.out.println(Arrays.toString(buf));  */
				
		String str = "小仙女";
		byte[]  buf = str.getBytes();     // 默认使用 gbk
		System.out.println("数组: " + Arrays.toString(buf));    //  [-48, -95, -49, -55, -59, -82]	
		str = new String(buf, "iso8859-1");		
		System.out.println(str);
	//  出现乱码之后都可以被  还原  吗?   不一定,因为有些码值在所有的表中不一定统一存在
		byte[] buf2 = str.getBytes("iso8859-1");
	        String	str2 = new String(buf2, "gbk");
		System.out.println(str2);		
	}
}

**********************************  output  ******************************************
数组: [-48, -95, -49, -55, -59, -82]
??????
小仙女           
3.  转换流(InputStreamRead, OutputStreamWrite)

  转换流: 

 *    输入字节流的转换流:  InputStreamRead   字节流-------》字符流

               是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。

 *    输出字节流的转换流:   OutputStreamWrite  输出字节流-------》 输出字符流    

 *   转换流的作用:

     1.  如果目前所获取到的是一个字节流需要转换字符流使用,这时候就可以使用转换流;

     2.  使用转换流可以指定编码进行读写文件  

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class test_1 {	
	public static void main(String[] args) throws IOException  {
	//	readTest();
	//	writeTest();
	//	write2();
		read2();	
	}
// 	
	public static void readTest() throws IOException {
		InputStream in = System.in;  // 获取标准的输入流
		System.out.println("读到的字符: " + (char)in.read());  // read() 一次只能读一个字符	
		// 需要把字节流转换成字符流	
		InputStreamReader inputStreamReader = new InputStreamReader(in);
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);   // 应该是字符流
		String line = null;
		while((line = bufferedReader.readLine()) !=null) {		
			System.out.println(line);		
		}		
	}
// 	
	public static void writeTest() throws IOException {		
		File file = new File("J:/a.txt");
		FileOutputStream fileOutputStream = new FileOutputStream(file);  // 字节流
	//	fileOutputStream.write("付祖贤".getBytes());	
		// 把输出字节流转换成字符流
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
		outputStreamWriter.write("付祖贤");
		outputStreamWriter.close();		
	}	
// 使用输出字节流的转换流 指定码表 写出数据
	public static void write2() throws IOException {
		File file = new File("J:/a.txt");
		FileOutputStream fileOutputStream = new FileOutputStream(file);  // 字节流
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
		outputStreamWriter.write("付祖贤女神经");
		outputStreamWriter.close();		
	}	
// 使用输入字节流的转换流  指定码表 进行读取文件数据
	public static void read2() throws UnsupportedEncodingException, IOException {
		File file = new File("J:/a.txt");
		FileInputStream fileInputStream = new FileInputStream(file);   // 字节流
		InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");   //转换表,必须使用原来使用的码表"utf-8"
		char[] buf = new char[15];
		int length = 0;
		while ((length = inputStreamReader.read(buf)) !=-1) {			
			System.out.println(new String(buf,0,length));		
		}	
		inputStreamReader.close();				
	}
}