Program to Write to file using FileWriter in java file IO











Program to Write to file using FileWriter in java file IO >




import java.io.FileWriter;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class WriteToFileUsingFileWriter {
   public static void main(String... args) {
          FileWriter fw = null;
          try {
                 // if file doesn't exist new file will be created
                 // if file already exists contends will be overridden.
                 fw = new FileWriter("c:/myFile.txt");
                 String str="You are learning File IO from javaMadeSoEasy.com";
                 fw.write(str); //Write String in the file
                
                 fw.flush(); //bytes in buffer are written in file
                
                 System.out.println("String str has been written successfully in file "
                              + "using FileWriter");
                
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (fw != null){
                              fw.close(); //close FileWriter
                       }
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/* OUTPUT
String str has been written successfully in file using FileWriter
*/


RELATED LINKS>

Program to Read text from file using FileReader in java file IO


Program to Write byteArray to file using FileOutputStream in java file IO


Copy a file in java 2 in ways - use org.apache.commons.io.FileUtils's copyDirectory(sourceDirectory, destDirectory)


Program to Move a file - in java file IO


Program to Create Directory - Single and multiple (i.e. parent and child directories) in java file IO

eEdit
Must read for you :