我有一个文本文件,其中包含4个不同的选项“开始时间”,“结束时间”,“ ID”和“值”,每个选项卡之间都用制表符分隔。我需要获取信息的每一位并将其设置为等于类的构造函数中的参数。 IV之前已经完成了基本的set和get方法,但是从不使用文本文件中的数据。到目前为止,这是iv。

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

最佳答案

不要那样做Getter和Setter只是不用于直接公开属性的约定。创建一个仅加载非getter或setter的数据的方法。

关于java - 使用Java从文本文件获取和设置数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22844511/

10-16 14:59