日期:2014-05-20 浏览次数:20893 次
File file = new File("D:/aa.cer"); InputStreamReader isr; isr = new InputStreamReader(new FileInputStream(file),"utf-8"); String line=null; while((line=bf.readLine())!=null){ System.out.print(line); }
File file = new File("D:/aa.cer"); FileReader fr=null; try { fr = new FileReader(file); char[] temp=new char[(int) file.length()]; fr.read(temp); fr.close(); System.out.println(new String(temp)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
------解决方案--------------------
你确定你的D:/aa.cer文件时utf-8编码的吗?
------解决方案--------------------
public static void readGBKCodingFile(String fileName) {//读取GBK文件的内容 BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream( fileName), "gbk")); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (Exception e) { } finally { try { if (br != null) br.close(); } catch (IOException e) { } } } public static void readUTFCodingFile(String fileName) {//读取UTF-8文件的内容 BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream( fileName), "utf-8")); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (Exception e) { } finally { try { if (br != null) br.close(); } catch (IOException e) { } } }
------解决方案--------------------
你想转化文件格式,试试下面的两个方法
public static void convertGBKFileToUTFFile(String srcFileName, String destFileName) {//把GBK文件转换为UTF文件 BufferedReader br = null; ; BufferedWriter bw = null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream( srcFileName), "gbk")); bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(destFileName), "UTF-8")); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); } } catch (Exception e) { } finally { try { if (br != null) br.close(); if (bw != null) bw.close(); } catch (IOException e) { } } } public static void convertUTFFileToGBKFile(String srcFileName, String destFileName) {//把GBK文件转换为UTF文件 BufferedReader br = null; ; BufferedWriter bw = null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream( srcFileName), "utf-8")); bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(destFileName), "gbk")); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); } } catch (Exception e) { } finally { try { if (br != null) br.close(); if (bw != null) bw.close(); } catch (IOException e) { } } }