FileUtils { public static void appendContentsTofile(String fileName, String contents) throws Exception { RandomAccessFile file = null; try{ file = new RandomAccessFile(fileName, "rw"); file.seek(file.length()); file.writeBytes(contents); }finally{ if(file != null) file.close(); } } private  FileUtils(); static void appendContentsTofile(String fileName, String contents); static String appendSlash(String s); @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="OBL_UNSATISFIED_OBLIGATION") static boolean bcmp(String fn1, String fn2); static boolean checkFileExists(String fileName); static void copy(InputStream in, OutputStream out); static void copyFile(String src, String dst); @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="OBL_UNSATISFIED_OBLIGATION") static void cp(String src, String dst); static void createFileAndWriteContents(String fileName, String contents); static void createPath(String path, boolean mustCreate); static File createTempPath(String path); static boolean dirExists(String fName); static boolean emptyDir(String dirName); static boolean fileExists(String fName); static byte[] fileToByteArray(String fname); static String readFile(String fileName, String newLine); static String readFileOrNull(String fileName); static void writeLine(BufferedWriter out, String line); @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION") static boolean writeLine(String outFname, String line, boolean append); static void xcopy(String src, String dst);  }
@Test public void testappendContents() throws Exception{ BufferedReader reader = null; StringBuffer all = new StringBuffer(); try{ file1 = File.createTempFile("123", "txt"); System.out.println(file1.getAbsolutePath()); System.out.println(file1.exists()); FileUtils.appendContentsTofile(file1.getAbsolutePath(), "test"); reader = new BufferedReader(new FileReader(file1)); String oneline = null; while((oneline = reader.readLine()) != null){ all.append(oneline); } }finally{ if(reader != null) reader.close(); } assertEquals("test", all.toString()); }