2014-11-23 67 views
0

我试图从.csv文件中使用java获取选择性数据。使用java在CSV文件上搜索范围数据

例如:

CSV文件包含:

Blue, 03/11/2014, 13:00, 10 
pink, 04/11/2014, 14:00, 15 
Red, 03/11/2014, 15:00, 50 

我想在Java中创建一个程序,将允许用户选择他们从该文件中想要的信息。

我一直工作在下面的例子,但只能够打印字符串,而不是日期/ intergers:

package csv; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Arrays; 
import java.util.Scanner; 


public class ReadCVS { 
    public static void main(String[] args) { 

     ReadCVS obj = new ReadCVS(); 
     obj.run(); 

    } 

    public void run() { 

     String csvFile = "GeoIPCountryWhois.csv"; 
     BufferedReader br = null; 
     String line = ""; 
     String cvsSplitBy = ","; 
     File file = new File("GeoIPCountryWhois.csv"); 
     Scanner in = null; 

     try { 

      br = new BufferedReader(new FileReader(csvFile)); 
      while ((line = br.readLine()) != null) { 

       // use comma as separator 
       String[] colour = line.split(cvsSplitBy); 

       //System.out.println(country[0]+ country[1] + country[2]+ country[3]); 

       for (int i = 0; i < colour.length; i++) { 
        if (colour[i].equals("Pressure")) { 
         System.out.println(colour[0] + colour[1] + colour[2] + colour[3]); //Matching the string and printing. 
        } 
       } 

      } 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (br != null) { 
       try { 
        br.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

     System.out.println("Done"); 
    } 

} 

任何帮助/提示将不胜感激!

谢谢

+2

问题很模糊,请详细解释。 – alterfox 2014-11-23 22:31:52

回答

1

我会推荐使用第三方库来解析csv文件。这可以让你专注于你正在尝试做的事情的本质,而不是陷入文件解析。看看,例如Apache Commons CSV。这将让你的代码看起来是这样的:

Reader in = new FileReader("GeoIPCountryWhois.csv"); 
DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in); 
for (CSVRecord record : records) { 
    String colour = record.get(0); 
    Date date = df.parse(record.get(1)); 
    String timeString = record.get(2); 
    Integer value = Integer.parseInt(record.get(3)); 
    // do what you want with the values here. 
} 

注意如何做一些额外的解析,如解析DateInteger。这样可以让您更轻松地过滤这些列,因为您可以进行比较。

如果您不希望第三方的依赖,你可以做类似你所拥有的东西:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 

while ((line = br.readLine()) != null) { 
    // use comma as separator 
    String[] columns = line.split(cvsSplitBy); 

    // extract the columns. 
    String colour = columns[0].trim(); 
    Date date = df.parse(columns[1].trim()); 
    String time = columns[2].trim(); 
    Integer otherValue = Integer.parseInt(columns[3].trim()); 

    // filter on the colour column. 
    if(colour.equals("Red")) { 
     System.out.printf("colour = %s, date = %s, time = %s, val = %d\n", colour, df.format(date), time, otherValue); 
    } 

} 

注意:此代码是如何呼吁所有列的String.trim()。这是为了防止分割线后在列周围留有空白。例如,"a, b, c".split(",")将导致String阵列{"a", " b", " c"}" b"" c"中有额外的空间。这可能是您只能在第一列进行过滤的错误的来源。

作为第三种选择,这是一种矫枉过正,你可以使用CsvJdbc。这为sql文件提供了JDBC接口,然后您可以执行SQL查询。我从来没有使用过这个库,但看起来很有趣。鉴于您正在尝试过滤CSV文件。

+0

谢谢,我会给它以前 – user1291606 2014-11-24 11:08:51

+0

修剪()修复你的代码? – 2014-11-25 00:52:07