This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.getProperty( ) method

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.getProperty("file.separator");

what this line is doing and why we are doing this ?
can any body please explain me ..
thanks a lot .
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is getting the value of the property named file.separator from your system properties.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is is what you are saying :
there is a property file in my system , that has one property file.separator and we are taking the value of this .
If it is right then what is the name of that property file , where it is & can we see or modify that property file ?

please solve my doubt ..
thanks a lot .
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what the JavaDocs have to say, which should be the first place you go when trying to understand a method in the standard Java libraries (you can download them to view locally as well):
Gets the system property indicated by the specified key. . . . If there is no current set of system properties, a set of system properties is first created and initialized in the same manner as for the getProperties method.
Ah, so it seems to load properties using getProperties(). Here's that method's description:
Determines the current system properties. . . . The current set of system properties for use by the getProperty(String) method is returned as a Properties object. If there is no current set of system properties, a set of system properties is first created and initialized. This set of system properties always includes values for the following keys:
So it seems that the JVM is built with these properties as pat of it rather than reading them from a file. But it doesn't specify that. It just says it creates them, and that's all you need to know to use them.

Of course, you can also provide your own set of properties using setProperties(Properties) if you want to try to blow up your JVM.
[ February 02, 2005: Message edited by: David Harkness ]
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add properties to System.properties. There is a time and a place for that. However, the jvm reads the file.separator property from the system that it is running on each time. Why do you want to change it? You can just hardcode the '/' or '\' if you need it to be one way or the other.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you every body . Definately at this stage I will not use this type of code but my project using that ...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
Thank you every body . Definately at this stage I will not use this type of code but my project using that ...


I would strongly encourage you to start writing code like this now. The reason is that it makes your code more portable. As explained above, different operating systems use different characters to separate the directory (or folder) names in the path for a file. Using the "file.seperator" property allows your program to run on different operating systems with fewer changes than if you hard-coded the file separator.

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
Is is what you are saying :
there is a property file in my system , that has one property file.separator and we are taking the value of this .
If it is right then what is the name of that property file , where it is & can we see or modify that property file ?

please solve my doubt ..
thanks a lot .


In case it wasn't clear from earlier posts, there is NO single property file that stores this value. The JVM has several properties that are loaded into memory. The JVM probably queries the Operating System for the value. It's up to the OS where exactly this value is stored. In fact, that's the reason for writing code like this. It frees your program from being dependant on a particular operating system.

Layne
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed, Layne. But he wants to change the file.separator system property. I'm suggesting that he should not do that.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marilyn de Queiroz:
Agreed, Layne. But he wants to change the file.separator system property. I'm suggesting that he should not do that.


I don't see anywhere that the OP mentioned changing any properties. David suggested in his answer to the OP's question that you can. I agree that changing existing system properties is a Bad Thing (TM). In fact, now that I think about it, setting ANY system properties is not really necessary. If your application needs its own properties, you could just use the Properties class or something similar.

Layne
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rathi Ji's response to my first response:

Originally posted by rathi ji:
... what is the name of that property file , where it is & can we see or modify that property file ?


(Italics added by Marilyn)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic