Results 1 to 4 of 4
  1. #1
    mavuk is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default File[] turned into a string?

    Hi all,

    I'm currently trying to teach myself java while working on a pet project that I originally wrote in (very bad) PERL. I want to get the available root drives as simple strings to be used else where. Getting the drive names seems simple using:

    File[] roots = File.listRoots();
    for(int i=0;i<roots.length;i++) {
    System.out.println("Root["+i+"]:" + roots[i]);}

    But this returns the data, roots, as a File object. Is there some way to get the drives (in the case of Windows, C:/ for example as a simple string for future use?

    Cheers!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: File[] turned into a string?

    What are you intending to do once you have the drives? Are you saying you wish to get the String representation of the File path? If yes, see the getAbsolutePath() method of File.

  3. #3
    mavuk is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: File[] turned into a string?

    Ok, thanks. I'll have a look.

    I'm trying to create e very basic drive chooser that will give the user the options of the currently available local drive roots, so they can choose the desired one and the program can then use that (string) to do other manipulations. Thanks for your help!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: File[] turned into a string?

    In general -- not just for the File class -- wherever the toString() method doesn't give you the human-readable form that you hoped for, go through the API and read up on all the methods that return String.

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

Similar Threads

  1. searching a string in a file
    By sathya11491 in forum New To Java
    Replies: 4
    Last Post: 03-27-2012, 01:12 PM
  2. Replies: 2
    Last Post: 05-25-2011, 02:33 PM
  3. Read a file and converting this file into a string
    By kostinio in forum New To Java
    Replies: 7
    Last Post: 12-26-2009, 03:54 PM
  4. String to file name
    By merehephet in forum New To Java
    Replies: 12
    Last Post: 11-17-2009, 04:59 PM

Posting Permissions

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