我有一个包含4个不同选项的文本文件"开始时间","结束时间"," ID"和"价值"每个由一个标签分隔。我需要得到每一点信息,并将其设置为我班级构造函数中的参数。以前我做过基本设置和获取方法,但从不使用文本文件中的数据。这是我到目前为止所做的事情。
import java.util.Date;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Sensor {
//need all info for sensors such as sensor ID, Ontime, Offtime, Label
//needs to store all sensor data in memory
//try using jfreechart again instead of manually drawing
private Date startTime1;
private Date endTime1;
private String sensorID1;
private int sensorState1;
String fileName = "C:/kasterenDataset/kasterenSenseData.txt";
public Sensor(Date startTime, Date endTime, String sensorID, int sensorState){
startTime = startTime1;
endTime = endTime1;
sensorID = sensorID1;
sensorState = sensorState1;
}
public void setStartTime(){
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
//start reading from the 24th line on the text file
int lineNumber = 0;
while((line=bufferedReader.readLine())!=null){
lineNumber++;
if (lineNumber >= 24) {
String values[] = line.split("\\t", -1);
}
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex1) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
}
public void getStartTime(){
}
public void getEndTime(){
}
public void getSensorID(){
}
public void getSensorState(){
}
}
这是文本文件中的一些数据
25-Feb-2008 23:22:31 25-Feb-2008 23:24:35 5 1
25-Feb-2008 23:28:31 25-Feb-2008 23:28:32 6 1
25-Feb-2008 23:28:36 25-Feb-2008 23:29:12 6 1
25-Feb-2008 23:30:12 25-Feb-2008 23:30:52 5 1
25-Feb-2008 23:31:53 25-Feb-2008 23:32:30 24 1
25-Feb-2008 23:32:33 25-Feb-2008 23:32:34 24 1
26-Feb-2008 00:38:49 26-Feb-2008 00:38:50 24 1
26-Feb-2008 00:38:52 26-Feb-2008 00:38:59 5 1
26-Feb-2008 00:38:53 26-Feb-2008 00:39:41 24 1
26-Feb-2008 00:39:00 26-Feb-2008 00:39:01 5 1
26-Feb-2008 00:39:03 26-Feb-2008 00:39:04 5 1
26-Feb-2008 00:39:09 26-Feb-2008 00:39:10 5 1
27-Feb-2008 23:23:57 27-Feb-2008 23:24:09 8 1
27-Feb-2008 23:26:19 27-Feb-2008 23:37:08 6 1
27-Feb-2008 23:37:01 27-Feb-2008 23:37:02 14 1
27-Feb-2008 23:37:05 27-Feb-2008 23:37:06 14 1
27-Feb-2008 23:37:09 28-Feb-2008 00:10:39 5 1
27-Feb-2008 23:42:20 27-Feb-2008 23:42:21 18 1
28-Feb-2008 00:10:42 28-Feb-2008 00:10:43 5 1
28-Feb-2008 00:11:10 28-Feb-2008 00:13:42 6 1
28-Feb-2008 00:13:38 28-Feb-2008 00:13:39 14 1
28-Feb-2008 00:13:42 28-Feb-2008 00:13:43 6 1
答案 0 :(得分:1)
不要这样做。 getter和setter只是一种不直接公开属性的约定。创建一个方法只是为了加载不是getter或setter的数据。