• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

specifying paths to create file

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm simply learning how to write data to a file with the FileWriter class. This seems simple enough, but how do I pass a pathname as an argument? The constructor is:

or

Basically, if I pass just a file name, it will create the file in the \\jdev\bin directory. What if I want an absolute path, such as "c:\903\jdev\mywork..."? When I pass this as an argument, the compiler reads the backslash as an escape character, which is NOT what I want. Help?
David
nothing fancy to say yet
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the single argument is an absolute path, it will work fine. But single backslashes have special meaning in Java strings, so you've got to double them:

You can actually also use forward slashes; Windows is just fine with them.
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would that then be "c:/903/jdev/mywork/file.txt" ? Thank you for your response.
David
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, "c:/903/jdev/mywork/file.txt" will work. In fact, I try and avoid the back-slash at all cost because it looks ugly ;-)
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While we're on the topics...
Note that putting two \ characters together is referred to as character "escaping". The \ character is the escape character, which is used in combination with other characters to specify various special sequences. Section 3.10.6 of the JLS describes the various escape sequences. As Ernest already pointed out, to specify a \ character, it must be escaped - by itself.
If we were to ponder whether it's a good idea to specify absolute file names in a Java application, we might well come to the conclusion that it is not. Because how could such a thing be written once and run everywhere?
In many situations, it may be a good idea to let the user specify the absolute location of some file, by doing something like editing a properties file. Such a thing could be done manually, as part of the required installation of the app, or through some setup routine, which allowed the user to specify the location through a running program.
reply
    Bookmark Topic Watch Topic
  • New Topic