About Me

B.E.(Computer Science), Android/Java Developer, CCNA, CCNA SECURITY (IINS), CCNP (R&S), 4011 Recognized(NSA & CNSS)U.S.A. , MCSA, MCTS, REDHAT CERTIFIED NETWORK SECURITY ADMINISTRATOR(RH253), AFCEH.

Sunday, May 12, 2013

       

    //---> Write to File:

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

public class WriteFile {

 public boolean writeToFile(File f) {

  boolean isSuccessful = false; // ----- Variable to get the success of writing

  try {

   FileWriter fw = new FileWriter(f);

   BufferedWriter bw = new BufferedWriter(fw);

   bw.write("Hello this is vivek");

   bw.close(); // -------- Close the BufferedWriter

   // ---- Closing the BufferedWriter will automatically the FileWriter
   // and in-turn the File

   isSuccessful = true;

  } catch (IOException e) {

   isSuccessful = false;

   e.printStackTrace();

  }

  return isSuccessful;

 }

 public static void main(String[] args) {

  File f = new File("e:\\Hello.txt");

  boolean isDone = new WriteFile().writeToFile(f);

  if (isDone == true) {

   System.out.println("File successfully written");

  }

  else {

   System.out.println("File writing failed");

  }

 }

}

    
            
       
 

No comments:

Post a Comment