Tuesday, November 20, 2012

Java class to Write Data into a Text File


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();
            }
        }
    }
}

1 comment:

  1. thanks for the post.this really helped me in my project.
    one more thing i stuck it was reading property file and after a long seek i found a artical.hope this will also helpful for others
    http://techighost.com/java-program-to-read-properties-file-in-java/

    ReplyDelete