Monday, 17 October 2016

Java File handling


package multispeed;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class myclsmultispeed {

      public static void main(String[] args) throws IOException  {
              BufferedWriter bufferedWriter = null;
              String strContent = "This "+"\t"+"example"+"\n"+" how to write string content to a file";
                  File myFile = new File("C:/MyTestFile.doc");
                  // check if file exist, otherwise create the file before writing
                  if (!myFile.exists()) {
                      myFile.createNewFile();
                  }
                  Writer writer = new FileWriter(myFile);
                  bufferedWriter = new BufferedWriter(writer);
                  bufferedWriter.write(strContent);
                  if(bufferedWriter != null)
                  {
                      bufferedWriter.close();
                  }
          
      }

}




..........................................................................
output:textfile created in c drive and written content in it
..............................................................................
note: public static void main(String[] args) throws IOException
It should be written else there will be error like

Unhandled exception type IOException