JavaIO读取文件中文乱码问题

2014-11-13 23:00:07 · 作者: · 浏览: 26

  1、JAVA读取文件,避免中文乱码。


  /**


  * 读取文件内容


  *


  * @param filePathAndName String 读取文件路径


  * @return String 文件中的内容


  */


  public static String readFile(String filePathAndName) {


  String fileContent = "";


  try {


  File f = new File(filePathAndName);


  if(f.isFile()&&f.exists()){


  InputStreamReader read = new InputStreamReader(new FileInputStream(f),"UTF-8");


  BufferedReader reader=new BufferedReader(read);


  String line;


  while ((line = reader.readLine()) != null) {


  fileContent += line;


  }


  read.close();


  }


  } catch (Exception e) {


  System.out.println("读取文件内容操作出错");


  e.printStackTrace();


  }


  return fileContent;


  }