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 '联通'
相关推荐
拓网科技 2020-11-23
85433664 2020-11-17
拓网科技 2020-11-13
mspgqrs 2020-10-19
xiaotutu0000 2020-10-15
lifan0 2020-09-25
移动互联技术酒歌 2020-09-18
何砝 2020-09-16
anyvip 2020-09-15
zrhCSDN 2020-09-11
SanBa 2020-08-14
hiarxiaoliang 2020-08-05
虞凌云 2020-07-29
程序员说书 2020-07-08
dm00 2020-06-28
foamy 2020-06-17
saradx 2020-06-09
ASIC验证 2020-06-02
xiaoxiaodedu 2020-05-27