Skip to main content

how to write text file using java


welcome java file handling tutorial .

in this tutorial we are going to wrie text file using  java here i have used Filewirter to write . use following steps to read text file using java  program
1. create one folder on d drive as f that is name of folder is f
2. create demo.txt file to write on it using program
3. create java program use following code or follow steps from video save java program in same folder import java.io.* package properly in java program
4. compile program and run
5. to understand program read what is FileReader read program explanation given below.









// Program by Prakash Sonar

import java.io.*;

class jWrite
{
public static void main(String s[])
{
try{
FileWriter w=new FileWriter("demo.txt",true);
w.write("welcome to Prakash Programming Tutorial");
w.write("\n ");
w.write("Thanks for Watching");
w.close();


}
catch(Exception e){}



}



}






 Writer, OutputStreamWriter, FileWriter and BufferedWriter


Writer is the abstract class for writing character streams. It implements the following fundamental methods:
·         write(int): writes a single character.
·         write(char[]): writes an array of characters.
·         write(String): writes a string.
·         close(): closes the stream.

OutputStreamWriter is a bridge from byte streams to character streams. Characters are encoded into bytes using a specified charset. The charset can be default character encoding of the operating system, or can be specified explicitly when creating an OutputStreamWriter.

FileWriter is a convenient class for writing text files using the default character encoding of the operating system.

BufferedWriter writes text to a character stream with efficiency (characters, arrays and strings are buffered to avoid frequently writing to the underlying stream) and provides a convenient method for writing a line 

separator: newLine().

 writer classes in the java.io package:







Comments

Labels

Show more

Popular posts from this blog

jbutton Click Event example Advance Java

Jbutton Click Event example Advance Java  Change frame Color On Button click Advance Java  import javax.swing.*; import java.awt.*; import java.awt.event.*;      class FrameDemo extends JFrame implements ActionListener {       JButton btn; Container c;          FrameDemo() {               c=this.getContentPane();        JButton btn= new JButton("Click"); btn.setBounds(100,50,100,50);         c.add(btn);         c.setLayout(null);        btn.addActionListener(this); }                    public void actionPerformed(ActionEvent a) {                     c.setBackground(Color.RED); } }        // cre...

Nested Classes sample program

Nested Classes In Java, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class , and the class that holds the inner class is called the outer class . class Outer {  private class Inner  {    void dispaly()    {        System.out.println("this is inner class");       }   }  void call()  {    Inner obj=new Inner();    obj.dispaly();  } } class InnerDemo { public static void main(String s[]) {  Outer ob=new Outer();  ob.call(); } } Example solve watch video for understanding  // inner class By Prakash Sonar class outer {  private class Inner  {    void display()   {      System.out.println...

java project using CMD Source Code Download

java project using CMD Source Code Download import java.sql.*; import java.*; import java.io.*; class whileInfinite { public static void main(String s[]) {  try{ while(true) { System.out.println("Select Your Choice :"); System.out.println("________________________________________"); System.out.println("1. Insert record :"); System.out.println("2. disply all records :"); System.out.println("3. display Specific record :"); System.out.println("4. update record:"); System.out.println("5. delete Record:"); System.out.println("6. Exit:"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));           System.out.println("Enter your choice"); int choice = Integer.parseInt(reader.readLine()); System.out.println("yo entered"+ choice); switch(choice) { case 1:whileInfinite obj=new whileInfin...