A strange behaviour of a File property

A

alelvb

Hello,
I've written this toy-code to test the funcionality of the File class
and I've found a strange behaviour.
My code works only with the dot directory (the directory where the
Main.class is contained) and it doesn't with any other. What's the
problem?

here is the code

<cut here>

import java.io.*;

public class Main {
public static void main(String[] args){
File f = new File("."); // try to change the path
File[] file_array = null;
String[] content = null;
content = f.list();
file_array = new File[content.length];
boolean[] bool_array1 = new boolean[content.length];
boolean[] bool_array2 = new boolean[content.length];

for(int i=0; i<content.length; i++){

file_array = new File("." + "\\" + content);
bool_array1 = file_array.isDirectory();
bool_array2 = file_array.isFile();

System.out.println(content + "\tis a directory?: " +
bool_array1);
}
}

}
 
L

Lew

I've written this toy-code to test the funcionality of the File class
and I've found a strange behaviour.
My code works only with the dot directory (the directory where the
Main.class is contained) and it doesn't with any other. What's the
problem?

What *precisely* are the symptoms when it doesn't work, and what values other than "." have you tried?

It's difficult to help when you're vague about the problem.
here is the code

<cut here>

import java.io.*;

public class Main {
public static void main(String[] args){
File f = new File("."); // try to change the path

DO NOT INDENT USENET POSTS WITH TAB CHARACTERS!

If you're asking the world of strangers to help, please do try to make it easy to read your code.

Use spaces to indent, up to four per level.
File[] file_array = null;'
String[] content = null;
content = f.list();

Why not 'String[] content = f.list();'?

Extra asssignments to 'null' are not helpful.
file_array = new File[content.length];
boolean[] bool_array1 = new boolean[content.length];
boolean[] bool_array2 = new boolean[content.length];

for(int i=0; i<content.length; i++){

file_array = new File("." + "\\" + content);


Does 'content' contain what you expect?
bool_array1 = file_array.isDirectory();
bool_array2 = file_array.isFile();


This value is not used?
System.out.println(content + "\tis a directory?: " +
bool_array1);
}
}

}


What are your outputs in the scenario that "works" (and what does "works" mean?) and in the scenario that doesn't?
 
A

Andreas Leitgeb

public class Main {
public static void main(String[] args){
File f = new File("."); // try to change the path
....

for(int i=0; i<content.length; i++){
file_array = new File("." + "\\" + content);


First of all, did you change the path also here?
or better: define a variable and use it in both spots.

Second, hardcoding "\\" is the worst approach at assembling a
file name from components. See the docu for File class for
a static field that contains the appropriate separator char
for the current platform. For test code, "/" is often good
enough (even on Windows).
 
A

alelvb

If you're asking the world of strangers to help, please do try to make it easy to read your code.

Use spaces to indent, up to four per level.

ok it'll be done, i'm sorry

What are your outputs in the scenario that "works" (and what does "works" mean?) and in the scenario that doesn't?


the call to isDirectory() returns true only for the directories
present in the "." directory, for every other path it returns always
false, even when the file >is< a directory.
 
A

alelvb

public class Main {
   public static void main(String[] args){
      File f = new File(".");        // try to change the path
...

      for(int i=0; i<content.length; i++){
          file_array = new File("." + "\\" + content);

First of all, did you change the path also here?
   or better: define a variable and use it in both spots.

Yes, I know that I'd have had used a variable.
You can remove the "." + "\\" + string and the code will work out the
same.
Second, hardcoding "\\" is the worst approach at assembling a
  file name from components.  See the docu for File class for
  a static field that contains the appropriate separator char
  for the current platform.  For test code, "/" is often good
  enough (even on Windows).

do you mean that a path like C:\Documents and Settings\Program Files
in Java can be inputed like C:/Documents and Settings/Program Files
even when running on Windows?
ok it's good to know it
 
A

Arne Vajhøj

do you mean that a path like C:\Documents and Settings\Program Files
in Java can be inputed like C:/Documents and Settings/Program Files
even when running on Windows?

It works.

Arne
 
L

Lew

public class Main {
public static void main(String[] args){
File f = new File("."); // try to change the path
...

for(int i=0; i<content.length; i++){
file_array = new File("." + "\\" + content);


First of all, did you change the path also here?
or better: define a variable and use it in both spots.

Second, hardcoding "\\" is the worst approach at assembling a
file name from components. See the docu for File class for
a static field that contains the appropriate separator char
for the current platform. For test code, "/" is often good
enough (even on Windows).


Or you could use one of:
<http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.io.File, java.lang.String)>
or
<http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.lang.String, java.lang.String)>
 
L

Lew

ok it'll be done, i'm sorry




the call to isDirectory() returns true only for the directories
present in the "." directory, for every other path it returns always
false, even when the file >is< a directory.

I repeat:

What *precisely* are the symptoms when it doesn't work, and what values other than "." have you tried?

It's difficult to help when you're vague about the problem.

What is "every other path"? What are the inputs and outputs that you see?

The code you show doesn't show the scenario that fails. How can we tell what's failing when you don't show the code?

The word "precisely" was emphasized for a reason. Your information is so vague, I don't even know what's going wrong much less why.

Follow the advice in
http://sscce.org/

Try to imagine that you are not at all familiar with your code, and read your explanation. Does it tell you enough to figure out what's wrong, let alone why?

Where's the code?

Where's the code?

What are the outputs?

You haven't shown any inputs, outputs or code for the failure case.

Sorry, can't help you with such little data.
 
E

Eric Sosman

public class Main {
public static void main(String[] args){
File f = new File("."); // try to change the path
...

for(int i=0; i<content.length; i++){
file_array = new File("." + "\\" + content);


First of all, did you change the path also here?
or better: define a variable and use it in both spots.

Second, hardcoding "\\" is the worst approach at assembling a
file name from components. See the docu for File class for
a static field that contains the appropriate separator char
for the current platform. For test code, "/" is often good
enough (even on Windows).


Even better is to forgo the silly string-bashing and let File
figure it out:

File parentDir = ...;
File childFile = new File(parentDir, "child");

IMHO, Java errs in exposing any "path separator" at all, because
it just encourages string-bashing. Note that in some file systems
there is no such thing as a "path separator;" on one such I had
files with names like

SYS$DISK:[USERS.ERIC.PROJECT]README.TXT;22

.... and even this example doesn't show the full glory(?) of the syntax,
which could also supply host names, user names, and passwords -- all
as part of what a File *ought* to be able to manage.

(In the O.P.'s specific case, using listFiles() instead of list()
would simplify things a good deal.)
 
A

Andreas Leitgeb

Eric Sosman said:
file_array = new File("." + "\\" + content);


Even better is to forgo the silly string-bashing and let File
figure it out:
File parentDir = ...;
File childFile = new File(parentDir, "child");


Indeed a good remark.

It reminds me also of the caveat from old DOS days, where .\foo.exe
wouldn't work, if the current directory (containing foo.exe) was a
drive's root directory, as root directories just didn't have a "."
entry.
Is that still so? I don't have a Windows machine at hand right now to
test.
IMHO, Java errs in exposing any "path separator" at all, because
it just encourages string-bashing. Note that in some file systems
there is no such thing as a "path separator;" on one such I had
files with names like
SYS$DISK:[USERS.ERIC.PROJECT]README.TXT;22

Such beasts still exist in the wild?
Or was it something embedded like e.g. on a smartcard?
 
D

Daniel Pitts

IMHO, Java errs in exposing any "path separator" at all, because
it just encourages string-bashing. Note that in some file systems
there is no such thing as a "path separator;" on one such I had
files with names like

SYS$DISK:[USERS.ERIC.PROJECT]README.TXT;22

.... and even this example doesn't show the full glory(?) of the syntax,
which could also supply host names, user names, and passwords -- all
as part of what a File *ought* to be able to manage.
Ah, good old VMS.

Revision number 22 of README.TXT in the PROJECT directory in the ERIC
directory in the USERS directory :)

The way I describe VMS CLI is to contrast it with Linux. in Linux, you
give "garbled english" commands and get well-formed english responses.
In VMS the converse is true.
 
R

Roedy Green

File f = new File("."); // try to change the path

You can discover the value of the set path= environment variable with
System.getEnv( "path" ) but you cannot change it.

You can't change the CWD inside Java. It stays constant for your run.
Yo would have spawn shell scripts to get that effect.

The File class has lots of surprises. See

http://mindprod.com/jgloss/filesanddirectories.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
Windows is a case-insensitive operating system,
but that does not mean you can forget about case.
For example, Let us assume you have a file called
Abc.txt in C:\temp, and a file called
aBc.txt in D:\temp and you type
copy C:\temp\abC.txt D:\temp. What is the name of the file in
D:\temp when you are done?
1) Abc.txt 2) aBc.txt 3) abC.txt 4) abc.txt 5) ABC.txt
Hint, the answer rhymes with the most popular word in advertising.
 
E

Eric Sosman

Eric Sosman said:
[...] Note that in some file systems
there is no such thing as a "path separator;" on one such I had
files with names like
SYS$DISK:[USERS.ERIC.PROJECT]README.TXT;22

Such beasts still exist in the wild?
Or was it something embedded like e.g. on a smartcard?

http://en.wikipedia.org/wiki/Files-11#Disk_organization_and_naming

"A fossil!" I hear you cry, "A dried relic of prehistory!"
Yet, according to

http://en.wikipedia.org/wiki/OpenVMS#Major_release_timeline

.... there was a new release (V8.4) just over a year ago. The V7.0
version was roughly contemporaneous with the first release of Java.
 
E

Eric Sosman

[...]
The correct behaviour of the program is obtained only if I use "."

import java.io.File;
public class Alexo {
public static void main(String[] unused) {
tryit(".");
tryit("\\");
}
private static void tryit(String start) {
System.out.println("Starting String:\t" + start);
File begin = new File(start);
System.out.println("Starting File:\t" + begin);
for (File child : begin.listFiles()) {
char file = child.isFile() ? 'Y' : 'N';
char dir = child.isDirectory() ? 'Y' : 'N';
System.out.println("File? " + file + ", Dir? " + dir
+ "\t" + child);
}
System.out.println();
}
}

Starting String: .
Starting File: .
File? N, Dir? Y .\build
File? Y, Dir? N .\build.xml
File? N, Dir? Y .\coverage
File? Y, Dir? N .\manifest.mf
File? N, Dir? Y .\nbproject
File? N, Dir? Y .\src
File? N, Dir? Y .\test

Starting String: \
Starting File: \
File? Y, Dir? N \AUTOEXEC.BAT
File? Y, Dir? N \boot.ini
File? Y, Dir? N \BOOTSECT.DOS
File? N, Dir? Y \Config.Msi
File? Y, Dir? N \CONFIG.SYS
File? N, Dir? Y \djgpp
File? N, Dir? Y \Documents and Settings
File? N, Dir? Y \DRIVERS
File? N, Dir? Y \I386
File? Y, Dir? N \INSTALL.LOG
File? Y, Dir? N \IO.SYS
File? Y, Dir? N \IPH.PH
File? N, Dir? Y \Media
File? Y, Dir? N \MSDOS.SYS
File? N, Dir? Y \My Music
File? Y, Dir? N \net_save.dna
File? Y, Dir? N \NTDETECT.COM
File? Y, Dir? N \ntldr
File? N, Dir? Y \NVIDIA
File? Y, Dir? N \pagefile.sys
File? N, Dir? Y \Program Files
File? N, Dir? Y \RECYCLER
File? N, Dir? Y \System Volume Information
File? N, Dir? Y \TEMP
File? Y, Dir? N \uninstall.log
File? N, Dir? Y \WINDOWS
File? N, Dir? Y \WINNT
File? N, Dir? Y \WUTemp
 
A

Arne Vajhøj

You can discover the value of the set path= environment variable with
System.getEnv( "path" ) but you cannot change it.

Path does not mean DOS/Windows global search path her - it means path
in the Java meaning.

Given that this is a Java group, then that should not be so
surprising.

Arne
 
Z

Zlatko Äurić

Alexo said:
hei Eric your code works, but where is the trouble in mine?

I think you've been told, here is the problem. You get the list of the "/"
directory with the:
File f = new File("/");

which is the wanted list of filenames.
And then you're trying to take something out of the "." directory, each time
you go through:
file_array = new File("." + "\\" + content);
 
E

Eric Sosman

hei Eric your code works, but where is the trouble in mine?

No idea. You've shown us part (well, most) of your code, but
you haven't given a description of how you've used it. You've
said things like "try to change the path" and "when I try to
change the path," but that's not precise enough to allow me or
someone else to duplicate your actions.

Observe an important difference between my example and yours:
Yours is "Here's the code, but you'll need instructions for running
it," while mine is "Here's the code, entirely self-contained."
Do you see why one is easier to diagnose than the other?
 
A

Andreas Leitgeb

Eric Sosman said:
Eric Sosman said:
[...] Note that in some file systems
there is no such thing as a "path separator;" on one such I had
files with names like
SYS$DISK:[USERS.ERIC.PROJECT]README.TXT;22
Such beasts still exist in the wild?
Or was it something embedded like e.g. on a smartcard?
http://en.wikipedia.org/wiki/Files-11#Disk_organization_and_naming
"A fossil!" I hear you cry, "A dried relic of prehistory!"

I'm not the type who would cry out about it, but I admit, I did think
something more or less similar.
Yet, according to
http://en.wikipedia.org/wiki/OpenVMS#Major_release_timeline
... there was a new release (V8.4) just over a year ago. The V7.0
version was roughly contemporaneous with the first release of Java.

I also admit, I'm surprised that at least Java 6 appears to be available
for it. (result of brief googling)
 
A

Arne Vajhøj

Eric Sosman said:
IMHO, Java errs in exposing any "path separator" at all, because
it just encourages string-bashing. Note that in some file systems
there is no such thing as a "path separator;" on one such I had
files with names like
SYS$DISK:[USERS.ERIC.PROJECT]README.TXT;22

Such beasts still exist in the wild?

Yes !

$ type DISK2:[ARNE]HELLOWORLD.JAVA;1
public class HelloWorld {
public static void main(String[] args) throws Exception {
System.out.println("Hello world!");
}
}
$ javac helloworld.java
$ java "HelloWorld"
Hello world!

Arne
 
A

Arne Vajhøj

Eric Sosman said:
[...] Note that in some file systems
there is no such thing as a "path separator;" on one such I had
files with names like
SYS$DISK:[USERS.ERIC.PROJECT]README.TXT;22
Such beasts still exist in the wild?
Or was it something embedded like e.g. on a smartcard?
http://en.wikipedia.org/wiki/Files-11#Disk_organization_and_naming
"A fossil!" I hear you cry, "A dried relic of prehistory!"

I'm not the type who would cry out about it, but I admit, I did think
something more or less similar.

I do not consider version numbers prehistoric relics.

:)

Note that:
* ODS-5 file systems are case insensitive case preserving (Windows
style) unlike ODS-2 that is case insensitive uppercasing (DOS style)
* Java actually supports both native syntax and traditional
*nix / syntax for filenames

import java.io.*;

public class Disp {
public static void show(String fnm) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fnm));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
public static void main(String[] args) throws Exception {
show("DISK2:[ARNE]HELLOWORLD.JAVA");
show("/disk2/arne/helloworld.java");
}
}

actually displays the same file twice.
I also admit, I'm surprised that at least Java 6 appears to be available
for it. (result of brief googling)

6.0-3 which is equivalent of Oracle 6.0u27 was released last month.

But no 1.7 yet.

Arne
 

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