How do I write text file?
Category: java.io, viewed: 2867 time(s).
Here is an example code to create a text file an put some contents in it. This program will create a file called write.txt.
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.File; import java.io.Writer; import java.io.FileNotFoundException; import java.io.IOException; public class WriteTextFileExample { public static void main(String[] args) { Writer writer = null; try { String text = "This is a text file"; File file = new File("write.txt"); writer = new BufferedWriter(new FileWriter(file)); writer.write(text); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (writer != null) { writer.close(); } } catch (IOException e) { e.printStackTrace(); } } } } |
Related Examples
- How do I store properties as XML file?
- How do I load properties from XML file?
- How do I convert InputStream to String?
- How do I convert string into InputStream?
- How do I get an exception stack trace message?
- How do I use Console class to read user input?
- How do I store objects in file?
- How do I use DataInputStream and DataOutputStream?
- How do I use RandomAccessFile class?
- How do I create a directories recursively?
|
|