Advertisement
oona

Write To File

Sep 16th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.BufferedWriter;
  4. import java.io.IOException;
  5.  
  6. public class fileWriterExample {
  7.  
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.        
  11.         try
  12.         { // beginning of try
  13.             String content = "computer science is cool ";
  14.             File file = new File("C:\\Users\\Oona\\My Documents\\dataFileExample.txt");
  15.            
  16.             if(!file.exists())
  17.             {
  18.                 file.createNewFile();
  19.             }
  20.            
  21.             FileWriter fw = new FileWriter(file,true);
  22.             BufferedWriter bw = new BufferedWriter(fw);
  23.             bw.write(content);
  24.             bw.close();
  25.            
  26.             System.out.println("Data successfully appended at the end of file");
  27.         } // end of try
  28.        
  29.         catch(IOException ioe)
  30.         {
  31.             System.out.println("Exception occurred:");
  32.         }
  33.  
  34.     } //class ends
  35.  
  36. } //program ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement