当前位置: 首页 > 知识库问答 >
问题:

读取远程。使用opencsv的csv文件

封昊天
2023-03-14

我已经考虑了很长一段时间了。我正在尝试从雅虎的股票API下载数据。当您使用 API 时,它会为您提供一个.csv文件。我一直在看opencsv,这似乎很完美,除了我想避免下载和保存文件,如果可能的话。

根据示例,OpenCSV只能从FileReader中读取。根据Oracle关于FileReader的文档,文件需要是本地的。

是否可以使用 OpenCSV 从远程文件读取而不下载?

共有2个答案

海宁
2023-03-14

opencsv的实现,用于读取csv文件并保存到数据库。

import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.bean.CsvBindByPosition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.Column;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class FileUploadService {

    @Autowired
    private InsertCSVContentToDB csvContentToDB;


    /**
     * @param csvFileName location of the physical file.
     * @param type Employee.class
     * @param delimiter  can be , | # etc
     * @param obj    //new Employee();
     *
     *            import com.opencsv.bean.CsvBindByPosition;
     *            import lombok.Data;
     *
     *            import javax.persistence.Column;
     *
     *              @Data
     *              public class Employee {
     *
     *              @CsvBindByPosition(position = 0, required = true)
     *              @Column(name = "EMPLOYEE_NAME")
     *              private String employeeName;
     *
     *              @CsvBindByPosition(position = 1)
     *              @Column(name = "Employee_ADDRESS_1")
     *              private String employeeAddress1;
     *          }
     *
     * @param sqlQuery  query to save data to DB
     * @param noOfLineSkip make it 0(Zero) so that it should not skip any line.
     * @param auditId apart from regular column in csv we need to add more column for traking like file id or audit id
     * @return
     */
    public <T> void readCSVContentInArray(String csvFileName, Class<? extends T> type, char delimiter, Object obj,
                                                  String sqlQuery, int noOfLineSkip, Long auditId) {
        List<T> lstCsvContent = new ArrayList<>();
        Reader reader = null;
        CSVReader csv = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(csvFileName), "utf-8"));
            log.info("Buffer Reader : " + ((BufferedReader) reader).readLine().isEmpty());
            CSVParser parser = new CSVParserBuilder().withSeparator(delimiter).withIgnoreQuotations(true).build();
            csv = new CSVReaderBuilder(reader).withSkipLines(noOfLineSkip).withCSVParser(parser).build();
            String[] nextLine;
            int size = 0;
            int chunkSize = 10000;
            Class params[] = { Long.class };
            Object paramsObj[] = { auditId };
            long rowNumber = 0;
            Field field[] = type.getDeclaredFields();
            while ((nextLine = csv.readNext()) != null) {
                rowNumber++;
                try {

                    obj = type.newInstance();

                    for (Field f : field) {
                        if(!f.isSynthetic()){
                            f.setAccessible(true);
                            Annotation ann[] = f.getDeclaredAnnotations();
                            CsvBindByPosition csv1 = (CsvBindByPosition) ann[0];
                            Column c = (Column)ann[1];

                            try {
                                if (csv1.position() < nextLine.length) {
                                    if (csv1.required() && (nextLine[csv1.position()] == null
                                            || nextLine[csv1.position()].trim().isEmpty())) {
                                        String message = "Mandatory field is missing in row: " + rowNumber;
                                        log.info("null value in " + rowNumber + ", " + csv1.position());
                                        System.out.println(message);

                                    }

                                    if (f.getType().equals(String.class)) {
                                        f.set(obj, nextLine[csv1.position()]);

                                    }
                                    if (f.getType().equals(Boolean.class)) {
                                        f.set(obj, nextLine[csv1.position()]);

                                    }
                                    if (f.getType().equals(Integer.class)) {
                                        f.set(obj, Integer.parseInt(nextLine[csv1.position()]));

                                    }
                                    if (f.getType().equals(Long.class)) {
                                        f.set(obj, Long.parseLong(nextLine[csv1.position()]));
                                    }
                                    if (f.getType().equals(Double.class) && null!=nextLine[csv1.position()] && !nextLine[csv1.position()].trim().isEmpty()  ) {
                                        f.set(obj, Double.parseDouble(nextLine[csv1.position()]));

                                    }if(f.getType().equals(Double.class) && ((nextLine[csv1.position()]==null) || nextLine[csv1.position()].isEmpty())){
                                        f.set(obj, new Double("0.0"));
                                    }
                                    if (f.getType().equals(Date.class)) {
                                        f.set(obj, nextLine[csv1.position()]);
                                    }
                                }
                            } catch (Exception fttEx) {
                                log.info("Exception when parsing the file: " + fttEx.getMessage());
                                System.out.println(fttEx.getMessage());
                            }
                        }
                    }
                    lstCsvContent.add((T) obj);
                    if (lstCsvContent.size() > chunkSize) {
                        size = size + lstCsvContent.size();
                        //write code to save to data base of file system in chunk.
                        lstCsvContent = null;
                        lstCsvContent = new ArrayList<>();
                    }

                } catch (Exception ex) {
                    log.info("Exception: " + ex.getMessage());
                }

            }
            //write code to save list into DB or file system
            System.out.println(lstCsvContent);
        } catch (Exception ex) {
            log.info("Exception:::::::: " + ex.getMessage());

        } finally {
            try {
                if (csv != null) {
                    csv.close();
                }
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException ioe) {

                log.info("Exception when closing the file: " + ioe.getMessage());
            }
        }
        log.info("File Processed successfully: ");
    }


}
燕烨
2023-03-14

