public static boolean writeFile ( File directory , File fileName , String text ) { try { File destination = new File ( directory , fileName . getPath ( ) ) ; File parent = destination . getParentFile ( ) ; if ( ! parent . exists ( ) ) { if ( ! parent . mkdirs ( ) ) { return false ; } } RandomAccessFile ra = new RandomAccessFile ( destination , "rw" ) ; try { ra . setLength ( 0 ) ; ra . writeBytes ( text ) ; } finally { ra . close ( ) ; } return true ; } catch ( FileNotFoundException e ) { logger . error ( e . getMessage ( ) , e ) ; return false ; } catch ( IOException e ) { logger . error ( e . getMessage ( ) , e ) ; return false ; } }
This method writes the content of a String into a file specified by a directory and its fileName .