Java 在文本文件中存储数据
下面是我的checkDTS函数代码。我必须每隔10分钟将DTS的状态存储在单独的文本文件中Java 在文本文件中存储数据,java,storing-data,Java,Storing Data,下面是我的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.currentTimeMill
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
循环中的代码未显示您想要编写的信息类型,因此我不能再多说了。可能重复的