Request Help - how to pass correctly formatted strings to Java inWindows

Discussion in 'Java' started by Daku, Dec 30, 2009.

  1. Daku

    Daku Guest

    Could some Java guru please help ? I am trying to port )from Linux to
    Windows) an application that will open directories and search for
    files with specific extensions
    inside.

    I have found that in Windows, to open a directory, I need to write,
    for example:

    File file = new File("C:\\temp\\");

    However, hard-coded string literals are unaaceptable and I have to
    read in the
    directory names from a property file
    with entries as:
    srcdir=C:\\temp\\
    .........

    After the properties file is read in, if I
    do a System.out.println(..); I get the
    following to be displayed: C:\temp\

    If I pass this string to the File constructor,
    the directory does not open.
    The question is how do I modify the String
    that is read in, so that when passed to
    the File constructor, it sees it as
    the literal "C:\\temp\\"

    Any hints, suggessstions would be of immense value. thanks in advance
    for
    your help.
     
    Daku, Dec 30, 2009
    #1
    1. Advertising

  2. Daku

    Arne Vajhøj Guest

    Re: Request Help - how to pass correctly formatted strings to Javain Windows

    On 30-12-2009 10:12, Daku wrote:
    > Could some Java guru please help ? I am trying to port )from Linux to
    > Windows) an application that will open directories and search for
    > files with specific extensions
    > inside.
    >
    > I have found that in Windows, to open a directory, I need to write,
    > for example:
    >
    > File file = new File("C:\\temp\\");
    >
    > However, hard-coded string literals are unaaceptable and I have to
    > read in the
    > directory names from a property file
    > with entries as:
    > srcdir=C:\\temp\\
    > ........
    >
    > After the properties file is read in, if I
    > do a System.out.println(..); I get the
    > following to be displayed: C:\temp\
    >
    > If I pass this string to the File constructor,
    > the directory does not open.


    It should.

    > The question is how do I modify the String
    > that is read in, so that when passed to
    > the File constructor, it sees it as
    > the literal "C:\\temp\\"


    It already should.

    > Any hints, suggessstions would be of immense value.


    You could use / instead of \. Java on Windows does accept /.

    Arne
     
    Arne Vajhøj, Dec 30, 2009
    #2
    1. Advertising

  3. Daku

    Eric Sosman Guest

    Re: Request Help - how to pass correctly formatted strings to Javain Windows

    On 12/30/2009 10:12 AM, Daku wrote:
    > Could some Java guru please help ? I am trying to port )from Linux to
    > Windows) an application that will open directories and search for
    > files with specific extensions
    > inside.
    >
    > I have found that in Windows, to open a directory, I need to write,
    > for example:
    >
    > File file = new File("C:\\temp\\");
    >
    > However, hard-coded string literals are unaaceptable and I have to
    > read in the
    > directory names from a property file
    > with entries as:
    > srcdir=C:\\temp\\
    > ........
    >
    > After the properties file is read in, if I
    > do a System.out.println(..); I get the
    > following to be displayed: C:\temp\
    >
    > If I pass this string to the File constructor,
    > the directory does not open.


    The name of the File class is unfortunate, because it is
    somewhat deceptive. A File object doesn't really represent a
    file, but a "file name" or "file path" or "file designator."
    Simply constructing a File object doesn't do anything to or
    with the file that the File names; it doesn't try to "open" it,
    for example. (I don't think constructing a File does anything
    at all with the file system -- the fact that File's constructors
    don't throw IOException is suggestive.)

    Once you've got a File object, you can use some of its
    methods to do things with the file that the File names: You
    can find out whether such a file exists, you can ask whether
    it's a data file or a directory, you can change its name, and
    so on. Which of these operations "does not open" for you?

    > The question is how do I modify the String
    > that is read in, so that when passed to
    > the File constructor, it sees it as
    > the literal "C:\\temp\\"


    You almost certainly don't want the File constructor to
    see the String whose twelve characters are "C:\\temp\\" , but
    the eight-character String C:\temp\ . Keep in mind that the
    quotation marks and the doubled backslashes are "markup" for
    the "payload" that you want -- and from what you say of the
    println() output, it appears that the "markup" has already
    been processed and you're good to go. It might be that you
    need to use the seven-character String C:\temp (without the
    final backslash), but I'm not sure.

    > Any hints, suggessstions would be of immense value. thanks in advance
    > for
    > your help.


    What we need most, I think, is to see some actual code.
    Boil your program down to the smallest complete chunk that
    demonstrates the difficulty, and post that chunk. (Post it
    with copy-and-paste; let's have no more of the transcription
    errors that crept into your other, similar thread!) Describe
    what you want the code to do, and what it actually does when
    you try to run it. That will give potential helpers a lot
    more to work with than we have now.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 30, 2009
    #3
  4. Daku

    kopik

    Joined:
    Dec 31, 2009
    Messages:
    6
    hii,

    WinPath = WinPath.replace("\\", "\\\\");

    .. kopik
     
    kopik, Dec 31, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryniek90
    Replies:
    0
    Views:
    557
    Ryniek90
    Aug 24, 2009
  2. Aleksey
    Replies:
    1
    Views:
    324
    Nobody
    Jan 23, 2010
  3. MBALOVER
    Replies:
    9
    Views:
    2,301
    Richard Bos
    Mar 17, 2010
  4. Laphan
    Replies:
    0
    Views:
    118
    Laphan
    Nov 29, 2005
  5. Alf P. Steinbach
    Replies:
    11
    Views:
    538
    Tobias Müller
    Nov 26, 2011
Loading...

Share This Page