Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Creating a file

  1. #1
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    19
    Rep Power
    0

    Default Creating a file

    It is possible to delay the creation of a file until I know I am writing to it.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Creating a file

    You need to clarify your question, preferably with a short example code. A new file will be created on disk only when you write to it -- not before that.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Creating a file

    Are you sure? I used this one line and a file was created:
    Java Code:
          FileOutputStream fos = new FileOutputStream("TestingCreation.txt");
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Creating a file

    That means I was wrong. Apologies, and thanks for the correction!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Creating a file

    Why not create a small class for the purpose? Something like this will do:

    Java Code:
    public class LazyFile {
       private FileOutputStream fos;
       private String name;
    
       public LazyFile(String name) { this.name= name; }
    
       public FileOutputStream get() throws FileNotFoundException {
          if (fos == null)
             fos= new FileOutputStream(name);
          return fos;
       }
    }
    Create a LazyFile object with a name and when you want to write to it, get an OutputStream by calling its get() method. You can call the get() method as many times as you want.

    kind regards,

    Jos
    DarrylBurke likes this.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. creating a jar file
    By marchello in forum New To Java
    Replies: 2
    Last Post: 04-25-2013, 06:40 PM
  2. Creating a XML file
    By Solid Snake in forum XML
    Replies: 3
    Last Post: 04-06-2012, 08:43 PM
  3. Replies: 2
    Last Post: 05-26-2011, 06:38 AM
  4. Creating an XML file with XML DOM
    By b1l0s in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2010, 06:17 AM
  5. creating .ear file
    By psn in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 01-06-2008, 06:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •