标签:
一、读取txt文件。
场景 : txt文件内容 :
123
456
...
读取每行内容, 并将每行内容用 , 拼接成 123,456
public class ReadTxt { public static void main(String[] args) { String filePath = "/Users/xupengwei/Downloads/test.txt"; readTxtFile(filePath ); } public static void readTxtFile(String filePath) { try { String encoding = "GBK"; File file = new File(filePath); if (file.isFile() && file.exists()) { // 判断文件是否存在 InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; StringBuffer sb = new StringBuffer(); while ((lineTxt = bufferedReader.readLine()) != null) { sb.append(lineTxt); sb.append(","); } System.out.println(sb.toString()); read.close(); } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } } }
二、拓展...
场景 : 正则匹配
标签:
原文地址:http://www.cnblogs.com/chenmo-xpw/p/5578614.html