簡體   English   中英

將數據存儲在文本文件中

[英]Storing data in text file

以下是我的checkDTS函數代碼。 我必須每隔10分鍾將DTS的狀態存儲在分隔的文本文件中。

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();

}

我試圖將用於存儲DTS狀態的這段代碼放入文件中。但是然后,我不知道應該在myconsole.print()中放入哪一行,因為我對這段代碼不太熟悉。

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

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

現有示例僅說明如何創建新文件並將其存儲在其中。 但是我的,我知道如何創建文件。 但是,我不知道如何從代碼中獲取DTS狀態(我應該執行哪一行?)並將其保存在文本文件中。

您可能需要閱讀有關在Java中創建和寫入文件的答案

實質上,

// 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();

如果您知道擁有什么信息以及要將其保存為哪種格式,只需將其寫出到光盤即可。 try循環中,您的代碼沒有透露您要編寫哪種信息,因此我只能說更多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM