/* * [IO.java] * * Summary: Miscellaneous I/O helper routines. * * Copyright: (c) 2003-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 10.2 2009-04-03 tidy up code to check presence of necessary files to make it more WORA. */ package com.mindprod.replicatorcommon; import com.mindprod.common18.EIO; import java.io.File; import java.io.IOException; /** * Miscellaneous I/O helper routines. * * @author Roedy Green, Canadian Mind Products * @version 10.2 2009-04-03 tidy up code to check presence of necessary files to make it more WORA. * @since 2003-09-16 */ public final class IO { /** * Ensure the given directory exists. If it doesn't, create it, even if it means deleting a file named that to do * it. * * @param dirName Directory name that must exist. * * @throws IOException if can't create dir. */ public static void ensureDirectoryExists( String dirName ) throws IOException { final File dir = new File( dirName ); if ( dir.exists() ) { if ( dir.isDirectory() ) { return; } else { // delete a file by that name, so can put a dir by than name in // its place. //noinspection ResultOfMethodCallIgnored dir.delete(); } } // create all necessary parent levels too. //noinspection ResultOfMethodCallIgnored dir.mkdirs(); if ( !dir.exists() ) { throw new IOException( "Can't create directory " + dirName ); } } // ensureDirectoryExists /** * Ensure the given file exists. If it doesn't, throw an exception. * * @param f file that must exist. * * @throws IOException if file missing. */ public static void ensureFileExists( File f ) throws IOException { if ( !( f.isFile() && f.canRead() ) ) { throw new IOException( "Missing file " + EIO.getCanOrAbsPath( f ) ); } } // ensureFileExists /** * get the current directory * * @return fully qualified path of current directory */ public static String getCurrentDir() { return EIO.getCanOrAbsPath( new File( "." ) ).intern(); } }