Blogger Information
Blog 2
fans 0
comment 0
visits 2373
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Java——获取文件编码格式
MLinM
Original
1651 people have browsed it

java开发时在以流的方式读取文件内容时,往往会中文乱码,那么就要考虑到统一编码格式

InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);

获得文件编码  

  1. /**
  2. * 获得文件编码  
  3. * @param fileName
  4. * @return
  5. * @throws Exception
  6. */
  7. public static String codeString(String fileName) throws Exception {
  8. BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
  9. int p = (bin.read() << 8) + bin.read();
  10. bin.close();
  11. String code = null;
  12. switch (p) {
  13. case 0xefbb:
  14. code = "UTF-8";
  15. break;
  16. case 0xfffe:
  17. code = "Unicode";
  18. break;
  19. case 0xfeff:
  20. code = "UTF-16BE";
  21. break;
  22. default:
  23. code = "GBK";
  24. }
  25. return code;
  26. }

 

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post