package vork; import java.io.*; import java.net.*; public class Teade2 { static String failinimi = "nimed.txt"; public static boolean kasNimiFailis(String eesnimi) throws IOException { boolean leitud = false; try { BufferedReader failist = new BufferedReader(new FileReader( failinimi)); String rida = failist.readLine(); while (rida != null) { if (rida.equals(eesnimi)) { leitud = true; } rida = failist.readLine(); } failist.close(); } catch (IOException ex) { System.out.println("faili pole"); } return leitud; } public static void lisaNimiFaili(String eesnimi) throws IOException { PrintWriter faili = new PrintWriter(new FileWriter(failinimi, true)); faili.println(eesnimi); faili.close(); } public static void main(String argumendid[]) throws IOException { ServerSocket ss = new ServerSocket(3001); int loendur = 0; while (true) { Socket sc = ss.accept(); loendur++; PrintWriter valja = new PrintWriter(sc.getOutputStream(), true); BufferedReader sisse = new BufferedReader(new InputStreamReader( sc.getInputStream())); valja.println("Kasutaja nr " + loendur + ", mis su nimi on?"); String nimi = sisse.readLine(); if (!kasNimiFailis(nimi)) { lisaNimiFaili(nimi); } if (nimi.equals("Mari")) { valja.println("Tule homme minu juurde!"); } else { valja.println("Mind pole homme kodus."); } sc.close(); } } }