所以,我有一个类似这样的csv文件
Id | Stamp | Date | Value 1 | Value 2 | Value 3
01 | 14E+ | 2011/05 | 333 | 312 | 457
02 | 15T+ | 2011/05 | 432 | 345 | 354
等。 所以,我编写了一个程序,它使用JFileChooser并打开这个csv文件,将其存储到一个文件中,读取它并将其显示在文本区域中。我想做什么(如果我可以修复程序)基本上删除了ID Stamp和Date colums,然后将数据解析成双打,这样我就可以找到值1,值2和值3的平均值。有人可以请帮助我明白怎么做?我完全不了解CSV fiel处理得好。
程序 http://gyazo.com/b57563c6dfde12ec838584b8a93fdbb8
csv文件 http://gyazo.com/3b6dfa366df7d6d57a7c81e757e07db6
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.TextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;//Importing any required tools.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class CSVFiles extends JFrame { //Class, inherits properties of the JFrame.
private JPanel contentPane; //Create a container for the GUI.
//Create other components used in the GUI
private JTextField maxTxtVCC;
private JTextField maxTxtTemp;
private JTextField maxTxtLight;
private JTextField minTxtLight;
private JTextField avTxtLight;
private JTextField minTxtTemp;
private JTextField avTxtTemp;
private JTextField minTxtVCC;
private JTextField avTxtVCC;
private JButton btnMax;
private JButton btnMin;
private JButton btnAv;
private JTextField opnTxt;
private JButton btnOpn;
private TextArea textArea;
private JFileChooser fc;
private String content = "";
String [] contentCSV = new String [53000]; //String array to hold the data, 2000 gives more than enough space
int totalValues; //Used to hold the amount of values in the array (52790 ish)
/**
* Launch the application.
*/
public static void main(String[] args) { //Main method
EventQueue.invokeLater(new Runnable() {
public void run() { //Create a runnable method
try {
CSVFiles frame = new CSVFiles(); //Launch the GUI
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace(); //Print errors
}
}
});
}
/**
* Create the frame.
*/
public CSVFiles() { //Open constructor
super ("CSV Files"); //Create a title for the GUI
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Instruct how the GUI is closed
setBounds(100, 100, 800, 600); //Set size and location
contentPane = new JPanel(); //Declare the JPanel
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); //Create a boarder
setContentPane(contentPane); //Add the JPanel
contentPane.setLayout(null); //Set the layout
maxTxtVCC = new JTextField(); //Declare this text field
maxTxtVCC.setBounds(113, 534, 86, 20); //Set size and location
maxTxtVCC.setEditable(false); //Set it so it cannot be edited
contentPane.add(maxTxtVCC); //Add to the content pane
maxTxtTemp = new JTextField(); //Declare this text field
maxTxtTemp.setBounds(113, 503, 86, 20); //Set size and location
maxTxtTemp.setEditable(false); //Set it so it cannot be edited
contentPane.add(maxTxtTemp); //Add to the content pane
maxTxtLight = new JTextField(); //Declare this text field
maxTxtLight.setBounds(113, 472, 86, 20); //Set size and location
maxTxtLight.setEditable(false); //Set it so it cannot be edited
contentPane.add(maxTxtLight); //Add to the content pane
JLabel lblLight = new JLabel("Light"); //Declare this label
lblLight.setFont(new Font("Tahoma", Font.BOLD, 14)); //Set the font and size of text
lblLight.setBounds(22, 469, 46, 17); //Set size and location
contentPane.add(lblLight); //Add to the content pane
JLabel lblTemp = new JLabel("Temperature"); //Declare this label
lblTemp.setFont(new Font("Tahoma", Font.BOLD, 14)); //Set the font and size of text
lblTemp.setBounds(10, 503, 109, 17); //Set size and location
contentPane.add(lblTemp);
JLabel lblVCC = new JLabel("VCC"); //Declare this label
lblVCC.setFont(new Font("Tahoma", Font.BOLD, 14)); //Set the font and size of text
lblVCC.setBounds(22, 534, 46, 17); //Set size and location
contentPane.add(lblVCC); //Add to the content pane
minTxtLight = new JTextField(); //Declare this text field
minTxtLight.setBounds(221, 472, 86, 20); //Set size and location
minTxtLight.setEditable(false); //Set it so it cannot be edited
contentPane.add(minTxtLight); //Add to the content pane
avTxtLight = new JTextField(); //Declare this text field
avTxtLight.setBounds(331, 472, 86, 20); //Set size and location
avTxtLight.setEditable(false); //Set it so it cannot be edited
contentPane.add(avTxtLight); //Add to the content pane
minTxtTemp = new JTextField(); //Declare this text field
minTxtTemp.setBounds(221, 503, 86, 20); //Set size and location
minTxtTemp.setEditable(false); //Set it so it cannot be edited
contentPane.add(minTxtTemp); //Add to the content pane
avTxtTemp = new JTextField(); //Declare this text field
avTxtTemp.setBounds(331, 503, 86, 20); //Set size and location
avTxtTemp.setEditable(false); //Set it so it cannot be edited
contentPane.add(avTxtTemp); //Add to the content pane
minTxtVCC = new JTextField(); //Declare this text field
minTxtVCC.setBounds(221, 534, 86, 20); //Set size and location
minTxtVCC.setEditable(false); //Set it so it cannot be edited
contentPane.add(minTxtVCC); //Add to the content pane
avTxtVCC = new JTextField(); //Declare this text field
avTxtVCC.setBounds(331, 534, 86, 20); //Set size and location
avTxtVCC.setEditable(false); //Set it so it cannot be edited
contentPane.add(avTxtVCC); //Add to the content pane
btnMax = new JButton("Maximum"); //Declare this button
btnMax.setBounds(110, 438, 89, 23); //Set size and location
contentPane.add(btnMax); //Add to the content pane
btnMin = new JButton("Minimum"); //Declare this button
btnMin.setBounds(221, 438, 89, 23); //Set size and location
contentPane.add(btnMin); //Add to the content pane
btnAv = new JButton("Average"); //Declare this button
btnAv.setBounds(328, 438, 89, 23); //Set size and location
contentPane.add(btnAv); //Add to the content pane
textArea = new TextArea(); //Declare this text area
textArea.setBounds(22, 55, 551, 367); //Set size and location
textArea.setEditable(false); //Set it so it cannot be edited
contentPane.add(textArea); //Add to the content pane
btnOpn = new JButton("Open File"); //Declare this button
btnOpn.addActionListener(new ActionListener() { //Add an action listener to this button
public void actionPerformed(ActionEvent arg0) { //Method for action performed
try{
fc = new JFileChooser(); //Declare the file chooser
fc.setFileFilter(new FileNameExtensionFilter("CSV Files", "csv")); //Add a filter for only choosing CSV files
fc.removeChoosableFileFilter(fc.getAcceptAllFileFilter()); //Remove option to select any file type
int returnVal = fc.showOpenDialog(contentPane); // Open the file chooser
File f; //Create a file to hold the data
//If the selected file is approved by the file chooser...
if(returnVal == JFileChooser.APPROVE_OPTION){
f = fc.getSelectedFile(); //Stored selected file into file variable
BufferedReader in = new BufferedReader(new FileReader(f));
String line = null;
textArea.append("Opening "+ f.getAbsolutePath()); //Print out file path
textArea.append("\nLoading file...\n\n"); //Print out loading message and some new lines
in.readLine(); //Skip the first line as it's just headers
int index = 0; //Integer used to label the indexes of the array
while((line = in.readLine()) != null){
content += line+"\n"; //+= Add left operand to right, so "" + line, then new line
contentCSV[index] = line; //Storing each line of the file into the array and assigning the indexs
++index; //increment the index to move the next one up for hte next line
}
totalValues = index;
textArea.append(content);
textArea.append("\n*** End of File"); //Print the file onto the text area and an end of file message
}
else{
f = null;
}
}
catch(Exception e){
}
}
});
btnOpn.setBounds(484, 26, 89, 23); //Set size and location
contentPane.add(btnOpn); //Add to the content pane
opnTxt = new JTextField(); //Declare this text field
opnTxt.setBounds(22, 27, 452, 20); //Set size and location
opnTxt.setEditable(false); //Set it so it cannot be edited
contentPane.add(opnTxt); //Add to the content pane
}
}
答案 0 :(得分:0)
String content = line //
String cols[] = content.split("\\|");
String v1 = cols[3];
String v2 = cols[4];
String v3 = cols[5];
String dt = cols[2]
使用Integer.parseInt / Double.parseDouble将拆分值转换为int值,并使用DateFormat
答案 1 :(得分:0)
向Satheesh Cheveri道歉,我正在删除他的建议中的一些错误。
String cols[] = line.split("\\|"); // since the argument to split is a regex and | is a special character, it must be quoted.
double v1 = Double.parseDouble(cols[3]);
double v2 = Double.parseDouble(cols[4]);
double v3 = Double.parseDouble(cols[5]);
handleValuesFromLine(v1, v2, v3); // accumulate stuff for averaging later
我还建议您执行提取方法来创建
loadDataFromFile(BufferedReader in)
方法。通过这种方式,您可以编写第二个应用程序来测试文件解析逻辑,而无需运行GUI并单击5个内容来测试代码更改。