Monday, October 11, 2010

Creating and inserting data into a Text File Example (Java)

In this post, I am giving the code for creating a file and inserting text into it.


import java.io.*;;
import java.util.*;

public class InsertDataToFile {

public static void main(String[] args) {
try {
File makefile = new File("sampleFile.txt");
FileWriter fwriter = new FileWriter(makefile);
fwriter.write("The text is ");
fwriter.write("entered and ");
fwriter.write("displayed here.");
fwriter.flush();
fwriter.close();
System.out.println("File is created and text is inserted!");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

No comments:

Post a Comment