码迷,mamicode.com
首页 > 编程语言 > 详细

Java(springboot) 读取txt文本内容

时间:2018-08-22 16:43:43      阅读:5257      评论:0      收藏:0      [点我收藏+]

标签:vat   直接   ade   文件   append   stream   main   txt   java   

public class TxtTest {
private static final Logger logger = LoggerFactory.getLogger(TxtTest.class);
public static String readTxt(File file) throws IOException {
String s = "";
InputStreamReader in = new InputStreamReader(new FileInputStream(file),"UTF-8");
BufferedReader br = new BufferedReader(in);
StringBuffer content = new StringBuffer();
while ((s=br.readLine())!=null){
content = content.append(s);
}
return content.toString();
}

public static void main(String[] args) {
try {
        //通过绝对路径获取文件
String s1 = TxtTest.readTxt(new File("C:\\Users\\....\\du.txt"));
logger.info(s1);

        //spring boot中文件直接放在resources目录下
String s2 = TxtTest.readTxt(ResourceUtils.getFile("classpath:du.txt"));
logger.info(s2);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Java(springboot) 读取txt文本内容

标签:vat   直接   ade   文件   append   stream   main   txt   java   

原文地址:https://www.cnblogs.com/strideparty/p/9517713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!