【问题标题】:Java Program to read .csv and give sorted .xml output读取 .csv 并给出排序的 .xml 输出的 Java 程序
【发布时间】:2014-03-25 07:08:07
【问题描述】:

我有一个 .csv 文件作为输入。我需要一个 Java 程序来为我读取此文件并根据 csv 中的日期字段生成排序输出。输出文件格式必须为 .xml

我有以下代码,虽然我是 JAVA 新手,请帮忙。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Sort {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader("data1.csv"));
        Map<String, List<String>> map = new TreeMap<String, List<String>>();
        String line = reader.readLine();//read header
        while ((line = reader.readLine()) != null) {
            String key = getField(line);
            List<String> l = map.get(key);
            if (l == null) {
                l = new LinkedList<String>();
                map.put(key, l);
            }
            l.add(line);

        }
        reader.close();
        FileWriter writer = new FileWriter("sorted_numbers.txt");
        writer.write("UserID, Module, Mark\n");
        for (List<String> list : map.values()) {
            for (String val : list) {
                writer.write(val);
                writer.write("\n");
            }
        }
        writer.close();
    }

    private static String getField(String line) {
        return line.split(",")[0];// extract value you want to sort on
    }
}

【问题讨论】:

  • 代码中的错误是什么?你可以发布你的控制台输出吗?

标签: java xml csv


【解决方案1】:

你可以在java中使用csvreader来有效地读取csv文件而不是BufferedReader

Read a csv file

【讨论】:

    【解决方案2】:
       public class ReadCVS {
    
    public static void main(String[] args) throws ParseException {
    
        ReadCVS obj = new ReadCVS();
        obj.run();
    
    }
    
    public void run() throws ParseException {
    
        String csvFile = "config/one.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";
    
        try {
    
            Map<Date, String> maps = new HashMap<Date, String>();
    
            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {
    
                String[] country = line.split(cvsSplitBy);
                SimpleDateFormat sdf = new SimpleDateFormat("dd:MM:yyyy");
                Date date = sdf.parse(country[2]);
    
                                //country[2] is date
                maps.put(date, line);
    
            }
            System.out.println("Unsort Map......");
            // printMap(maps);
            System.out.println("Sorted Map......");
            Map<Date, String> treeMap = new TreeMap<Date, String>(maps);
            printMap(treeMap);
    
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        System.out.println("Done");
    }
    
    public static void printMap(Map<Date, String> map) {
        for (Map.Entry entry : map.entrySet()) {
            System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
        }
    }
    

    }

    【讨论】:

    • **example csv **: 1.0.0.0 ,1.0.0.255,12:12:14,16777471,AU,Australia 1.0.1.0,1.0.3.255,12:01:12,16778239,中国,中国
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2017-10-24
    • 1970-01-01
    • 2020-06-29
    • 2011-01-24
    • 1970-01-01
    相关资源
    最近更新 更多