思路:通过流读取,然后拼接
这个也比较简单,学过流的同学应该都会,只不过长时间不用就都忘了,这个是封装好的,我们可以拿过来直接用,废话不多说,直接上代码;
代码:
//filePathAndName : 你要转换的html的 绝对路径
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();
}
System.out.println("fileContent:"+fileContent);
return fileContent;
}
很多时候我们不需要把这些东西都记住,因为他会占用我们很多的精力和时间,我们只需要在袄子中有个印象,当有需求的时候我们做到能够知道使用什么去解决,至于怎么写我们没有必要去记住,因为我们可以百度....当然如果你能记住的话更好。。。