Java : Help Needed in FileOutPutStream

I am working on java.
I got one requirement that.I hvae to create a file that should be stored in particular location with some unique name.
For example if first time when we run it will be example1 and secondtime filename should be example2 like this..
And that fuction should return file name also.

---------->

Below is a simple source code ....... you can modify as per your needs.
____________________________________________________________

import java.io.File;
import java.io.IOException;

public class DualOP {
 public static void main(String ar[])throws IOException{
  File fileOne = new File("one.txt");
  File fileTwo = new File("two.txt");
  boolean exists1 = fileOne.exists();
  boolean exists2 = fileTwo.exists();
  if(!exists1 && !exists2){
   fileOne.createNewFile();
   System.out.println("None exists so one.txt created");
  }else if(exists1 && (!exists2)){
   fileOne.delete();
   fileTwo.createNewFile();
   System.out.println("one exists so two.txt created");
  }else if((!exists1) && exists2){
   fileTwo.delete();
   fileOne.createNewFile();
   System.out.println("two exists so one.txt created");
  }
 }
}

-- Bharath Chinnadurai

---------->

Here's a part of code from the file upload utility of the commons package. Change it to suit your needs.

 /**
     * Creates and returns a {@link java.io.File File} representing a uniquely
     * named temporary file in the configured
repository path.
     *
     * @return The {@link java.io.File File} to be
used for temporary storage.
     */
    protected File getTempFile()
    {
        File tempDir = repository;
        if (tempDir == null)
        {
            tempDir = new File(System.getProperty("java.io.tmpdir"));
        }

        String fileName = "upload_" + getUniqueId() +
".tmp";

        File f = new File(tempDir, fileName);
        f.deleteOnExit();
        return f;
    }
 

    //
--------------------------------------------------------
Private methods

    /**
     * Returns an identifier that is unique within the class loader used to
     * load this class, but does not have random-like apearance.
     *
     * @return A String with the non-random looking instance identifier.
     */
    private static String getUniqueId()
    {
        int current;
        synchronized (DefaultFileItem.class)
        {
            current = counter++;
        }
        String id = Integer.toString(current);

        // If you manage to get more than 100 million
of ids, you'll
        // start getting ids longer than 8 characters.
        if (current < 100000000)
        {
            id = ("00000000" +
id).substring(id.length());
        }
        return id;
    }

}

-- Vinay.

Related:

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.