How to format text file?


How to format text file?

View Answers

December 17, 2008 at 4:30 AM

Hi friend,

I am sending running code. I hope that this code will help you.


public class ReadWriteTextFile {

static public String getContents(File aFile) {
StringBuilder contents = new StringBuilder();

try {

BufferedReader input = new BufferedReader(new FileReader(aFile));
try {
String line = null; //not declared within while loop

while (( line = input.readLine()) != null){
finally {
catch (IOException ex){

return contents.toString();

static public void setContents(File aFile, String aContents)
throws FileNotFoundException, IOException {
if (aFile == null) {
throw new IllegalArgumentException("File should not be null.");
if (!aFile.exists()) {
throw new FileNotFoundException ("File does not exist: " + aFile);
if (!aFile.isFile()) {
throw new IllegalArgumentException("Should not be a directory: " + aFile);
if (!aFile.canWrite()) {
throw new IllegalArgumentException("File cannot be written: " + aFile);

//use buffering
Writer output = new BufferedWriter(new FileWriter(aFile));
try {
//FileWriter always assumes default encoding is OK!
output.write( aContents );
finally {

public static void main(String args[]) throws IOException {
File testFile = new File("/home/vinod/amarexamples/anju.txt");
System.out.println("Original file contents: " + getContents(testFile));
setContents(testFile, "The content of this file has been overwritten...");
System.out.println("New file contents: " + getContents(testFile));


Visit for more information.



Related Tutorials/Questions & Answers:
How to print differences of two text files
How to Convert Text Files into Gzip File
How to convert multiple files in java to .zip format
How to format text file? - Java Beginners
How can I Convert my Image Files to Text Files? - IDE Questions
Text Files
How could we get log file as simple text format
autocomplete(): Spring mvc with jquery: I am not getting correct value in the text filed. Please help me
How to download files from server to local disk using ZIP format in JSP page - JSP-Servlet
Converting the text files into gzip file.
How to create a new text file that contains the file names of the copied files in a new directory? - Java Beginners
Converting Text Files into Bzip File
how to read 100 text files from a folder or directory and write the data into a single file.using java programming?
save output in multiple text files
How to print this Format exactly?
How to format number in Java?
how to read the values for text and csv files and store those values into database in multiple rows..means one value for one row
Java show files in tree format
How Compare 2 xml files with JDOM - Java Beginners
How create csv format in jsf ?
how to print pdf format
How to list files in hadoop?
unicode conversion to its actual text format
Converting the text files into bzip file.
How to convert EBCDIC format value into ASCII format value in java
how to upload multiple files in jsp and saving the path in database and the file in folder
How to format json String in Java
How to make a cloudy text, cloudy text, text
how to update the text file?
How to write text on image
how to read files of directory objective c