简体   繁体   English

读取Java中的CSV文件

[英]Read a CSV file in Java

I wrote a programm that reads a csv file and puts it into a TableModel. 我编写了一个程序,用于读取csv文件并将其放入TableModel中。 My problem is that I want to expand the programm so, that if the csv file gets changes from outside my tablemodel gets updated and gets the new values. 我的问题是我想扩展程序,以便如果csv文件从我的表模型外部获取更改,则更新并获取新值。

I would now programm a scheduler so that the thread sleeps for about a minute and checks it every minute if the timestamp of the file changed. 现在,我将对一个调度程序进行编程,以便该线程休眠约一分钟,如果文件的时间戳发生更改,则每分钟检查一次。 If so it would read the file again. 如果是这样,它将再次读取该文件。 But i dont know what happens to the whole programm if i use a scheduler because this little software i write will be a part of a much much bigger software wich is running on JDK 6. So I search for a performant and independent from the bigger software solution to get the changes in the tablemodel. 但是我不知道如果我使用调度程序会对整个程序产生什么影响,因为我编写的这个小软件将成为JDK 6上运行的更大软件的一部分。因此,我寻找性能卓越且独立于更大软件的软件解决方案以获取表模型中的更改。

Can someone help out? 有人可以帮忙吗?

java.nio.file package now contains the Watch Service API . java.nio.file软件包现在包含Watch Service API This, effectively: 有效地:

This API enables you to register a directory (or directories) with the watch service. 使用此API,您可以通过监视服务注册一个或多个目录。 When registering, you tell the service which types of events you are interested in: file creation, file deletion, or file modification. 注册时,您可以告诉服务您感兴趣的事件类型:文件创建,文件删除或文件修改。 When the service detects an event of interest, it is forwarded to the registered process. 当服务检测到感兴趣的事件时,会将其转发到注册的进程。 The registered process has a thread (or a pool of threads) dedicated to watching for any events it has registered for. 已注册的进程有一个线程(或线程池)专用于监视其已注册的任何事件。 When an event comes in, it is handled as needed. 当事件进入时,将根据需要进行处理。

See reference here . 请参阅此处的参考。

Oh! 哦! This API is only available from JDK 7 (onwards). 该API仅可从JDK 7(及以后)获得。

**OpenCsv is a best way to read csv file in java.
if your are using maven then you can use below dependency or download it's jar from web.**

 @SuppressWarnings({"rawtypes", "unchecked"})
  public void readCsvFile() {
    CSVReader csvReader;
    CsvToBean csv;
    File fileEntry; 
    try {
      fileEntry = new File("path of your file");
      csv = new CsvToBean();
      csvReader = new CSVReader(new FileReader(fileEntry), ',', '"', 1);
      List list = csv.parse(setColumMapping(), csvReader);
     //List of LabReportSampleData class
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  //Below function is used to map the your csv file to your mapping object. 
  //columns String array: The value inside your csv file. means 0 index map with degree variable in your mapping class.
  @SuppressWarnings({"rawtypes", "unchecked"})
  private static ColumnPositionMappingStrategy setColumMapping() {
    ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();
    strategy.setType(LabReportSampleData.class);
    String[] columns =
        new String[] {"degree", "radian", "shearStress", "shearingStrain", "sourceUnit"};
    strategy.setColumnMapping(columns);
    return strategy;
  } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM