Monday, October 31, 2011

Writing data to a new file

package au.com.d2klry.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteData {
    public static void main(String args[]){
        try {
            File file = new File("C:\\temp\\java\\WriteData.txt");
            if(!file.exists())
                file.createNewFile();
           
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            writer.write("First Line");
            writer.newLine();//Add a new line
            writer.write("Second Line");
            writer.newLine(); //Add a new line
            writer.write("\tThird Line");//Add a tab before the third line content
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

No comments:

Post a Comment