From 600ef1b2cd3528692f147c34b2459fad45b807b2 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 4 Mar 2020 15:14:18 +0100 Subject: [PATCH] Fix crash when unexisting subdir --- src/serverP2P/ServerP2P.java | 1 + src/tools/Directories.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index d4346c3..9b068c1 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -13,6 +13,7 @@ public class ServerP2P { public ServerP2P() { directories = new Directories("P2P_JAVA_PROJECT_SERVER"); + directories.createSubdir(subdir); logger = new Logger(directories.getDataHomeDirectory() + "server.log"); port = 40001; System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory() + subdir); diff --git a/src/tools/Directories.java b/src/tools/Directories.java index cb0326a..97ec091 100644 --- a/src/tools/Directories.java +++ b/src/tools/Directories.java @@ -48,6 +48,20 @@ public class Directories { new File(dataHomeDirectory).mkdirs(); } + /** Create a subdirectory. + * @param subdir Name of subdirectory to create + */ + public void createSubdir(String subdir) { + String d = dataHomeDirectory; + if (subdir != null) { + d += subdir; + if (!subdir.endsWith("/")) { + d += "/"; + } + } + new File(d).mkdirs(); + } + /** Getter for dataHomeDirectory. * @return path to the application home directory */