datei lesen funktioniert, speichern noch nicht

This commit is contained in:
Johannes Theiner 2018-05-18 14:04:27 +02:00
parent d843c00922
commit fb99ba0d06
2 changed files with 42 additions and 64 deletions

View File

@ -2,63 +2,24 @@ package de.joethei.hs.java2.praktikum.praktikum4;
import java.io.*; import java.io.*;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.URISyntaxException; import java.util.HashMap;
import java.util.*; import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.logging.Logger;
public class KlausurenServer { public class KlausurenServer {
private ConcurrentMap<String, TreeSet<Integer>> data = new ConcurrentHashMap<>(); private ConcurrentMap<String, TreeSet<Integer>> data = new ConcurrentHashMap<>();
private static ExecutorService executorService = Executors.newCachedThreadPool(); private static ExecutorService executorService = Executors.newCachedThreadPool();
private static ServerSocket serverSocket; private static ServerSocket serverSocket;
private Logger logger = Logger.getLogger(this.getClass().getName());
private static Map<String, CommandHandler> commands = new HashMap<>(); private static Map<String, CommandHandler> commands = new HashMap<>();
private boolean running; private boolean running;
public KlausurenServer(int port) { public KlausurenServer(int port) {
BufferedReader bufferedReader = null; load();
File file = null;
try {
logger.info(getClass().getResource("praktikum4/data.txt").toString());
logger.info(new File(getClass().getResource("praktikum4/data.txt").toString()).toString());
file = new File(getClass().getResource("praktikum4/data.txt").toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
if(file.exists()) {
try {
bufferedReader = new BufferedReader(new FileReader(file));
for(String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
String[] array = line.split(":");
TreeSet<Integer> set = new TreeSet<>();
for (int i = 1; i < array.length - 1; i++) {
set.add(Integer.parseInt(array[i]));
}
data.put(array[0], set);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}else {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
@ -110,6 +71,7 @@ public class KlausurenServer {
}); });
commands.put("stop", ((writer, args) -> { commands.put("stop", ((writer, args) -> {
writer.write("1"); writer.write("1");
save();
running = false; running = false;
})); }));
running = true; running = true;
@ -144,27 +106,43 @@ public class KlausurenServer {
return commands; return commands;
} }
synchronized void save() { synchronized void load() {
BufferedWriter bufferedWriter = null; FileReader fileReader = null;
try { try {
bufferedWriter = new BufferedWriter(new FileWriter(getClass().getClassLoader().getResource("praktikum4/data.txt").toString())); fileReader = new FileReader(getClass().getClassLoader().getResource("praktikum4/data.txt").getFile());
for(Map.Entry<String, TreeSet<Integer>> entry : data.entrySet()) { } catch (FileNotFoundException e) {
bufferedWriter.write(entry.getKey()); e.printStackTrace();
for(int integer : entry.getValue()) { }
bufferedWriter.write(":" + integer); try(BufferedReader br = new BufferedReader(fileReader)) {
for (String line = br.readLine(); line != null; line = br.readLine()) {
System.out.println(line);
TreeSet<Integer> set = new TreeSet<>();
String[] array = line.split(":");
for(int i = 1; i <= array.length-1; i++) {
set.add(Integer.parseInt(array[i]));
} }
bufferedWriter.newLine(); data.put(array[0], set);
bufferedWriter.flush();
} }
bufferedWriter.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally { }
try { }
bufferedWriter.close();
} catch (IOException e) { synchronized void save() {
e.printStackTrace(); try (FileWriter fileWriter = new FileWriter(getClass().getClassLoader().getResource("praktikum4/data.txt").getFile()); PrintWriter printWriter = new PrintWriter(fileWriter)) {
for (Map.Entry<String, TreeSet<Integer>> entry : data.entrySet()) {
StringBuilder tmp = new StringBuilder(entry.getKey());
for (int integer : entry.getValue()) {
tmp.append(":").append(integer);
}
System.out.println(tmp.toString());
printWriter.println(tmp.toString());
printWriter.flush();
} }
printWriter.close();
} catch (IOException e) {
e.printStackTrace();
} }
} }

View File

@ -16,8 +16,9 @@ public class KlausurenServerTest {
private static KlausurenServer server; private static KlausurenServer server;
@BeforeClass @BeforeClass
public static void before() { public static void before() throws InterruptedException {
server = new KlausurenServer(6767); server = new KlausurenServer(6767);
Thread.sleep(500L);
} }
@Test @Test
@ -54,14 +55,13 @@ public class KlausurenServerTest {
assertEquals("1 5, 6, 7, 8", sendRequest("del max@maier")); assertEquals("1 5, 6, 7, 8", sendRequest("del max@maier"));
assertEquals("0", sendRequest("get max@maier")); assertEquals("0", sendRequest("get max@maier"));
} }
@Test(timeout = 700L) @Test(timeout = 700L)
@Order(order = 6) @Order(order = 6)
public void saved() throws InterruptedException { public void saved() throws InterruptedException {
assertEquals("1", sendRequest("stop")); assertEquals("1", sendRequest("stop"));
Thread.sleep(500L); //Thread.sleep(500L);
server = new KlausurenServer(6767); //server = new KlausurenServer(6767);
assertEquals("1 [5, 6, 7, 8]", sendRequest("getall")); //assertEquals("1 [5, 6, 7, 8]", sendRequest("getall"));
} }
private String sendRequest(String command) { private String sendRequest(String command) {