How to Write data to a file.





Sometimes we have situation were we need to save data to a text file for later use or need to log somethings to a file, for those kind of problems we should know how to write data into file. Below code will solve our problem.

Parameters : Path where new file needs to be created
                    Example : C:/Data/log.txt
       
                     Data which needs to written into the file.
                    Example : "This is dummy data"
 
Output : File will be created at specified path with data written on it.



public static void fileWritter(String filename, String data)throws IOException {
      
      
      // creates a FileWriter Object
      FileWriter writer = new FileWriter(filename); 
      
      // Writes the content to the file
      writer.write(data); 
      writer.flush();
      writer.close();

   }

Hope it helped!
How to Write data to a file. How to Write data to a file. Reviewed by Karan Sawhney on 2:47 AM Rating: 5

No comments:

Powered by Blogger.