FileUtil { public static boolean createOrUpdateFile( final String filePath, final String fileContent ) { return FileUtil.createOrUpdateFile( filePath, fileContent, false ); } private  FileUtil(); static boolean isFileExists( final String dirAbsPath, final String fileName ); static boolean isFileExists( final String fileAbsPath ); static boolean isDirExists( final String dirAbsPath ); static File[] findFiles( final String dirAbsPath, final String ext ); static boolean setFilesExecutable( final String dirAbsPath, final String ext ); static boolean extractArchive( final String archiveFileAbsPath, final String destDirAbsPath ); static boolean extractArchive( final File archiveFile, final File destDir ); static File[] findFiles( final String dirAbsPath, final String[] extensions, boolean recursive ); static File[] findFiles( final File dirAbsPath, final String[] extensions, boolean recursive ); static File[] findFiles( final String dirAbsPath, final String fileNamePattern, boolean recursive ); static String getFileChecksum( final String fileAbsPath, ChecksumMethod checksumMethod ); static boolean deleteDirectory( final String directory ); static boolean deleteDirectory( final File directory ); static String getTemporaryFolderPath(); static boolean deleteFile( final String filePath ); static boolean createDirectory( final String dirPath ); static String getFileBaseName( final String fileName ); static boolean unzipFile( final String zipFileAbsPath, final String outputDirAbsPath ); static boolean createOrUpdateFile( final String filePath, final String fileContent ); static boolean createOrUpdateFile( final String filePath, final String fileContent,
                                              final boolean createBackup ); static boolean createNewFile( final String fileAbsPath ); static String readFileToString( final String fileName ); static String findLatestFileByLastModified( final String directory, final String fileNamePattern,
                                                       final boolean recursive );  }
@Test public void testCreateOrUpdateFile() throws IOException { final String fileAbsPath = FilenameUtils.concat( tmpDir, this.getDateTimeInMillis() + ".txt" ); String fileContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id ne cogitari quidem " + "potest quale sit, ut non repugnet ipsum sibi. Ita multa dicunt, quae vix intellegam. Quamquam " + "id quidem, infinitum est in hac urbe; Primum quid tu dicis breve? Aliis esse maiora, illud dubium," + " ad id, quod summum bonum dicitis, ecquaenam possit fieri accessio. Quamquam tu hanc copiosiorem" + " etiam soles dicere.\n"; assertFalse( FileUtil.isFileExists( fileAbsPath ) ); boolean created = FileUtil.createOrUpdateFile( fileAbsPath, fileContent ); assertTrue( created ); assertTrue( FileUtil.isFileExists( fileAbsPath ) ); String fileContent2 = "File updated."; boolean updated = FileUtil.createOrUpdateFile( fileAbsPath, fileContent2 ); assertTrue( updated ); List<String> lines = Files.readAllLines( Paths.get( fileAbsPath ), StandardCharsets.UTF_8 ); String text = lines.toString(); assertFalse( text.contains( fileContent ) ); assertTrue( text.contains( fileContent2 ) ); assertTrue( FileUtil.deleteFile( fileAbsPath ) ); }