工具类ClassPathResourceReader
import java.io.*;
import java.util.stream.Collectors;
import org.springframework.core.io.ClassPathResource;
public final class ClassPathResourceReader {
private final String path;
private String content;
public ClassPathResourceReader(String path) {
this.path = path;
}
public String getContent() {
if (content == null) {
BufferedReader reader = null;
try {
ClassPathResource resource = new ClassPathResource(path);
reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
content = reader.lines().collect(Collectors.joining("\n"));
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content;
}
}
String content = new ClassPathResourceReader("token.txt").getContent();
System.out.println(content);
Comments | 0 条评论