【发布时间】:2018-11-25 17:55:30
【问题描述】:
我仍然是 Java 编程的初学者,所以如果我的问题过于复杂,我提前道歉。
我的程序是什么? 我正在构建一个基于 GUI 的程序。该程序的目标是加载 CSV、XML 或 JSON 文件,然后程序将数据存储到一个数组中。然后数据将显示在文本框中。最终,该程序将能够将数据绘制到图表中。
界面细节:
- 3 单选按钮 - 允许用户选择 CSV、XML 或 JSON
- 加载文件按钮
- 显示按钮 - 将数据显示到 textArea 中
- 显示图表按钮
- 文本区域
问题:我无法将数据存储到数组中。我相信这是因为数据的格式。例如,这是 CSV 文件的前 3 行:
millis,stamp,datetime,light,temp,vcc
1000, 1273010254, 2010/5/4 21:57:34, 333, 78.32, 3.54
2000, 1273010255, 2010/5/4 21:57:35, 333, 78.32, 3.92
3000, 1273010256, 2010/5/4 21:57:36, 344, 78.32, 3.95
(注意 - CSV/XML/JSON 文件中有 52789000 行数据)
CSV-Reader 类包含读取数据、将其存储到数组中然后将其存储到 dataList 的方法。
从上面的例子可以看出,一些数据类型有很大的不同。我在拆分/解析时间和日期变量时遇到了特别的麻烦。
这是我的 CSV-Reader 类代码目前的样子(再次,我为菜鸟代码道歉)。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CSVReader {
//create a class that will hold arraylist which will have objects representing all lines of the file
private List<Data> dataList = new ArrayList<Data>();
private String path;
public List<Data> getDataList() {
return dataList;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
//Create a method to read through the csv stored in the path
//Create the list of data and store in the dataList
public void readCSV() throws IOException{
//i will create connection with the file, in the path
BufferedReader in = new BufferedReader(new FileReader(path));
String line = null;
line = in.readLine();
while((line = in.readLine())!=null){
//I need to split and store in the temporary variable and create an object
String[] splits = line.split("\\s*(=>|,|\\s)\\s*");
long millis = Long.parseLong(splits[0].trim());
long stamp = Long.parseLong(splits[1].trim());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
System.out.println(splits[2].trim());
LocalDateTime dateTime = LocalDateTime.parse(splits[2].trim(), formatter);
LocalDate dateTime = dateTime.toLocalDate();
LocalTime time = dateTime.toLocalTime();
int light = Integer.parseInt(splits[3].trim());
double temp = Double.parseDouble(splits[4].trim());
double vcc = Double.parseDouble(splits[5].trim());
Data d = new Data(millis,stamp,datetime,light,temp,vcc);//uses constructor
//final job is to add this object 'd' onto the dataList
dataList.add(d);
}//end of while loop
}
任何帮助将不胜感激!
编辑 1 - 我认为日期和时间是单独的 CSV 标题。它们不是。因此,时间变量已从程序中删除。它已被替换为 datetime 变量。
编辑 2 - 我的程序现在正在读取 CSV 文件,直到 csv 的第 15 行
27000、1273010280、2010/5/4 21:58:0、288、77.74、3.88
控制台错误
Exception in thread "AWT-EventQueue-0"
java.time.format.DateTimeParseException: Text **'2010/5/4 21:58:0'** could not
be parsed at index 15
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
at CSVReader.readCSV(CSVReader.java:55)
at GUI$2.actionPerformed(GUI.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
【问题讨论】:
-
你为什么试图将日期解析为双精度?
-
因为我觉得那是最合适的……应该是什么?
-
您可能希望探索
java.time包以获取处理日期和时间的适当类型。