文件转字符串方式 --- (环绕执行模式&行为参数化&函数式接口|Lambda表达式)
嗯,当然论方便的话,我们直接可以使用,
org.apache.commons.io.FileUtils;
String readFileToString(final File file, final String encoding)
这里紧作为一种思路学习
@FunctionalInterfacepublic
interface ObjectMethodFunctionalInterface {
void count(int i);
String toString(); //same to Object.toString
int hashCode(); //same to Object.hashCode
boolean equals(Object obj); //same to Object.equals
}
1)第一步;当需要更改逻辑代码是,需要重写代码,所以想到行为参数化
public static String processFile()throws IOException {
try(BufferedReader bufferedReader =
new BufferedReader(new FileReader("data.txt"))){
// return bufferedReader.readLine();
return bufferedReader.readLine()+bufferedReader.readLine();
}
2)第二步,使用函数式接口来传递一个行为
@FunctionalInterface
public interface BufferReaderProcessFile{
// 方法签名为 BufferReader -> String
String peocess(BufferedReader bufferedReader)throws IOException;
}
3)第三步,执一个行为,任何BufferReader -> String的Lambda表达式都可以作为参数传入。只要符合peocess方法的签名即可。
public static String processFiles(BufferReaderProcessFile bufferReaderProcessFile)throws IOException {
try(BufferedReader bufferedReader =
new BufferedReader(new FileReader("data.txt"))){
return bufferReaderProcessFile.peocess(bufferedReader) ;
4)第四步,传递Lambda
String string = processFiles((BufferedReader bs) ->bs.readLine());
我的思路,我对java IO用的不是很熟,大家有好的方法请留言,相互学习:
所以我的思路是避免在逻辑里出现太多的IO流关闭,和异常捕获,专心处理读取逻辑即可,结合以下两种技术:
函数式接口传递行为的定义:
package com.liruilong.demotext.service.utils.interfaceutils;
import java.io.BufferedReader;
import java.io.IOException;
/**
* @Description : 函数接口,描述BufferedReader ->String的转化方式
* @Author: Liruilong
* @Date: 2020/3/17 15:44
*/
@FunctionalInterface
public interface InputStreamPeocess {
/**
* @Author Liruilong
* @Description 方法签名 BufferedReader ->String
* @Date 15:47 2020/3/17
* @Param [inputStream]
* @return com.liruilong.demotext.service.utils.InputStream
**/
String peocess(BufferedReader bufferedReader) throws IOException;
}
执一个行为,任何BufferReader -> String的Lambda表达式都可以作为参数传入。只要符合peocess方法的签名即可。
/**
* @return java.lang.String
* @Author Liruilong
* @Description 环绕处理
* @Date 17:14 2020/3/17
* @Param [inputStreamPeocess, file]
**/
public static String fileToBufferedReader(InputStreamPeocess inputStreamPeocess, File file) {
try (FileInputStream fileInputStream = new FileInputStream(file)) {
try (InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream)) {
try (BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
return inputStreamPeocess.peocess(bufferedReader);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
return null;
}
}
执行Lambda
/**
* @return java.lang.String
* @Author Liruilong
* @Description 文件转字符串
* @Date 17:22 2020/3/17
* @Param [file]
**/
public static String readJsonToString(File file) {
return string = fileToBufferedReader((bufferedReader) -> {
String str = null;
StringBuilder stringBuilder = new StringBuilder();
while ((str = bufferedReader.readLine()) != null) {
stringBuilder.append(str);
}
return stringBuilder.toString();
}, file);
}
不足之处希望小伙伴批评指教。生活加油..