A strange behaviour of a File property

Discussion in 'Java' started by alelvb@inwind.it, Nov 11, 2011.

  1. Guest

    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);
    }
    }

    }
    , Nov 11, 2011
    #1
    1. Advertising

  2. Lew Guest

    wrote:
    > 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?

    --
    Lew
    Lew, Nov 11, 2011
    #2
    1. Advertising

  3. <> wrote:
    > 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).
    Andreas Leitgeb, Nov 11, 2011
    #3
  4. Guest


    > 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.
    , Nov 11, 2011
    #4
  5. Guest

    > > 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
    , Nov 11, 2011
    #5
  6. Arne Vajhøj Guest

    On 11/11/2011 12:42 PM, wrote:
    >> 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?


    It works.

    Arne
    Arne Vajhøj, Nov 11, 2011
    #6
  7. Lew Guest

    On Friday, November 11, 2011 8:25:11 AM UTC-8, Andreas Leitgeb wrote:
    > <> wrote:
    > > 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)>

    --
    Lew
    Lew, Nov 11, 2011
    #7
  8. Lew Guest

    On Friday, November 11, 2011 9:28:59 AM UTC-8, wrote:
    > > 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.


    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.

    --
    Lew
    Lew, Nov 11, 2011
    #8
  9. Eric Sosman Guest

    On 11/11/2011 11:25 AM, Andreas Leitgeb wrote:
    > <> wrote:
    >> 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.)

    --
    Eric Sosman
    d
    Eric Sosman, Nov 12, 2011
    #9
  10. Eric Sosman <> wrote:
    > On 11/11/2011 11:25 AM, Andreas Leitgeb wrote:
    >> <> wrote:
    >>> 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?
    Andreas Leitgeb, Nov 12, 2011
    #10
  11. Daniel Pitts Guest

    On 11/11/11 5:38 PM, Eric Sosman wrote:
    > 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.
    Daniel Pitts, Nov 12, 2011
    #11
  12. Roedy Green Guest

    On Fri, 11 Nov 2011 05:45:29 -0800 (PST), wrote,
    quoted or indirectly quoted someone who said :

    > 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.
    Roedy Green, Nov 12, 2011
    #12
  13. Eric Sosman Guest

    On 11/11/2011 10:21 PM, Andreas Leitgeb wrote:
    > Eric Sosman<> wrote:
    >> [...] 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.

    --
    Eric Sosman
    d
    Eric Sosman, Nov 12, 2011
    #13
  14. Eric Sosman Guest

    On 11/12/2011 9:04 AM, Alexo wrote:
    >[...]
    > 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

    --
    Eric Sosman
    d
    Eric Sosman, Nov 12, 2011
    #14
  15. Arne Vajhøj Guest

    On 11/12/2011 8:00 AM, Roedy Green wrote:
    > On Fri, 11 Nov 2011 05:45:29 -0800 (PST), wrote,
    > quoted or indirectly quoted someone who said :
    >
    >> 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.


    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
    Arne Vajhøj, Nov 12, 2011
    #15
  16. Alexo wrote:

    > 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);




    --
    Zlatko
    Zlatko Äurić, Nov 12, 2011
    #16
  17. Eric Sosman Guest

    On 11/12/2011 10:17 AM, Alexo wrote:
    > 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?

    --
    Eric Sosman
    d
    Eric Sosman, Nov 12, 2011
    #17
  18. Eric Sosman <> wrote:
    > On 11/11/2011 10:21 PM, Andreas Leitgeb wrote:
    >> Eric Sosman<> wrote:
    >>> [...] 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)
    Andreas Leitgeb, Nov 12, 2011
    #18
  19. Arne Vajhøj Guest

    On 11/11/2011 10:21 PM, Andreas Leitgeb wrote:
    > Eric Sosman<> wrote:
    >> 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
    Arne Vajhøj, Nov 12, 2011
    #19
  20. Arne Vajhøj Guest

    On 11/12/2011 5:09 PM, Andreas Leitgeb wrote:
    > Eric Sosman<> wrote:
    >> On 11/11/2011 10:21 PM, Andreas Leitgeb wrote:
    >>> Eric Sosman<> wrote:
    >>>> [...] 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.

    >> 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)


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

    But no 1.7 yet.

    Arne
    Arne Vajhøj, Nov 12, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    725
  2. Andy Chambers
    Replies:
    1
    Views:
    385
    Daniel Dyer
    May 14, 2007
  3. Dev_SH
    Replies:
    0
    Views:
    658
    Dev_SH
    Dec 30, 2010
  4. Francesco Moi

    Strange behaviour when parsing a XML file

    Francesco Moi, Jul 26, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    199
    Francesco Moi
    Jul 27, 2005
  5. felix
    Replies:
    34
    Views:
    775
    James Kuyper
    Nov 19, 2012
Loading...

Share This Page