Java(springboot) 读取txt文本内容


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();
}
}
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM