保存/加载数据? [英] Saving/loading data?

查看:172
本文介绍了保存/加载数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在做我的钟。当我使用时钟时,我想保存/加载我的数据,所以当我在特定时间(如1月23日)关闭时,当我重新打开日食时,我可以做一些事情,让我回到上述时间。有任何想法吗?
(请求Snippits)

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

扫描仪键盘=新的扫描仪(System.in);

out.print(设置周和日);
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; (;秒< = 60;秒++){
}
{
Thread.sleep(1);
if(seconds == 60)minutes ++; {
if(minutes == 60)hours ++; {
if(hours == 24)day ++; {
if(day == 7&& hours == 23& amp;&分钟== 59&&秒== 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 =7月;
if(week> 28&& week< 33)month =august;
if(week> 32&& week< 37)month =9月
如果(周> 36&& week< 41)month =10月;
if(week> 40&& week< 45)month =十一月;
if(week> 44&& week< 49)month =12月;
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(小时+:+分钟+:+秒+早+天+月+季+ 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 =;
}
}
}
}
}
}
}


解决方案

属性 使用基本键/值 String 对来存储值



它还具有方便的存储加载方法

  FileReader reader = null; 
属性属性= new Properties();
try {
reader = new FileReader(clock.properties);
properties.load(reader);
} catch(IOException exp){
exp.printStackTrace();
}

String lastTime = reader.getProperty(Clock.lastTime);
//如果需要,使用像SimpleDateFormat这样的东西来解析String返回一个Date ...

  String strHour = reader.getProperty(Clock.lastHour,0); 
String strMin = reader.getProperty(Clock.lastMinute,0);
String strSec = reader.getProperty(Clock.lastSecond,0);
//使用Integer.toString来解析结果,不要忘记检查nulls;)

您还可以使用 首选项 API ,类似于属性 API,但支持自动保存和支持原始数据类型


I'm still making my clock. I want to save/load my data from when I use the clock so when I close at a specific time(like January 23rd), when I re-open eclipse I can do something so it brings me back to said time. Any ideas? (Snippits requested)

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 = " ";
}
}
}
}
}
}
}

解决方案

Properties uses a basic key/value String pair for storing a value against a specific key.

It also has convenient store and load methods

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...

or

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

You could also use the Preferences API which is similar to the Properties API but supports automatic saving and support for primitive data types

这篇关于保存/加载数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