天天看点

java之读取文本字符串

步骤如下:增加引用

<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.3</version>
		</dependency>
           

读取文件

import org.apache.commons.io.IOUtils;
           
ClassPathResource classPathResource = new ClassPathResource("/hkjson");
        InputStream inputStream =classPathResource.getInputStream();
        String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
           

另外,文件防止在资源目录下面,如下:

java之读取文本字符串