/**
* Created by @author xu on @date 2018年 1月 10日
*/
public class IOUtils {
public static String ReadFile(String filepath) {
String str = ""; // 保存读取的全部内容
StringBuffer sb = new StringBuffer(str); // 使用StringBuffer来拼接字符串
File file = new File(filepath);
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
int len = 0;
byte[] temp = new byte[1024];
while ((len = bis.read(temp)) != -1) {
sb.append(new String(temp, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally { // 关闭流资源
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString(); // 将读取的结果以字符串的形式返回
}
}
使用BufferedInputStream来实现读取本地文件
最新推荐文章于 2023-07-23 14:51:21 发布