【问题标题】:Saving/loading data?保存/加载数据?
【发布时间】:2013-09-02 07:53:35
【问题描述】:

我还在做我的时钟。我想从我使用时钟时开始保存/加载我的数据,所以当我在特定时间(如 1 月 23 日)关闭时,当我重新打开 Eclipse 时,我可以做一些事情,这样它就会让我回到所说的时间。有任何想法吗? (要求提供摘要)

import java.util.Scanner;
import static java.lang.System.out;
public class Clock {
public static void main(String args[]) throws InterruptedException {

    Scanner keyboard = new Scanner(System.in);

    out.print("Set the week and day.");
    String specday = null;
    String days = null;
    String season = null;
String morning = null;
String month = null;
int inputweek = keyboard.nextInt();
int inputday = keyboard.nextInt(); 
int week = inputweek;
int day = inputday;
int hours = 1;
int minutes = 0;
int seconds = 0; {
}
for (;seconds <= 60; seconds++) {
Thread.sleep(1);
 if (seconds == 60) minutes++; {
if (minutes == 60) hours++; {
if (hours == 24) day++; {
     if (day == 7 && hours == 23 && minutes == 59 && seconds == 59)week++; {
if (week > 0 && week < 9) season = " summer";
if (week > 44 && week < 49) season = " summer";
if (week > 8 && week < 21) season = " autumn";
if (week > 20 && week < 33) season = " winter";
if (week > 32 && week < 45) season = " spring";
if (week > 0 && week < 5) month = " january";
if (week > 4 && week < 9) month = " february";
if (week > 8 && week < 13)month = " march";
if (week > 12 && week < 17) month = " april";
if (week > 16 && week < 21) month = " may";
if (week > 20 && week < 25) month = " june";
if (week > 24 && week < 29) month = " july";
if (week > 28 && week < 33) month = " august";
if (week > 32 && week < 37) month = " september";
if (week > 36 && week < 41) month = " october";
if (week > 40 && week < 45) month = " november";
if (week > 44 && week < 49) month = " december";
if (week == 47 && day == 2) specday = " christmas eve";
if (week == 47 && day == 3) specday = " christmas";
if (week == 47 && day == 4) specday = " boxing day";
if (hours < 12) morning = " am";
if (hours > 11) morning = " pm";
if (day == 1) days = " monday";
if (day == 2) days = " tuesday";
if (day == 3) days = " wednesday";
if (day == 4) days = " thursday";
if (day == 5) days = " friday";
if (day == 6) days = " saturday";
if (day == 7) days = " sunday";
}
System.out.println(hours + ":" + minutes + ":" + seconds + morning + days + month + season + specday); {
    if (seconds == 60) seconds = 0;
    if (minutes == 60) minutes = 0;
if (hours == 24) hours = 0;
if (day > 7) day = 1;
if (week == 49) week = 1;
if (specday == null); specday = " ";
}
}
}
}
}
}
}

【问题讨论】:

  • 向我们展示一些您正在制作的代码?
  • 查看serialization。时钟状态的简单存储应该相当简单。
  • 你在使用插件吗?然后Preferences prefs = new InstanceScope().getNode(MY_PLUGIN_ID);你可以使用eclipse的首选项。
  • 我没有插件,没有。
  • 把sn-p放在帖子里,不要放在评论里

标签: java eclipse load save clock


【解决方案1】:

时钟必须实现可序列化

public Clock implements Serializable{
     //logic
}

如何序列化Clock的状态:

FileOutputStream fileOut =
         new FileOutputStream("/tmp/clock.state");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(clockInstance);
out.close();
fileOut.close();

如何在下次运行时阅读:

FileInputStream fileIn = new FileInputStream("/tmp/clock.state");
ObjectInputStream in = new ObjectInputStream(fileIn);
clockInstance = (Clock) in.readObject();
in.close();
fileIn.close();

【讨论】:

    【解决方案2】:

    Properties 使用基本键/值 String 对来针对特定键存储值。

    它还有方便的storeload方法

    FileReader reader = null;
    Properties properties = new Properties();
    try {
        reader = new FileReader("clock.properties");
        properties.load(reader);
    } catch (IOException exp) {
        exp.printStackTrace();
    }  
    
    String lastTime = reader.getProperty("Clock.lastTime");
    // Use something like SimpleDateFormat to parse the String back to a Date if required...
    

    String strHour = reader.getProperty("Clock.lastHour", "0");
    String strMin = reader.getProperty("Clock.lastMinute", "0");
    String strSec = reader.getProperty("Clock.lastSecond", "0");
    // Use Integer.toString to parse the results, don't forget to check for nulls ;)
    

    您也可以使用Preferences API,它类似于Properties API,但支持自动保存并支持原始数据类型

    【讨论】:

    • 为什么不简单地存储 long 并从 long 中恢复时钟?将使我们免于麻烦存储时间、日期、年份等。
    • @pimpf0r 是的,并且首选项 API 将允许您这样做,但属性仅处理 Stongs AFAK
    • 是的,好吧...然后 Long l = Long.valueOf("8364836438"); ...或.... Long.toString(date.getMillis());或类似的东西。
    • @pimpf0r 是的,我确实提到了Integer.toString,但我想这取决于 OP 打算将时钟运行多长时间......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 2021-10-19
    相关资源
    最近更新 更多