File Create In Java

How To create file in java

Example:

package filles;

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

public class CreateFiles {
 
    public static void main(String[] args) {
          try{
             // create new file
             String content = “This is the content to write into create file”;
             String path=”D:\a\hi.txt”;
             File file = new File(path);
   
                // if file doesnt exists, then create it
                if (!file.exists()) {
                    file.createNewFile();
                }
   
                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                // write in file
                bw.write(content);
                // close connection
                bw.close();
          }catch(Exception e){
              System.out.println(e);
          }
         
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

18 − 10 =