java 读取TXT文件,以逗号分隔文件内容。
读取TXT文件,以逗号分隔文件的每一行内容。
1、上代码:
import java . io . * ; public class TxtTest { 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 ) . append ( "," ) ; } //返回的时候把最后一个逗号去掉 return content . deleteCharAt ( content . length ( ) - 1 ) . toString ( ) ; } public static void main ( String [ ] args ) { try { //通过绝对路径获取文件 String s1 = TxtTest . readTxt ( new File ( "C:\\HGproject\\apache-tomcat-8.5.38\\webapps\\V5008Upload.txt" ) ) ; System . out . println ( "文件内容:" + s1 ) ; //spring boot中文件直接放在resources目录下 // String s2 = TxtTest.readTxt(ResourceUtils.getFile("classpath:du.txt")); } catch ( IOException e ) { e . printStackTrace ( ) ; } } }
如有侵权请及时联系我们处理,转载请注明出处来自
推荐文章
科技快看 网站地图广州壹创集信息科技有限公司 版权所有 粤ICP备2021122624号