diff --git a/CHANGELOG.md b/CHANGELOG.md index 78f1764e..e9965e84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- FileIO.newFile() create utility init a new File + ## [8.6.7] - 2025-05-09 ### Added diff --git a/fj-core/src/main/java/org/fugerit/java/core/io/FileIO.java b/fj-core/src/main/java/org/fugerit/java/core/io/FileIO.java index 5785cea8..8a769c98 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/io/FileIO.java +++ b/fj-core/src/main/java/org/fugerit/java/core/io/FileIO.java @@ -28,6 +28,40 @@ @Slf4j public class FileIO { + /** + * Creates a new File from a base dir and file name. + * + * If the baseDir is null, only file name is used. + * + * @param baseDir the parent folder + * @param fileName the file name + * @return the File + */ + public static File newFile( String baseDir, String fileName ) { + return baseDir == null ? new File( fileName ) :new File( baseDir, fileName ); + } + + /** + * Creates a new File from a base dir and file name. + * + * If the baseDir is null, only file name is used. + * + * Optionally mustAlreadyExists flag can be set. + * + * @param baseDir the parent folder + * @param fileName the file name + * @param mustAlreadyExists true if final file existence should be checked (if the path does not exist a IOException is thrown) + * @return the File + * @throws IOException in case of issues + */ + public static File newFile( String baseDir, String fileName, boolean mustAlreadyExists ) throws IOException { + File file = newFile( baseDir, fileName ); + if ( mustAlreadyExists && !file.exists() ) { + throw new IOException( String.format( "File [%s] does not exist", file.getCanonicalPath() ) ); + } + return file; + } + public static boolean isInTmpFolder( File tempFile ) throws IOException { return isInTmpFolder( tempFile.getCanonicalPath() ); } @@ -38,18 +72,16 @@ public static boolean isInTmpFolder( String tempFilePath ) throws IOException { } public static boolean createFullFile( File file ) throws IOException { - boolean created = true; if ( file.exists() ) { - created = false; + return Boolean.FALSE; } else { if ( file.isDirectory() ) { - created = file.mkdirs(); + return file.mkdirs(); } else { file.getParentFile().mkdirs(); - created = file.createNewFile(); + return file.createNewFile(); } } - return created; } /* diff --git a/fj-core/src/test/java/test/org/fugerit/java/core/io/TestFileIO.java b/fj-core/src/test/java/test/org/fugerit/java/core/io/TestFileIO.java index 4a6cc73f..e0eb8b86 100644 --- a/fj-core/src/test/java/test/org/fugerit/java/core/io/TestFileIO.java +++ b/fj-core/src/test/java/test/org/fugerit/java/core/io/TestFileIO.java @@ -20,4 +20,38 @@ public void testIsInTmpFolderKo() throws IOException { Assert.assertFalse(FileIO.isInTmpFolder( new File( "/" ) )); } + @Test + public void testNewFile() throws IOException { + String baseDir = "target/"; + String fileName = "not-exists.txt"; + String fileNameExists = "classes"; + File file = FileIO.newFile( baseDir, fileName ); + Assert.assertTrue( file.getPath().endsWith( fileName ) ); + Assert.assertTrue( FileIO.newFile( null, fileName ).getPath().endsWith( fileName ) ); + Assert.assertThrows( IOException.class, () -> FileIO.newFile( baseDir, fileName, Boolean.TRUE ) ); + Assert.assertTrue( FileIO.newFile( baseDir, fileName, Boolean.FALSE ).getPath().endsWith( fileName ) ); + Assert.assertTrue( FileIO.newFile( baseDir, fileNameExists, Boolean.TRUE ).getPath().endsWith( fileNameExists ) ); + } + + + @Test + public void testCreateFullFile() throws IOException { + String baseDir0 = "target/"; + String baseDir1 = "target/path/"; + String fileName = "not-exists-alt.txt"; + String fileNameExists = "classes"; + File file0 = FileIO.newFile( baseDir0, fileName ); + File file1 = FileIO.newFile( baseDir1, fileName ); + File file2 = FileIO.newFile( baseDir0, fileNameExists ); + file0.delete(); + file1.delete(); + file1.getParentFile().delete(); + file2.delete(); + Assert.assertTrue( FileIO.createFullFile( file0 ) ); + Assert.assertTrue( FileIO.createFullFile( file1 ) ); + Assert.assertFalse( FileIO.createFullFile( file0 ) ); + Assert.assertFalse( FileIO.createFullFile( file2 ) ); + Assert.assertFalse( FileIO.createFullFile( new File( baseDir1 ) ) ); + } + }