This code writes the string to a file. It is important to close the writer, so this is done in a finally block.
public class CustomFileWriter { public void writeToCustomFile(String content) throws IOException { File customFile = new File("customFile.txt"); BufferedWriter customBufferedWriter = null; try { customBufferedWriter = new BufferedWriter(new FileWriter(customFile)); customBufferedWriter.write(content); } finally { if (customBufferedWriter != null) { customBufferedWriter.close(); } } } // Version ≥ Java SE 7 public void writeToCustomFileJava7(String content) throws IOException { Path customFilePath = Paths.get("customFile.txt"); try (BufferedWriter customBufferedWriter = Files.newBufferedWriter(customFilePath)) { customBufferedWriter.write(content); } } }
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions