Java Reference
In-Depth Information
Table 2-2. File properties
Name
Type
Meaning
The system-dependent filename separator character (e.g., / or \).
separator
static
String
static char
The system-dependent filename separator character (e.g., / or \).
separatorChar
The system-dependent path separator character, represented as a string for
convenience.
pathSeparator
static
String
pathSeparatorChar static char
The system-dependent path separator character.
Both filename and path separators are normally characters, but they are also available in
String
form for convenience.
A second, more general, mechanism is the system
Properties
object mentioned in
Getting In-
formation from System Properties
.
You can use this to determine the operating system you
are running on. Here is code that simply lists the system properties; it can be informative to
run this on several different implementations:
public
public class
class
SysPropDemo
SysPropDemo
{
public
public static
static
void
void
main
(
String
[]
argv
)
throws
throws
IOException
{
iif
(
argv
.
length
==
0
)
System
.
getProperties
().
list
(
System
.
out
);
else
else
{
for
for
(
String s
:
argv
) {
System
.
out
.
println
(
s
+
" = "
+
System
.
getProperty
(
s
));
}
}
}
}
Some OSes, for example, provide a mechanism called “the null device” that can be used to
discard output (typically used for timing purposes). Here is code that asks the system proper-
ties for the “os.name” and uses it to make up a name that can be used for discarding data (if
no null device is known for the given platform, we return the name
junk
, which means that
on such platforms, we'll occasionally create, well, junk files; I just remove these files when I
stumble across them):