Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe07.iad.POSTED!00000000!not-for-mail From: Owen Jacobson Newsgroups: comp.lang.java.programmer Message-ID: <2011111400244576638-angrybaldguy@gmailcom> References: <5980efbc-9010-4145-b886-fe106c5ac2d5@c18g2000yqj.googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: A strange behaviour of a File property User-Agent: Unison/2.1.5 Lines: 39 X-Complaints-To: abuse@UsenetServer.com NNTP-Posting-Date: Mon, 14 Nov 2011 05:24:45 UTC Date: Mon, 14 Nov 2011 00:24:45 -0500 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9947 On 2011-11-12 01:38:55 +0000, Eric Sosman said: > On 11/11/2011 11:25 AM, Andreas Leitgeb wrote: >> alelvb@inwind.it wrote: >>> public class Main { >>> public static void main(String[] args){ >>> File f = new File("."); // try to change the path >> >> ... >> >>> for(int i=0; i>> file_array[i] = new File("." + "\\" + content[i]); >> >> 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"); You don't need that, either, if all you're doing is enumerating directory contents like the OP. File exposes a method for obtaining the entries of a directory *as File objects* already, so reconstructing File objects from the base names and parent directory is pretty much wasted effort. Have a look at the listFiles() method. -o