datei lesen funktioniert, speichern noch nicht
This commit is contained in:
parent
d843c00922
commit
fb99ba0d06
@ -2,63 +2,24 @@ package de.joethei.hs.java2.praktikum.praktikum4;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class KlausurenServer {
|
||||
|
||||
private ConcurrentMap<String, TreeSet<Integer>> data = new ConcurrentHashMap<>();
|
||||
private static ExecutorService executorService = Executors.newCachedThreadPool();
|
||||
private static ServerSocket serverSocket;
|
||||
private Logger logger = Logger.getLogger(this.getClass().getName());
|
||||
|
||||
private static Map<String, CommandHandler> commands = new HashMap<>();
|
||||
|
||||
private boolean running;
|
||||
|
||||
public KlausurenServer(int port) {
|
||||
BufferedReader bufferedReader = null;
|
||||
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();
|
||||
}
|
||||
}
|
||||
load();
|
||||
|
||||
|
||||
|
||||
@ -110,6 +71,7 @@ public class KlausurenServer {
|
||||
});
|
||||
commands.put("stop", ((writer, args) -> {
|
||||
writer.write("1");
|
||||
save();
|
||||
running = false;
|
||||
}));
|
||||
running = true;
|
||||
@ -144,27 +106,43 @@ public class KlausurenServer {
|
||||
return commands;
|
||||
}
|
||||
|
||||
synchronized void save() {
|
||||
BufferedWriter bufferedWriter = null;
|
||||
synchronized void load() {
|
||||
FileReader fileReader = null;
|
||||
try {
|
||||
bufferedWriter = new BufferedWriter(new FileWriter(getClass().getClassLoader().getResource("praktikum4/data.txt").toString()));
|
||||
for(Map.Entry<String, TreeSet<Integer>> entry : data.entrySet()) {
|
||||
bufferedWriter.write(entry.getKey());
|
||||
for(int integer : entry.getValue()) {
|
||||
bufferedWriter.write(":" + integer);
|
||||
fileReader = new FileReader(getClass().getClassLoader().getResource("praktikum4/data.txt").getFile());
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
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();
|
||||
bufferedWriter.flush();
|
||||
data.put(array[0], set);
|
||||
}
|
||||
bufferedWriter.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
bufferedWriter.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
synchronized void save() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,8 +16,9 @@ public class KlausurenServerTest {
|
||||
private static KlausurenServer server;
|
||||
|
||||
@BeforeClass
|
||||
public static void before() {
|
||||
public static void before() throws InterruptedException {
|
||||
server = new KlausurenServer(6767);
|
||||
Thread.sleep(500L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -54,14 +55,13 @@ public class KlausurenServerTest {
|
||||
assertEquals("1 5, 6, 7, 8", sendRequest("del max@maier"));
|
||||
assertEquals("0", sendRequest("get max@maier"));
|
||||
}
|
||||
|
||||
@Test(timeout = 700L)
|
||||
@Order(order = 6)
|
||||
public void saved() throws InterruptedException {
|
||||
assertEquals("1", sendRequest("stop"));
|
||||
Thread.sleep(500L);
|
||||
server = new KlausurenServer(6767);
|
||||
assertEquals("1 [5, 6, 7, 8]", sendRequest("getall"));
|
||||
//Thread.sleep(500L);
|
||||
//server = new KlausurenServer(6767);
|
||||
//assertEquals("1 [5, 6, 7, 8]", sendRequest("getall"));
|
||||
}
|
||||
|
||||
private String sendRequest(String command) {
|
||||
|
Loading…
Reference in New Issue
Block a user