001/******************************************************************************* 002The MIT License (MIT) 003 004Copyright (c) 2024 KILLCODING.COM 005 006Permission is hereby granted, free of charge, to any person obtaining a copy 007of this software and associated documentation files (the "Software"), to deal 008in the Software without restriction, including without limitation the rights 009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 010copies of the Software, and to permit persons to whom the Software is 011furnished to do so, subject to the following conditions: 012 013The above copyright notice and this permission notice shall be included in 014all copies or substantial portions of the Software. 015 016THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 017IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 018FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 019AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 020LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 021OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 022THE SOFTWARE. 023*****************************************************************************/ 024package com.killcoding.tool; 025 026import java.io.File; 027import java.nio.file.Path; 028import java.nio.file.Paths; 029import java.net.URI; 030import java.nio.file.Files; 031import java.io.IOException; 032import com.killcoding.log.Logger; 033import com.killcoding.file.DiskFile; 034 035public final class FileTools { 036 037 public static boolean deleteDirectory(File directoryToBeDeleted) throws IOException { 038 return new DiskFile(directoryToBeDeleted.getAbsolutePath()).delete(); 039 } 040 041 public static String text(String filePath) throws IOException { 042 return new DiskFile(filePath).readAllString(); 043 } 044 045 public static String text(File file) throws IOException { 046 return text(file.getAbsolutePath()); 047 } 048 049 public static long size(String filePath) throws IOException { 050 return size(new File(filePath)); 051 } 052 053 public static long size(File file) throws IOException { 054 if (file.exists()) { 055 Path path = Paths.get(file.getAbsolutePath()); 056 return Files.size(path); 057 } else { 058 return 0L; 059 } 060 } 061 062 public static void write(String filepath, String msg, boolean append) throws IOException { 063 write(new File(filepath), msg, append); 064 } 065 066 public static void write(File file, String msg, boolean append) throws IOException { 067 writeTo(file, msg, append); 068 } 069 070 private static void writeTo(File file, String msg, boolean append) throws IOException { 071 DiskFile df = new DiskFile(file.getAbsolutePath()); 072 df.write(msg,append); 073 } 074 075}