Java Create And Write In Text File

How To Create And Write In A Txt File Using Java NetBeans

create file in java



In this Java Tutorial we will see How To Create A Text File In A Specific Path And Write Into It Using Java NetBeans .




Project Source Code:

private void jButton_CreateWriteActionPerformed(java.awt.event.ActionEvent evt) {                                                    
       
        File file = new File("C:\\Users\\omar\\Desktop\\haha\\folder1\\myTextFile.txt");
        
        if(!file.exists())
        {
            try {
                file.createNewFile();
                FileWriter fw = new FileWriter(file.getAbsolutePath());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write("My Text Here");
                
                bw.close();
                fw.close();

            } catch (IOException ex) {
                Logger.getLogger(CreateFileAndWrite.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            System.out.println("File Already Exist");
        }
        
    }        





Share this

Related Posts

Previous
Next Post »