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

JAVA逐行读取TXT文件

时间:2018-09-12 18:00:25      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:util   output   div   void   readline   ror   final   highlight   red   

package help;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

public class TXTParseUtils {

    private static final Integer ONE = 1;

    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<String, Integer>();

        /* 读取数据 */
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:/value.txt")),
                                                                         "UTF-8"));
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
          //数据以逗号分隔 String[] names = lineTxt.split(","); for (String name : names) { if (map.keySet().contains(name)) { map.put(name, (map.get(name) + ONE)); } else { map.put(name, ONE); } } } br.close(); } catch (Exception e) { System.err.println("read errors :" + e); } /* 输出数据 */ try { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("E:/value_map.txt")), "UTF-8")); for (String name : map.keySet()) { bw.write(name + " " + map.get(name)); bw.newLine(); } bw.close(); } catch (Exception e) { System.err.println("write errors :" + e); } } }

  

JAVA逐行读取TXT文件

标签:util   output   div   void   readline   ror   final   highlight   red   

原文地址:https://www.cnblogs.com/fulucky/p/9636090.html

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