在平时的开发中,为了测试方便,经常会用到从文件中读取数据作为字符串的操作,这里主要记录Java读取文件的几种方法。
注意:以下方法主要是为了在debug的时候mock数据使用,如果文件过大,可能会导致OOM,慎用
1. 通过 BufferedReader 方式读取
通过BufferedReader
的readLine
方法逐行读取
/**
* 通过BufferedReader读取
* @param path 文件的本地绝对路径
* @return
*/
public static String readAsString(String path) {
try {
BufferedReader reader = new BufferedReader(new FileReader(path));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
// 删除最后一个新行分隔符
stringBuilder.delete(stringBuilder.length() - ls.length(), stringBuilder.length());
reader.close();
String content = stringBuilder.toString();
return content;
}catch (Exception exception){
logger.error("readAsString error,path={},exception={}", path, ExceptionUtils.getStackTrace(exception));
return null;
}
}
2. 通过BufferedReader和char数组的方式
/**
* 通过BufferedReader和char数组的方式
* @param path 文件的本地绝对路径
* @return
*/
public static String readAsString2(String path) {
try {
BufferedReader reader = new BufferedReader(new FileReader(path));
StringBuilder stringBuilder = new StringBuilder();
char[] buffer = new char[1024];
int len = -1;
while ((len = reader.read(buffer)) != -1) {
stringBuilder.append(new String(buffer, 0, len));
buffer = new char[1024];
}
reader.close();
String content = stringBuilder.toString();
return content;
}catch (Exception exception){
logger.error("readAsString2 error,path={},exception={}", path, ExceptionUtils.getStackTrace(exception));
return null;
}
}
3. 通过FileInputStream和byte数组
/**
* 通过FileInputStream和byte数组
* @param path 文件的本地绝对路径
* @return
*/
public static String readAsString3(String path) {
try {
FileInputStream fis = new FileInputStream(path);
byte[] buffer = new byte[1024];
StringBuilder sb = new StringBuilder();
int len = -1;
while ((len = fis.read(buffer)) != -1) {
sb.append(new String(buffer, 0, len));
buffer = new byte[1024];
}
fis.close();
String content = sb.toString();
return content;
}catch (Exception exception){
logger.error("readAsString3 error,path={},exception={}", path, ExceptionUtils.getStackTrace(exception));
return null;
}
}
4. 通过Files的readAllBytes方法
/**
* 通过Files的readAllBytes方法
* @param path 文件的本地绝对路径
* @return
*/
public static String readAsString4(String path) {
try {
String content = new String(Files.readAllBytes(Paths.get(path)));
return content;
}catch (Exception exception){
logger.error("readAsString4 error,path={},exception={}", path, ExceptionUtils.getStackTrace(exception));
return null;
}
}
5. 通过Scanner类读取
/**
* 通过Scanner类读取
* @param path
* @return
*/
public static String readAsString5(String path) {
try {
Scanner scanner = new Scanner(Paths.get(path), StandardCharsets.UTF_8.name());
String content = scanner.useDelimiter("\\A").next();
scanner.close();
return content;
} catch (Exception exception) {
logger.error("readAsString5 error,path={},exception={}", path, ExceptionUtils.getStackTrace(exception));
return null;
}
}
6.通过Apache Commons IO FileUtils读取
需要先添加maven依赖
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
/**
* 通过Apache Commons IO FileUtils类读取
* 需要依赖commons-io包
*
* @param path 文件的本地绝对路径
* @return
*/
public static String readAsString6(String path) {
try {
String content = FileUtils.readFileToString(new File(path), StandardCharsets.UTF_8);
return content;
} catch (Exception exception) {
logger.error("readAsString6 error,path={},exception={}", path, ExceptionUtils.getStackTrace(exception));
return null;
}
}
7. git仓库
仓库地址:
https://gitee.com/huangchunhua13/hch-tools/blob/master/src/main/java/org/example/util/FileUtil.java
网友评论