Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How do you check to see if a RandomAccessFile already exists?

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default How do you check to see if a RandomAccessFile already exists?

    I'm asking the user to give me the name of the file they wish to write to. If that file already exists, I will ask them if they wish to update that file, or just create a different file with a different name. If the file doesn't exist, I just create the file right there.

    This was working well through the ".exists()" method of the File class, but apparently RandomAccessFiles can't do that??

    I looked the API for the RAF and nowhere have I found anything that resembles ".exists()".

    Here's my code:
    System.out.println("What is the name of the file you would like to write to?");
            public RandomAccessFile receiveFile ()
            {
                    String fileName;
                    String yesno = "y";
                    RandomAccessFile raf = null;
                    Scanner input = new Scanner(System.in);
     
                    do
                    {
                            System.out.println("What is the name of the file you would like to write to?");
                            fileName = input.next();
     
                            try
                            {
                                    raf = new RandomAccessFile(fileName, "rw");
     
                            } catch (IOException ex)
                            {
                                    System.out.println();
                            }
     
                            // check if file already exists
                            if(raf.exists()) // ERROR THIS DOESN'T WORK
                            {
                                    System.out.println("That file already exists. Would you like to update it? (y/n)");
                                    yesno = input.next();
                            }
     
                    } while (yesno.equals("n"));
     
                    return raf;
            }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do you check to see if a RandomAccessFile already exists?

    But you can still use the user-specified filename as a File, determine if it exists, etc., and then create the RandomAccessFile using the constructor that takes a File as an argument. Won't that work?

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    ineedahero (November 2nd, 2013)

  4. #3
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: How do you check to see if a RandomAccessFile already exists?

    Well that works! Haha, thanks.

Similar Threads

  1. check if table exists from a list of files in a directory
    By efoikonom in forum JDBC & Databases
    Replies: 1
    Last Post: June 10th, 2013, 08:42 AM
  2. Question on RandomAccessFile searching
    By tcstcs in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 9th, 2013, 08:40 AM
  3. RandomAccessFile
    By loui345 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 5th, 2012, 04:16 PM
  4. File exists or not
    By Ballister in forum Java Theory & Questions
    Replies: 2
    Last Post: January 7th, 2011, 04:38 AM
  5. RandomAccessFile.readByte() find EOF
    By chopficaro in forum Java Theory & Questions
    Replies: 3
    Last Post: May 6th, 2010, 03:14 AM