Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.io.FileWriter; import java.io.BufferedWriter; public class Main { public static void main(String[] args) { try { String str = "Welcome To code4example.com.\n"; FileWriter fw = new FileWriter("file.txt"); BufferedWriter bw = new BufferedWriter(fw); bw.write(str); bw.close(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } |
The java.io.BufferedWriter.write(String str) method writes a string to the writer.
The advantage of using BufferedWriter is that it writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
Syntax : public class BufferedWriter extends Writer.