2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.folderwatcher.internal.common;
15 import java.io.BufferedWriter;
17 import java.io.FileWriter;
18 import java.io.IOException;
19 import java.nio.file.Files;
20 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
25 * The {@link WatcherCommon} class contains commonly used methods.
27 * @author Alexandr Salamatov - Initial contribution
30 public class WatcherCommon {
32 private static void initFile(File file, String watchDir) throws IOException {
33 try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file))) {
34 fileWriter.write(watchDir);
39 public static List<String> initStorage(File file, String watchDir) throws IOException {
40 List<String> returnList = List.of();
41 List<String> currentFileListing = List.of();
43 Files.createDirectories(file.toPath().getParent());
44 initFile(file, watchDir);
46 currentFileListing = Files.readAllLines(file.toPath().toAbsolutePath());
47 if (currentFileListing.get(0).equals(watchDir)) {
48 returnList = currentFileListing;
50 initFile(file, watchDir);
56 public static void saveNewListing(List<String> newList, File listingFile) throws IOException {
57 try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(listingFile, true))) {
58 for (String newFile : newList) {
59 fileWriter.write(newFile);