Thursday, March 22, 2012

How to Write a File in Java,Java Write to File Example,Write a File in Java

0 comments

Java a Write file  - Java Tutorial

In this Tutorial you will learn how to write a file,It use FileWriter class and BufferedWriter class to write the file.



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

/**
 *
 * @author JSupport http://javasrilankansupport.blogspot.com
 */
public class WriteFile {


    public static void writToFile(String data){

        try {

            // Create a new file  TEST.txt
            FileWriter file_writer  = new FileWriter("TEST.txt");
            BufferedWriter bufferd_writer   =   new BufferedWriter(file_writer);

            // write data to file
            bufferd_writer.write(data);
            // flush and close the file
            bufferd_writer.flush();
            bufferd_writer.close();

        } catch (IOException ex) {
            System.out.println("Cannot write    :" + ex.getMessage());
        }
    }

    public static void main(String[] args) {
        writToFile("Hello Java Sri Lankan Support");
    }
}

0 comments:

Post a Comment