2013-09-02 4 views
0

나는 아직도 내 시계를 만들고있다. 저장/내 데이터를로드 할 때 특정 시간 (예 : 1 월 23 일)에서 닫을 때 시계를 사용할 때 이클립스를 다시 연다 때 뭔가를 할 수 있도록 다시 시간을 말했다. 어떤 아이디어? 데이터 저장 /로드 중입니까?

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

제작 코드에 대해 알려주십시오. –

+0

[serialization] (http://docs.oracle.com/javase/tutorial/jndi/objects/serial.html)을 살펴보십시오. 그것은 시계의 상태의 간단한 저장을 위해 상당히 간단해야합니다. – hexafraction

+1

플러그인을 사용하고 있습니까? 'Preferences prefs = new InstanceScope(). getNode (MY_PLUGIN_ID);'이클립스의 환경 설정을 사용할 수 있습니다. –

답변

0

시계가 구현해야합니다 (Snippits 요청) 직렬화 시계의 상태를 직렬화하는 방법

public Clock implements Serializable{ 
    //logic 
} 

:

FileInputStream fileIn = new FileInputStream("/tmp/clock.state"); 
ObjectInputStream in = new ObjectInputStream(fileIn); 
clockInstance = (Clock) in.readObject(); 
in.close(); 
fileIn.close(); 
: 다음 실행에 읽는 방법

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

0

Properties은 기본 키/값 String 쌍을 사용하여 특정 키에 대한 값을 저장합니다.

또한

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

당신은 또한 Properties API와 유사하지만 기본 데이터 유형에 대한 자동 저장 및 지원을 지원하는 Preferences API를 사용할 수있는 편리한 storeload 방법이있다

+0

왜 단순히 저장하지 않고 긴 시계를 복원할까요? 시간, 일, 년 등을 저장하는 번거 로움을 덜어 줄 수 있습니다. – pimpf0r

+0

@ pimpf0r 예, 환경 설정 API를 사용하면 속성을 처리 할 수 ​​있지만 Stongs AFAK 만 처리합니다. – MadProgrammer

+0

예, 음 ... 긴 l = Long. valueOf ("8364836438"); ... or .... Long.toString (date.getMillis()); 또는 그런 것. – pimpf0r