CSVReader 根据文档采用读取器参数,因此它不限于参数的文件读取器

要在不首先保存文件的情况下使用CSVReader,您可以在加载数据的流周围使用BufferedReader

URL stockURL = new URL("http://example.com/stock.csv");
BufferedReader in = new BufferedReader(new InputStreamReader(stockURL.openStream()));
CSVReader reader = new CSVReader(in);
// use reader
 类似资料:
  • 我使用OpenCSV进行CSV文件的读写。在I hsd安装早期版本的Java(即Java 6)之前,它工作得很好。安装完之后,我的代码就停止工作了: 线程“main”java.lang.UnsupportedClassVersionError:COM/OpenCSV/CSVReader:不支持Major.Minor版本51.0在java.lang.ClassLoader.DefineClass1(

  • 我正在尝试使用Saxon 9将csv文件转换为xml文件。但是当我尝试检查存在并读取带有xslt函数的csv文件时,我遇到了一个问题:unparsed-text-可用(),unparsed-text() 它们可以很好地处理本地文件,但当我将远程文件作为参数传递时,unparsed-text-available()返回false。 例如,当我通过“D:\test\test.csv”时,它就工作了。当

  • 本文向大家介绍Java中使用opencsv读写csv文件示例,包括了Java中使用opencsv读写csv文件示例的使用技巧和注意事项,需要的朋友参考一下 OpenCSV是一个简单的用于解析CSV文件的java类库,它封装了CSV格式文件的输出和读入,可以自动处理CSV格式中的特殊字符,最重要的是OpenCSV可以用于商业化(commercial-friendly)。具体的使用方法: 读CSV文件

  • 问题内容: 我正在寻找一种简单的方法来获取位于远程服务器上的文件。为此,我在Windows XP上创建了本地ftp服务器,现在我想为测试Applet提供以下地址: 当然,我会收到以下错误: URI方案不是“文件” 我一直在尝试其他方法来获取文件,但它们似乎没有用。我该怎么办?(我也很想执行HTTP请求) 问题答案: 您不能使用ftp开箱即用。 如果文件位于http上,则可以执行以下操作: 如果要使

  • 我有一个简单的Hadoop安装有点麻烦。我已经下载了hadoop 2.4.0并安装在一个CentOSLinux节点(虚拟机)上。我已经为apache站点(http://hadoop.apache.org/docs/r2.4.0/hadoop-project-dist/hadoop-common/SingleCluster.html)上描述的具有伪分布的单个节点配置了hadoop。它从日志中没有问题

  • 我正在使用http://csvjdbc.sourceforge.net/doc.html要将磁盘上的CSV文件(例如“myDir”中的“myFile”)视为SQL DB,我可以使用SQL语法进行查询: 这工作正常,但是当CSV文件没有头时,我遇到了麻烦。在那种情况下,第一数据线被认为是报头并且因此不像其他数据线那样被读取。 有没有办法告诉查询不要寻找标头,而将第一行视为数据输入?