JavaChat - ako riešiť

Created: 2008-10-09 - 22:57

Na stránke Peťa Gurského ste si mohli všimnúť nasledovné triedy:


package pg.chat;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DoSprav implements ActionListener {

	/*
	 * (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 * 
	 * zisti, ci sa velkost suboru spravy.txt zvacsila
	 * ak ano, otvori subor a precita neprecitane spravy
	 * a vypise ich na obrazovku 
	 */
	public void actionPerformed(ActionEvent e) {
		

	}

}


a

package pg.chat;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Calendar;
import java.util.Scanner;

import javax.swing.Timer;

public class Chat {

	File ludia = new File("ludia.txt");
	File spravy = new File("spravy.txt");
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		Chat chat = new Chat();
		chat.inicializacia();
		Timer doSprav = new Timer(1000,new DoSprav());
		doSprav.start();
		chat.priebeh();
		chat.koniec();
	}
	
	/*
	 * zistime ci subory existuju, ak nie tak ich vytvorime
	 * spytame sa pouzivatela na nick
	 * zapiseme ho do suboru ludia.txt 
	 */
	void inicializacia() throws IOException {
		if (! ludia.exists()) {
			ludia.createNewFile();
		}
		if (! spravy.exists()) {
			spravy.createNewFile();
		}
		Scanner s = new Scanner(System.in);
		boolean ok;
		String meno;
		do {
			ok = true;
			System.out.println("Zadaj nick:");
			meno = s.next();
			
			RandomAccessFile raf = new RandomAccessFile(ludia,"r");
			raf.seek(0);
			
			while (raf.getFilePointer() < raf.length()) {
	 			String pom = raf.readLine();
				String[] pole = pom.split(" ");
				if (pole[1].equals(meno)) {
					ok = false;
				}
			}
			raf.close();
		} while (! ok);
		RandomAccessFile raf = new RandomAccessFile(ludia,"rw");
		raf.seek(raf.length());
		Calendar cal = Calendar.getInstance();
		raf.writeBytes(cal.getTimeInMillis()+ " " + meno+"\n");
		raf.close();
	}
	
	/*
	 * citame vstup z klavesnice a ak nieco precitame
	 * otvorime spravy na zapis a zapiseme spravu
	 * ak sa zada exit ta skoncime
	 */
	void priebeh() {
		
	}
	
}