中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

JAVA導(dǎo)出成CSV文件

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
import java.io.File;
import java.io.IOException;
import java.util.List;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.common.primitives.Bytes;

public class FooUtilsCsvHelper {

    // csv's default delemiter is ','
    private final static String DEFAULT_DELIMITER = ",";
    // Mark a new line
    private final static String DEFAULT_END = "\r\n";
    // If you do not want a UTF-8 ,just replace the byte array.
    private final static byte commonCsvHead[] = { (byte) 0xEF, (byte) 0xBB,
            (byte) 0xBF };

    /**
     * Write source to a csv file
     * 
     * @param source
     * @throws IOException
     */
    public static void writeCsv(List<List<String>> source) throws IOException {
        // Aoid java.lang.NullPointerException
        Preconditions.checkNotNull(source);
        StringBuilder sbBuilder = new StringBuilder();
        for (List<String> list : source) {
            sbBuilder.append(Joiner.on(DEFAULT_DELIMITER).join(list)).append(
                    DEFAULT_END);
        }
        Files.write(Bytes.concat(commonCsvHead,
                sbBuilder.toString().getBytes(Charsets.UTF_8.toString())),
                new File("d:\\/123.csv"));
    }

    /**
     * Simple read a csv file
     * 
     * @param file
     * @throws IOException
     */
    public static void readCsv(File file) throws IOException {
        System.out.println(Files.readFirstLine(file, Charsets.UTF_8));
    }

    // Run a small test yourself.
    public static void main(String[] args) throws IOException {
        List<List<String>> source = Lists.newArrayList();
        List<String> tmpL = Lists.newArrayList();
        tmpL.add("測(cè)試titile1");
        tmpL.add("測(cè)試titile2");
        source.add(tmpL);
        writeCsv(source);
        readCsv(new File("d:\\/123.csv"));
    }
}

標(biāo)簽: Google

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:一個(gè)根據(jù)URI定位到spring mvc映射代碼工具類

下一篇:Java圖片處理工具類