javacsv阅读器(代码片段)

author author     2022-12-22     600

关键词:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

/**
 * A simplified version of CSV Reader.
 *
 * Subclass of a BufferedReader to handle a character stream that consists of
 * comma separated values (CSVs)
 *
 * Provides an additional instance method, readCSVLine(), that parses lines into
 * substrings. The substrings are separated by comma in the original input
 * stream. The readCSVLine() method returns an array of references to Strings.
 * The Strings are the values from the line that were separated by commas. If a
 * value was surrounded by quotes, the quotes are removed.
 *
 * Limitations: Spaces before or after the commas are not removed. In the first
 * and last quote are removed from a value. Embedding commas in a quoted value
 * is not handled properly. (In this case, the commas will separate the values
 * and the quotes will not be removed from the ends of those values.
 *
 * @author Jeffrey Eppinger (jle@cs.cmu.com)
 * Date: October 2, 2007
 */
public class CSVReader extends BufferedReader 
    /**
     * Initializes the class.
     * @param in the reader from which to read CSV lines
     */
    public CSVReader(Reader in) 
        super(in);
    

    /**
     * This is the only additional method. It uses readLine from the superclass
     * to get a line but returns the comma separated values as in an array of
     * strings.
     * @return an array of Strings containing the values At the end of the file,
     *         readCSVLine returns null (just as readLine does).
     * @throws IOException throws IOException
     */
    public String[] readCSVLine() throws IOException 

        // Get a line by calling the superclass's readLine method
        String line = super.readLine();

        // If we're at the end of the file, readLine returns null
        // so we return null.
        if (line == null) 
            return null;
        

        // Count up the number of commas
        int commaCount = 0;
        for (int i = 0; i < line.length(); i++) 
            if (line.charAt(i) == ',') 
                commaCount = commaCount + 1;
            
        

        // Allocate an array of the necessary size to return the strings
        String[] values = new String[commaCount + 1];

        // In a loop, set beginIndex and endIndex to the start and end
        // positions of each argment and then use the substring method
        // to create strings for each of the comma separate values

        // Start beginIndex at the beginning of the String, position 0
        int beginIndex = 0;

        for (int i = 0; i < commaCount; i++) 
            // set endIndex to the position of the (next) comma
            int endIndex = line.indexOf(',', beginIndex);

            // if the argument begins and ends with quotes, remove them
            if (line.charAt(beginIndex) == '"' && line.charAt(endIndex - 1) == '"') 

                // If we made it here, we have quotes around our string.
                // Add/substract one from the start/end of the args
                // to substring to get the value. (See else comment
                // below for details on how this works.)
                values[i] = line.substring(beginIndex + 1, endIndex - 1);

             else 
                // If we name it here, we don't have quotes around
                // our string. Take the substring of this line
                // from the beginIndex to the endIndex. The substring
                // method called on a String will return the portion
                // of the String starting with the beginIndex and up
                // to but not including the endIndex.
                values[i] = line.substring(beginIndex, endIndex);
            

            // Set beginIndex to the position character after the
            // comma. (Remember, endIndex was set to the position
            // of the comma.)
            beginIndex = endIndex + 1;
        

        // handle the value that's after the last comma
        if (line.charAt(beginIndex) == '"' && line.charAt(line.length() - 1) == '"') 
            values[commaCount] = line.substring(beginIndex + 1, line.length() - 1);
         else 
            values[commaCount] = line.substring(beginIndex, line.length());
        

        return values;
    

利用javacsv实现java读写csv文件(代码片段)

今天跟大家分享一个利用外部Jar包来实现Java操作CSV文件一.资源下载1.直接下载Jar包:http://central.maven.org/maven2/net/sourceforge/javacsv/javacsv/2.0/javacsv-2.0.jar2.利用Maven下载Jar包:<dependency><groupId>net.so 查看详情

读取csv文件(代码片段)

<!--csv--><dependency><groupId>net.sourceforge.javacsv</groupId><artifactId>javacsv</artifactId><version>2.0</version></dependency>Map<String,CoordinateDto>map=newHashMap<>();@PostConstructprivateMap<String,CoordinateDto>generat... 查看详情

javacsv之写csv文件

与JavaCSV读CSV文件相对应,JavaCSV也可以用来写数据到CSV文件中。1.准备工作(1)第三方包库下载地址:https://sourceforge.net/projects/javacsv(2)相关文档:http://javacsv.sourceforge.net/2.使用简单的读操作(1)引入javacsv包相应的类importcom.cs... 查看详情

javacsv之读csv文件

...数据处理,有时候难免有进行CSV文件的操作,这里采用了JavaCSV读CSV文件。1.准备工作(1)第三方包库下载地址:https://sourceforge.net/projects/javacsv(2)相关文档:http://javacsv.sourceforge.net/2.使用简单的读操作(1)引入javacsv包importcom.... 查看详情

javacsv生成的csv用excel打开中文乱码

在SourceForge上找到读写csv文件的开源代码。但是生成的csv文件,如果用excel打开时,中文全部会乱码。原因是excel在解析csv文件时,默认以utf-8带BOM格式去解析的。utf-8保存的csv格式文件要让Excel正常打开的话,必须加入在文件最前... 查看详情

csv操作(代码片段)

需要引入javacsv.jar以下为一个完整的Utils的写法,具体输出和输入需要自己修改参数。importjava.io.File;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.nio.charset.Charset;importjava.util.Random;importorg.springf 查看详情

golanggolang阅读器(代码片段)

查看详情

csharpexcel阅读器(代码片段)

查看详情

html晓月阅读器(代码片段)

查看详情

markdown小说阅读器(代码片段)

查看详情

xml自动短信阅读器(代码片段)

查看详情

csharp数据阅读器到csv(代码片段)

查看详情

php简单的csv阅读器(代码片段)

查看详情

css屏幕阅读器,text.css(代码片段)

查看详情

python一个有点问题的队列阅读器。(代码片段)

查看详情

css屏幕阅读器,text.css(代码片段)

查看详情

javascriptxls阅读(代码片段)

查看详情

pdf(代码片段)

福昕(foxit)pdf阅读器:https://www.foxitsoftware.cn/downloads/SmartPDF阅读器:http://www.pdfyuedu.com/  查看详情