Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/fugerit-org/fj-lib/issues/90>

## [8.6.7] - 2025-05-09

### Added
Expand Down
42 changes: 37 additions & 5 deletions fj-core/src/main/java/org/fugerit/java/core/io/FileIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
Expand All @@ -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;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) );
}

}