我想在.CSV文件的第一列添加(插入)列号(从1到1000)

时间:2014-05-16 12:58:53

标签: java csv

例如

我的 input.csv 包含这样的数据..

Row_No,用户,操作

John,SQL事务

Suman,交易失败

Ram,按下按钮以检索细节

这就是我正在寻找的东西..感谢你们每个人尝试过这个

output.csv

Row_No,用户,操作

1,John,SQL事务

2,Suman,交易失败

3,Ram,按下按钮以检索细节

请帮帮我

这就是我尝试过的

public class MapWriter

{

public static void main(String[] args) throws IOException {

    CsvMapReader mapReader = null;

    ICsvMapWriter mapWriter = null;

    try 

     {

        CsvPreference prefs = CsvPreference.STANDARD_PREFERENCE;

        mapReader = new CsvMapReader(new FileReader("D:\\input.csv"), prefs);

        mapWriter = new CsvMapWriter(new FileWriter("D:\\output.csv"), prefs);

        // header used to read the original file

        final String[] readHeader = mapReader.getHeader(true);

        // header used to write the new file 

        // (same as 'readHeader', but with additional column)

        final String[] writeHeader = new String[readHeader.length + 1];

        System.arraycopy(readHeader, 0, writeHeader, 0, readHeader.length);

        final String timeHeader = "   ";

        writeHeader[writeHeader.length-1]= timeHeader;

        mapWriter.writeHeader(writeHeader);

        int count=1;

        Map<String, String> row;

        while( (row = mapReader.read(readHeader)) != null ) {

            // add your column with desired value

        row.put(timeHeader, String.valueOf(count));

        mapWriter.write(row, writeHeader);

            count++;  
        }


    }
    finally {
        if( mapReader != null )
        {
            mapReader.close();
        }

        if( mapWriter != null ) 
        {
            mapWriter.close();
        }
    }

}

}

1 个答案:

答案 0 :(得分:1)

据我所知,您正在尝试对无效数据使用验证CSV解析器(例如,Row_No在输入文件中的所有位置都是空白的)。我不确定你的解析器是否允许这样做,但我不相信你还需要解析文件。例如,我会使用StringTokenizerScanner这样做 -

public static void main(String[] args) {
    String inputFile = "c:/input.csv";
    String outputFile = "c:/output.csv";
    int lineNumber = 0;                    // <-- keep a line count.
    Scanner scanner = null;
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(outputFile);  // <-- output
        File source = new File(inputFile);
        scanner = new Scanner(source);     // <-- input
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            line = (line != null) ? line.trim() : "";
            if (line.length() < 1) {
                continue;
            }
            // line 0 is the header.
            if (lineNumber != 0) {
                pw.print(lineNumber);
                pw.print(", ");
            }
            int tokenCount = 0;
            StringTokenizer st = new StringTokenizer(line, ",");
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (tokenCount != 0) {
                    pw.print(", ");
                }
                pw.print(token.trim());
                tokenCount++;
            }
            pw.println();
            lineNumber++;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (scanner != null) {
            scanner.close();
        }
        if (pw != null) {
            pw.close();
        }
    }
}

运行上述内容后,我生成了output.csv(基于您的input.csv) -

Row_No, User, Actions
1, John, SQL transaction
2, Suman, Transaction failed
3, Ram, Button pressed to retrieve details