Skip to Content

Create new empty file

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.   Create new empty file
  3.   This Java example shows how to create new empty file at specified path using
  4.   createNewFile method of Java File class.
  5. */
  6.  
  7. import java.io.*;
  8.  
  9. public class CreateNewEmptyFile {
  10.  
  11.   public static void main(String[] args) {
  12.    
  13.     //create File object
  14.     File file = new File("C://demo.txt");
  15.    
  16.     /*
  17.      * To actually create a file specified by a pathname, use
  18.      * boolean createNewFile() method of Java File class.
  19.      *
  20.      * This method creates a new empty file specified if the file with same
  21.      * name does not exists.
  22.      *
  23.      * This method returns true if the file with the same name did not exist and
  24.      * it was created successfully, false otherwise.
  25.      */
  26.    
  27.      boolean blnCreated = false;
  28.      try
  29.      {
  30.        blnCreated = file.createNewFile();
  31.      }
  32.      catch(IOException ioe)
  33.      {
  34.        System.out.println("Error while creating a new empty file :" + ioe);
  35.      }
  36.    
  37.      System.out.println("Was file " + file.getPath() + " created ? : " + blnCreated);
  38.    
  39.      /*
  40.       * If you run the same program 2 times, first time it should return true.
  41.       * But when we run it second time, it returns false because file was already
  42.       * exist.
  43.       */
  44.   }
  45. }
  46.  
  47. /*
  48. Output would be
  49. Was file C:\demo.txt created ? : true
  50. */