我試圖從.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");
}
}
任何幫助/提示將不勝感激!
謝謝
問題很模糊,請詳細解釋。 – alterfox 2014-11-23 22:31:52