From 57757fe10b33c1d9fededf2537012dbe318f4c53 Mon Sep 17 00:00:00 2001 From: Notmoo Date: Sun, 30 Jul 2017 14:07:50 +0200 Subject: [PATCH] =?UTF-8?q?Module=20Server=20:=20ajout=20clss=20FileUtil,?= =?UTF-8?q?=20ajout=20v=C3=A9rif=20existance=20fichier=20dans=20clss=20Fil?= =?UTF-8?q?eStockDao?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pqt/server/module/stock/FileStockDao.java | 16 ++++++++++++ .../com/pqt/server/utils/FileUtil.java | 26 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 Workspace/server/src/main/WEB-INF/classes/com/pqt/server/utils/FileUtil.java diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/FileStockDao.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/FileStockDao.java index 6f8c42a1..37824cee 100644 --- a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/FileStockDao.java +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/module/stock/FileStockDao.java @@ -1,6 +1,7 @@ package com.pqt.server.module.stock; import com.pqt.core.entities.product.Product; +import com.pqt.server.utils.FileUtil; import java.io.*; import java.util.*; @@ -82,6 +83,15 @@ public class FileStockDao implements IStockDao { private Map load(){ Map loadedData = new HashMap<>(); + try{ + if(FileUtil.createFileIfNotExist(STOCK_FILE_NAME)){ + return loadedData; + } + }catch(IOException e){ + e.printStackTrace(); + return loadedData; + } + try(FileInputStream fis = new FileInputStream(STOCK_FILE_NAME); ObjectInputStream ois = new ObjectInputStream(fis)){ @@ -106,6 +116,12 @@ public class FileStockDao implements IStockDao { } private void save(Map products){ + try{ + FileUtil.createFileIfNotExist(STOCK_FILE_NAME); + }catch (IOException e){ + e.printStackTrace(); + return; + } try(FileOutputStream fos = new FileOutputStream(STOCK_FILE_NAME); ObjectOutputStream oos = new ObjectOutputStream(fos)){ diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/utils/FileUtil.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/utils/FileUtil.java new file mode 100644 index 00000000..08fddf40 --- /dev/null +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/utils/FileUtil.java @@ -0,0 +1,26 @@ +package com.pqt.server.utils; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class FileUtil { + + /** + * Check if the given file path correspond to an existing file, and create it if it doesn't. + * + * @param filePath the file path to check + * + * @return {@code true} if the file has been created, {@code false} if it already existed. + * @throws IOException if any IOException happend during this method's execution. + */ + public static boolean createFileIfNotExist(String filePath) throws IOException { + Path path = Paths.get(filePath); + if(!Files.exists(path)){ + Files.createFile(path); + return true; + } + return false; + } +}