0

Below are my code for the checkDTS function. I have to store the status of the DTS in the separated text file in 10 minutes interval.

private boolean DTS_firstTime = true;
private int num_of_DTS_tries = 0;
private long interval_DTS = 5 * 60 * 1000;
public void checkDTS(){

//  log.info("===Check DTS===");

    if((System.currentTimeMillis() - remoteLane.getLastDTSReceived() > interval_DTS)){

        if(num_of_DTS_tries >= 5){
            //if (ipc_reachable) {
            if (getStates().getLNK().isStatus()) {
                if (getStates().getDTS().isStatus() || DTS_firstTime) {
                    log.info(getRemoteLane().getName() + ">>DTS Service Failed.");
                    DTS_firstTime = false;
                    getStates().getDTS().setStatus(false);
                    getRemoteLane().setDTSMode("");
                    dataSyncStopPlayback = false;
                    doDataSyncAlert();
                    doDataSyncDisplay();
                }
            }

            remoteLane.setLastDTSReceived(System.currentTimeMillis());
            num_of_DTS_tries = 0;
        } else {
            sendDTSCommand(Status.ISDTSUP, "");
        }

        num_of_DTS_tries++;

    } else{
//      log.debug("DTS Mode: " +getRemoteLane().getDTSMode());
        if (getRemoteLane().getDTSMode().equalsIgnoreCase(Status.OK)){
            //if (ipc_reachable) {
            if (getStates().getLNK().isStatus()) {
            //  if (!getStates().getDTS().isStatus()) {
            //      clrDataSyncDisplay();
            //  }
                getStates().getDTS().setStatus(true);
                dataSyncStopPlayback = true;
                dataSyncAlert = false;
                if (!alert && !discrepancyalert && !exitWarningAlert) {
                    getLane().setRoadBackground(getLane().stateColor);
                }

                //clrDataSyncDisplay();
            }

        } else if (getRemoteLane().getDTSMode().equalsIgnoreCase(Status.NG)){
            //if (ipc_reachable) {
            if (getStates().getLNK().isStatus()) {
                if (getStates().getDTS().isStatus() || DTS_firstTime) {
                    log.info(getRemoteLane().getName() + ">>DTS Service Failed..");
                    DTS_firstTime = false;
                    getStates().getDTS().setStatus(false);
                    dataSyncStopPlayback = false;
                    doDataSyncAlert();
                    doDataSyncDisplay();

                    try{
                        PrintStream myconsole = new PrintStream (new File ("E://TMC//250216.y.txt"));
                        System.setOut(myconsole);
                        myconsole.print();
                    } catch (FileNotFoundException fx) {
                        System.out.println(fx);
                    }
                }
            }
        }
    //  getStates().getDTS().setStatus(true);
        num_of_DTS_tries = 0;
    }   

    this.repaint();

}

I try to put this segment of code for the storing DTS status into the file.But then, I don't know which line should i put in myconsole.print() since I am not that familiar with this code.

try{
    PrintStream myconsole = new PrintStream(new File("E://TMC//250216.y.txt"));
    System.setOut(myconsole);
    myconsole.print();

    } catch (FileNotFoundException fx) {
      System.out.println(fx);
    }

The existing example only show on how to create new file and store in it. But mine, i know how to create file. But, i don't know how to fetch the DTS status from code (which line should i execute?) and save it in text file.

Belle
  • 51
  • 1
  • 11
  • Possible duplicate of [How to create a file and write to a file in Java?](http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java) – ifly6 Feb 25 '16 at 05:10

1 Answers1

0

You may want to read the answer on creating and writing to a file in Java.

Essentially,

// One creates the PrintWriter to print to a place.
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");

// Then one prints each line. If you have an array of lines, you can print by iterating through that array.
writer.println("The first line");
writer.println("The second line");

// Remember to close the writer. Though, if you use `try-with-resources`, that is no longer a problem.
writer.close();

If you know what information you have and what format you want it saved into, simply write it out to disc. Your code in the try loop is unrevealing about what kind of information you want to write, so I can't say more than this.

Community
  • 1
  • 1
ifly6
  • 2,517
  • 2
  • 19
  • 33