File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes System.getProperty( ) method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.getProperty( ) method" Watch "System.getProperty( ) method" New topic
Author

System.getProperty( ) method

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jul 22, 2000
Posts: 9053
    
  12
It is getting the value of the property named file.separator from your system properties.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Jul 22, 2000
Posts: 9053
    
  12
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Jul 22, 2000
Posts: 9053
    
  12
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

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Jul 22, 2000
Posts: 9053
    
  12
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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System.getProperty( ) method