Skip to main content
Copy edited. Removed meta information (this belongs in comments).
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

This may help.

toTo create file without overwriting existing file:

System.out.println("Choose folder to create file");
JFileChooser c = new JFileChooser();
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
c.showOpenDialog(c);
c.getSelectedFile();
f = c.getSelectedFile(); // File f - global variable
String newfile = f + "\\hi.doc";//.txt or .doc or .html
File file = new File(newfile);

try {
    //System.out.println(f);
    boolean flag = file.createNewFile();
            
    if(flag == true) {
        JOptionPane.showMessageDialog(rootPane, "File created successfully");
    }
    else {
        JOptionPane.showMessageDialog(rootPane, "File already exists");
    }
    /* orOr use exists() function as follows:
        if(file.exists() == true) {
            JOptionPane.showMessageDialog(rootPane, "File already exists");
        }
        else {
            JOptionPane.showMessageDialog(rootPane, "File created successfully");
        }
    */
}
catch(Exception e) {
     //any Any exception handling method of your choice
}

This may help.

to create file without overwriting existing file

System.out.println("Choose folder to create file");
JFileChooser c = new JFileChooser();
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
c.showOpenDialog(c);
c.getSelectedFile();
f = c.getSelectedFile(); // File f - global variable
String newfile = f + "\\hi.doc";//.txt or .doc or .html
File file = new File(newfile);

try {
    //System.out.println(f);
    boolean flag = file.createNewFile();
            
    if(flag == true) {
        JOptionPane.showMessageDialog(rootPane, "File created successfully");
    }
    else {
        JOptionPane.showMessageDialog(rootPane, "File already exists");
    }
    /* or use exists() function as follows:
    if(file.exists() == true) {
        JOptionPane.showMessageDialog(rootPane, "File already exists");
    }
    else {
        JOptionPane.showMessageDialog(rootPane, "File created successfully");
    } */
}
catch(Exception e) {
     //any exception handling method of your choice
}

To create file without overwriting existing file:

System.out.println("Choose folder to create file");
JFileChooser c = new JFileChooser();
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
c.showOpenDialog(c);
c.getSelectedFile();
f = c.getSelectedFile(); // File f - global variable
String newfile = f + "\\hi.doc";//.txt or .doc or .html
File file = new File(newfile);

try {
    //System.out.println(f);
    boolean flag = file.createNewFile();

    if(flag == true) {
        JOptionPane.showMessageDialog(rootPane, "File created successfully");
    }
    else {
        JOptionPane.showMessageDialog(rootPane, "File already exists");
    }
    /* Or use exists() function as follows:
        if(file.exists() == true) {
            JOptionPane.showMessageDialog(rootPane, "File already exists");
        }
        else {
            JOptionPane.showMessageDialog(rootPane, "File created successfully");
        }
    */
}
catch(Exception e) {
    // Any exception handling method of your choice
}

This may help.

to create file without overwriting existing file

System.out.println("Choose folder to create file");
JFileChooser c = new JFileChooser();
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
c.showOpenDialog(c);
c.getSelectedFile();
f = c.getSelectedFile(); // File f - global variable
String newfile = f + "\\hi.doc";//.txt or .doc or .html
File file = new File(newfile);
    try 
   try {
        //System.out.println(f);
        boolean flag = file.createNewFile();
        
      
    if(flag==true)
     flag == true) {
            JOptionPane.showMessageDialog(rootPane, "File created successfully");
        }
        else
        {
            JOptionPane.showMessageDialog(rootPane, "File already exists");
        }
        /* or use exists() function as follows:
            if(file.exists()==true)
          == true) {
                JOptionPane.showMessageDialog(rootPane, "File already exists");
            }
            else
            {
                JOptionPane.showMessageDialog(rootPane, "File created successfully");
            }
         
        */
    }
    
    catch(Exception e)
    {
        //any exception handling method of your choice
    }

This may help.

to create file without overwriting existing file

System.out.println("Choose folder to create file");
JFileChooser c = new JFileChooser();
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
c.showOpenDialog(c);
c.getSelectedFile();
f = c.getSelectedFile(); // File f - global variable
String newfile = f + "\\hi.doc";//.txt or .doc or .html
File file = new File(newfile);
    try 
    {
        //System.out.println(f);
        boolean flag = file.createNewFile();
        
         if(flag==true)
        {
            JOptionPane.showMessageDialog(rootPane, "File created successfully");
        }
        else
        {
            JOptionPane.showMessageDialog(rootPane, "File already exists");
        }
        /* or use exists() function as follows:
            if(file.exists()==true)
            {
                JOptionPane.showMessageDialog(rootPane, "File already exists");
            }
            else
            {
                JOptionPane.showMessageDialog(rootPane, "File created successfully");
            }
         
        */
    }
    
    catch(Exception e)
    {
        //any exception handling method of your choice
    }

This may help.

to create file without overwriting existing file

System.out.println("Choose folder to create file");
JFileChooser c = new JFileChooser();
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
c.showOpenDialog(c);
c.getSelectedFile();
f = c.getSelectedFile(); // File f - global variable
String newfile = f + "\\hi.doc";//.txt or .doc or .html
File file = new File(newfile);

try {
    //System.out.println(f);
    boolean flag = file.createNewFile();
             
    if(flag == true) {
        JOptionPane.showMessageDialog(rootPane, "File created successfully");
    }
    else {
        JOptionPane.showMessageDialog(rootPane, "File already exists");
    }
    /* or use exists() function as follows:
    if(file.exists() == true) {
        JOptionPane.showMessageDialog(rootPane, "File already exists");
    }
    else {
        JOptionPane.showMessageDialog(rootPane, "File created successfully");
    } */
}
catch(Exception e) {
     //any exception handling method of your choice
}
Source Link
aashima
  • 1.2k
  • 12
  • 32

This may help.

to create file without overwriting existing file

System.out.println("Choose folder to create file");
JFileChooser c = new JFileChooser();
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
c.showOpenDialog(c);
c.getSelectedFile();
f = c.getSelectedFile(); // File f - global variable
String newfile = f + "\\hi.doc";//.txt or .doc or .html
File file = new File(newfile);
    try 
    {
        //System.out.println(f);
        boolean flag = file.createNewFile();
        
        if(flag==true)
        {
            JOptionPane.showMessageDialog(rootPane, "File created successfully");
        }
        else
        {
            JOptionPane.showMessageDialog(rootPane, "File already exists");
        }
        /* or use exists() function as follows:
            if(file.exists()==true)
            {
                JOptionPane.showMessageDialog(rootPane, "File already exists");
            }
            else
            {
                JOptionPane.showMessageDialog(rootPane, "File created successfully");
            }
         
        */
    }
    
    catch(Exception e)
    {
        //any exception handling method of your choice
    }