您的位置:首页 > 编程语言 > Java开发

java 读取文件内容

2016-01-14 14:03 441 查看
//读取方法

public static String txt2String(String path){
  String fileContent = "";
  try
   {
    File f = new File(path);
    if(f.isFile()&&f.exists())
    {
      InputStreamReader read = new InputStreamReader(new FileInputStream(f),"gbk");
      BufferedReader reader=new BufferedReader(read);
      String line;
      while ((line = reader.readLine()) != null)
      {
        fileContent += line;
      }
        read.close();
    }
  } catch (Exception e)
  {
    e.printStackTrace();
  }
  return fileContent;
}

//测试main方法
public static void main(String[] args) {
String a=txt2String("C:/Users/A12080084/Desktop/Test.html");
System.out.println(a);
System.out.println(a.length());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: