Utils { public static void writeFile(String in, String filename) { try { File outfile = new File(filename); if (!(new File(outfile.getParent()).exists())) { (new File(outfile.getParent())).mkdirs(); } BufferedWriter bw = new BufferedWriter(new FileWriter(outfile)); bw.write(in); bw.close(); } catch (Exception e) { e.printStackTrace(); } }  static String cleanLine(String line); static String getFile(String FileName); static void writeFile(String in, String filename); static Double round(double d, int decimalPlace); static Double entropy(double[] p);  }
@Test public final void testWriteFile() { String property = "java.io.tmpdir"; String fname = new String("dfgdfsgdfshfr.txt"); String fullpath = System.getProperty(property)+File.separatorChar+fname; String in = "something1\nsecond line\n\t3rd line\n\nlast line."; System.out.println("In=["+in+"]"); Utils.writeFile(in, fullpath); File fToReadFromAndDeleteAfterwards = new File(fullpath); if (fToReadFromAndDeleteAfterwards.exists()) { try { BufferedReader dataInput = new BufferedReader(new FileReader( fToReadFromAndDeleteAfterwards)); try { char[] outCharBuf = new char[1000]; int len = dataInput.read(outCharBuf); String out = new String(outCharBuf,0,len); System.out.println("Out=["+out+"]"); boolean isTheSame = in.equals(out); fToReadFromAndDeleteAfterwards.delete(); assertTrue(isTheSame); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { fail("file not found, "+e.getMessage()); } } else fail("file does not exist"); }