All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.TKnudsen.ComplexDataObject.model.io.FileUtils Maven / Gradle / Ivy

Go to download

A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.

The newest version!
package com.github.TKnudsen.ComplexDataObject.model.io;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.List;

/**
 * 

* ComplexDataObject *

* *

* Little helpers when working with the file system and file-IO. *

* *

* Copyright: (c) 2016-2020 Juergen Bernard, * https://github.com/TKnudsen/ComplexDataObject *

* * @author Juergen Bernard * @version 1.03 */ public class FileUtils { public static boolean testFileExists(String fileName) { File file = new File(fileName); boolean exists = false; try { exists = file.exists(); } catch (Exception e) { } return exists; } /** * clears all files in a the given folder. for a given filename nothing will * happen. use f.getParentFile() to come from a filename to a folder name. * * @param folderName */ public static void clearFolder(String folderName) { clearFolder(new File(folderName)); } /** * clears all files in a the given folder. for a given filename nothing will * happen. use f.getParentFile() to come from a filename to a folder name. * * @param folderName */ public static void clearFolder(File folderName) { clearFolder(folderName, false); } /** * clears all files in a the given folder. for a given filename nothing will * happen. use f.getParentFile() to come from a filename to a folder name. * * @param folderName */ public static void clearFolder(File folderName, boolean printOut) { if (!folderName.exists()) return; int deleteCount = 0; for (File someFile : folderName.listFiles()) if (!someFile.isDirectory()) if (someFile.delete()) deleteCount++; if (printOut && deleteCount > 0) System.out.println("FileUtils.clearFolder: cleared. " + deleteCount + " files deleted in folder " + folderName.getName()); } /** * recursive method that retrieves all files in a given directory. * * @param directoryName * @param files */ public static void listFilesOfDirectoryAndSubdirectories(String directoryName, List files, FilenameFilter filenameFilter, boolean querySubfolders) { File directory = new File(directoryName); File[] fList = directory.listFiles(); if (fList != null) for (File file : fList) { if (file.isFile()) { if (filenameFilter == null) files.add(file); else if (filenameFilter.accept(directory, file.getName())) files.add(file); } else if (file.isDirectory() && querySubfolders) { listFilesOfDirectoryAndSubdirectories(file.getAbsolutePath(), files, filenameFilter, querySubfolders); } } } /** * file name is compatible to Windows file systems * * @param raw * @return */ public static String createFileNameString(String raw) { String ret = raw; List evilChars = evilCharsForFileNames(); for (String s : evilChars) ret = ret.replace(s, "_"); ret = ret.replace("?", "e"); ret = ret.replaceAll("/", "_"); return ret; } /** * directory name is compatible to Windows file systems * * @param raw * @return */ public static String createDirectoryNameString(String raw) { String ret = raw; List evilChars = evilCharsForDirectoryNames(); for (String s : evilChars) ret = ret.replace(s, "_"); ret = ret.replace("?", "e"); return ret; } private static List evilCharsForFileNames() { return Arrays.asList("#", "<", "$", "+", "%", ">", "!", "`", "&", "*", "?", "|", "{", "?", "?", "=", "}", ":", "\\", "@"); // "/", } private static List evilCharsForDirectoryNames() { return Arrays.asList("#", "<", "$", "+", "%", ">", "!", "`", "&", "*", "?", "|", "{", "?", "?", "=", "}", // ":" "\\", "@"); // "/", } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy