파일 생성

반응형




파일 I/O를 이용한 텍스트 파일 생성과 읽기를 이용한 간단한 누적시간계산하기


1. 처음 시간과 끝 시간을 구한 후

-> 끝 시간 - 처음 시간 = 총 소요시간


2. 텍스트 파일 유무를 체크 한 뒤


3.파일이 없다면(처음 기록하는 시간이므로) 총 누적시간 = 총 소요시간이므로 이 시간을 totaltime.txt에 기록


4. 만약 파일이 있었다면 총 누적시간 = 이전 누적시간 + 총 소요시간이다.

-이를 다시 totaltime.txt 기록한다.(덮어 쓰게 됨)


이런 순서로 진행된다.


Time.java


import java.io.*;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Time {
	static Date dt = new Date();
	static Time timer = new Time();
	static SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd, hh:mm:ss a");
	static int playTimeCheck = 0;

	public long startTime() {
		
		long start = System.currentTimeMillis();
		return start;
	}
	 
	public long endTime() {
		long end = System.currentTimeMillis();
		return end;
	}
	
	public long playTime(long l){
		long longTime = 0;
			long end = timer.endTime();  		//끝 시간 
			longTime = ( end - l ) / 1000; 		//끝 시간 빼기 처음시간
			playTimeCheck = (int)longTime; 		// 플레이 시간 저장
			System.out.println("플레이 시간 : " + (int)(longTime / 3600) + "시간  " + (int)(longTime % 3600 / 60) + "분  " + (int)(longTime % 3600 % 60) + "초");
		return longTime;
	}
	public void totalTime(){
		
		int total = 0;
		try {
			int play = playTimeCheck; 				//아까 계산한 플레이 시간 값
			File file = new File("c:/totaltime.txt");
			if(file.isFile()){						//totaltime.txt 존재 유무 체크
				BufferedReader outReader = new BufferedReader(new FileReader("c:/totaltime.txt")); 
				String read = outReader.readLine();	//누적 시간 읽기
				total = Integer.parseInt(read);		//읽은 값 total에 저장
				total += play;						//play시간과 total 더함
				outReader.close();
				BufferedWriter out = new BufferedWriter(new FileWriter("c:/totaltime.txt"));
				out.write(Integer.toString(total));	//합산된 total 값을 기록
				out.close();
			}else{									//파일이 없다면
				BufferedWriter out = new BufferedWriter(new FileWriter("c:/totaltime.txt"));
				total = play;						//처음에는 play시간이 곧 누적시간이므로 total에 play시간 대입
				out.write(Integer.toString(total));	//파일에 기록
				out.close();
			}
			
			System.out.println("누적 플레이 시간 : " + ( total / 3600) + "시간  " + (total % 3600 / 60) + "분  " + (total % 3600 % 60) + "초");
			
						
		} catch (IOException e) {
			System.err.println(e); // 에러가 있다면 메시지 출력
			System.exit(1);
		}
	}
	
}


이를 응용하여 예를 들면

public class TimeTest {

	public Time time = new Time();  //Time.java 객체선언
	long startTime = 0;
	long endTime = 0;
	
	public void setUpGame() {
		
		startTime = time.startTime();  //시작과 동시에 시작시간 구함
	
	}

	

	private void startPlaying() {
	
		finishGame();
	}

	

	private void finishGame() {
		exitGame();					//끝날 때 시간 구함
	}
	
	private void exitGame() {
		System.out.println("게임 종료");
		time.totalTime();			//누적시간 계산 후 출력
			
		}
			

	public static void main(String[] args) {
		DotComBust game = new DotComBust();
		game.setUpGame();
		game.startPlaying();
	}
}





대강 이런 식으로 사용가능하다..





반응형

'지식메모 > JAVA' 카테고리의 다른 글

JAVA JPasswordField 암호 값 가져오기  (0) 2015.09.20
문자열에서 숫자만 추출하기  (0) 2015.09.02
숫자 천단위 콤마 입력하기  (0) 2014.09.25
Json의 이해  (0) 2013.11.05
대문자 소문자 서로 변환하기  (0) 2013.10.08

+ Recent posts