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

D

Daku

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.
 
A

Arne Vajhøj

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
 
E

Eric Sosman

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top