java读取utf8类型的文件

源文件为utf-8格式,且里面包含中文,如:

in ('133','153','180','189') then '电信'

in ('130','131','132','145','155','156','183','185','186') then '联通'

1,不加转码的时候中文会乱码:

public static void main(String[] args) throws Exception {
		File f = new File("D:/youni_insert_youni_file_transfer_ft.pl");
		InputStreamReader insReader = new InputStreamReader(new FileInputStream(f));
		BufferedReader bufReader = new BufferedReader(insReader);
		String line = new String();
		while ((line = bufReader.readLine()) != null) {
			System.out.println(line);
		}
		bufReader.close();
	}
 
in ('133','153','180','189') then '鐢典俊'
in ('130','131','132','145','155','156','183','185','186') then '鑱旈?'

2,如果对string进行utf-8编码,某些中文加单引号会导致乱码:

public static void main(String[] args) throws Exception {
		//String charsetName = "UTF-8"; 
		File f = new File("D:/youni_insert_youni_file_transfer_ft.pl");
		InputStreamReader insReader = new InputStreamReader(new FileInputStream(f));
		BufferedReader bufReader = new BufferedReader(insReader);
		String line = new String();
		while ((line = bufReader.readLine()) != null) {
			System.out.println(new String(line.getBytes(),"utf-8"));
		}
		bufReader.close();
	}
 
in ('133','153','180','189') then '电信'
in ('130','131','132','145','155','156','183','185','186') then '联??'

3,读文件时候用utf8编码读,则不会有问题了:

public static void main(String[] args) throws Exception {
		String charsetName = "UTF-8"; 
		File f = new File("D:/youni_insert_youni_file_transfer_ft.pl");
		InputStreamReader insReader = new InputStreamReader(new FileInputStream(f), charsetName);
		BufferedReader bufReader = new BufferedReader(insReader);
		String line = new String();
		while ((line = bufReader.readLine()) != null) {
			System.out.println(line);
		}
		bufReader.close();
	}
 
in ('133','153','180','189') then '电信'
in ('130','131','132','145','155','156','183','185','186') then '联通'
 

相关推荐