简体   繁体   English

将数据存储在文本文件中

[英]Storing data in text file

Below are my code for the checkDTS function. 以下是我的checkDTS函数代码。 I have to store the status of the DTS in the separated text file in 10 minutes interval. 我必须每隔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();

}

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. 我试图将用于存储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);
    }

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. 但是,我不知道如何从代码中获取DTS状态(我应该执行哪一行?)并将其保存在文本文件中。

You may want to read the answer on creating and writing to a file in Java. 您可能需要阅读有关在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. try循环中,您的代码没有透露您要编写哪种信息,因此我只能说更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM