File class question

Hi all,
I've been playing with the File class (as a way of learning about it) and am confused about the behaviour of the following code.
This class is uses recursion as a way of getting a file structure.
import java.io.File;
public class FileTest {
    public static void main(String[] args) {
        if (args.length == 0) {
            return;
        File file = new File(args[0]);
        processFileList(file);
    public static void processFileList(File f) {
        if (f.isDirectory()) {
            System.out.println("found directory: " + f.getName());
            String fileList[] = f.list();
            for (int i=0; i < fileList.length; i++) {
                File newF = new File(fileList);
if (newF.isFile()) {
System.out.println("Found file in list: " + newF.getName());
} else {
System.out.println("Found new directroy is list: " + newF.getName());
processFileList(newF);
} else {
System.out.println("Found file: " + f.getName());
The command line I use is java FileTest testdir
The output from the code is:
found directory: testdir
Found file in list: FileTest.class
Found file in list: Hello.class
Found file in list: LogTest.class
Found new directroy is list: testdir2
Found file: testdir2
The file structure being used is testdir, which contains the three class files and a directory called testdir2. testdir2 contains some java files.
I don't understand why testdir2 is recognised as not being a file, but is not recognised as being a directory. I changed the code to use isDirectory() rather than isFile(), but the results were similar, testdir2 is not recognised as a directory.
Any ideas? Thanks,
dan

When you instantiate the class java.io.File from a String you should use a pathname or depict the parent directory as well as the abstract file name. Or you could use the method listFiles() of the File class.
File[] fileList = f.listFiles();

Similar Messages

  • File class

    Hello, I have a curiosity question, why doesn't the File+ class provide a method for retrieving the basename of a file excluding the suffix? For example, for the path
    _"C:\some\file\path\filename.ext"_
    File*.+getName+() returns the string "*filename.ext*"; however, it would be nice if there was a convenience method such as, File*.+getName2()+, for instance, that returned "*filename*".
    I know it is trivial to do this programmatically, but I was surprised that this method was missing from File* class. Is there a particular reason for this becauseI do this all the time in Unix and Windows.
    Thanks.

    I see your point and it's vaild; however, in Unix I use the command "basename ext" specifying the suffix. I was thinking that they could have done the same thing, for instance, create the following method;
    "*+public String getName( String sSuffix )+*"
    One would have to specify the suffix you want filtered out of the filename. Then, the following code segment;
    File file = new File ( "c:\some\file\path\filename.ext" );
    System.out.println( file.getName() );
    System.out.println ( file.getName ( "ext" ) );would print the lines;
    filename.extfilename>
    Problem solved and it would be a nice convenience method :)
    Thanks

  • UrlLoader - replace with File class to access external assets

    Hi.
    Just reading up on the File class more than anything because I want to develop on mobile devices. Can I substitute all my urlLoader classes with the File class for my games on the web.
    var prefsFile:File = File.applicationDirectory;
    prefsFile = prefsFile.resolvePath("preferences.xml");
    I feel like this may be a stupid question but I have to ask.
    Cheers

    yes, but you probably don't want to use the applicationDirectory because of security issues.
    and is there some reason you can't use your urlloaders?

  • File class and an executable method?

    Unix and Windows files have attributes that indicate if they are executable. Why doesn�t Java have a �canBeExecuted� method in its File class?
    This is a question in an assignment that I am doing and I don't can't really figure out why there is no methos like this.
    Please help, and and all ideas would be most appreciated.

    I give up - where does windows have that?ntfs? what version of windows are you on! :)
    right-click->properties->security ...Sorry -- I picked a file at random and did
    rightclick->properties. There's no Security tab on my
    Windows (2000) computer.Probably because "Use simply file sharing" is checked in
    some tab (can't find it because I don't have access on this
    computer). I think it is under "Tools->Folder Options->View->
    Use Simple File Sharing". (under "Tools" in explorer).

  • I got new hard driver for my MacBook I don't have the cd but I do have flash drive that has the software I need help because when I turn on my laptop it shows me a file with question mark how can I install the software from the flash driver?

    I got new hard driver for my MacBook I don't have the cd but I do have flash drive that has the software I need help because when I turn on my laptop it shows me a file with question mark how can I install the software from the flash driver?

    Hold down the Option key while you boot your Mac. Then, it should show you a selection of devices. Click your flash drive and it will boot from that.

  • How to convert the javasource file(*.class) to execute file(*.exe)?

    How to convert the javasource file(*.class) to execute file(*.exe)?
    thank you!

    Although i have seen a few programs (that are platform specific) that will embed a small jvm into an exe with your class file, it is generally excepted that you cannot create an executable file using java. The JAR executable file is probably the closest your going to get
    Pete

  • Convert file.class in file.java

    i lost my file.java how to convert my file.class in file.java? help me please!!!!!!!!!!

    search for java decompiler. just know that it won't be anywhere as near easy to read as the original.
    -S-

  • Problem with file.listFiles() of File class??

    Hi all friends,
    Iam facing with one peculiar problem,Iam using [file.listfiles()] method of File class in my program and this method of file class introduced in Java2.Now when Iam runing on Mac OS classic(8 to 9)[it is my client requirement they can't change Mac OS classic to Mac OS X] it is giving me error file.listfiles() not found in java.io package coz in Mac classic I have used MRJ2.2.6(for runtime) and MRJ SDK2.2 which is based on jdk1.1.8 and this method[listfiles()] added in java2.But now for me I can't change this method in program coz it is a big code and I have to change lot of things in my program.Can any one plz tell me can i find any (java.io) package of java2 seperately in the from of .jar or .zip so that i can keep it in my MRJCLASSES folder and from there I can import it in my program.Eagerly waiting for reply.
    Regards
    Bikash

    Just use File.list(). There are minor semantical differences but they are easy to work around.

  • How to declare ftp in File class. I want to display filename of file in FTP

    How to declare ftp in File class. I want to display filename of file in FTP server.
    I try
    File f2= new File("ftp://abc:abc1111@ABC/file");
                   String n[] = f2.list();
                   System.out.print(n[0]);
                   Vector filename = null ;
                   for (int i2 = 0; i2 < n.length; i2++) {
                        filename.add(n[i2]);
    but it is not work
    Thank you.

    You can use Jakarta's net-package to connect to a FTP server.
    Download it here:
    http://jakarta.apache.org/site/downloads/downloads_commons-net.cgi
    Here's a small demo:
    import org.apache.commons.net.ftp.*;
    public class FTPTest {
        private boolean isConnected;
        private FTPClient client;
        public FTPTest() {
            isConnected = false;
            connect();
        private void connect() {
            String server = "server.org";
            String username = "user";
            String password = "password";
            client = new FTPClient();
            try {
                client.connect(server);
                client.login(username, password);
                String[] files = client.listNames();
                for(int i = 0; i < files.length; i++) {
                    System.out.println(files);
    isConnected = true;
    disconnect();
    catch(Exception e) {
    e.printStackTrace();
    private void disconnect() {
    if(isConnected) {
    try {
    client.disconnect();
    isConnected = false;
    catch(Exception e) {
    e.printStackTrace();
    public static void main(String[] args) {
    new FTPTest();

  • The "File" class does not accept my paths

    Hi ,
    I need yo creat an instance of a directory/file using the File class , but it seems to not recognize my paths:
    using :
    File f = new File("c:/Dir1/dir2/file.ext");
    or:
    File f = new File("c:/Dir1/dir2");
    allthow all do exist , when I use the f.exists() it returns false !!!!
    what is missing ?
    thanks , Adaya .

    Hi
    Found my problem - it wasn't the backslahes (one can use both ways '\\' or'/') it was a realy stupid problem..
    thanks any way ,
    Adaya .

  • Java.io.File class file

    hi may i know how can i get this java.io.File class file, where can i download it from to use it.

    Nothing..... just a visit from the Deprecation Fairy..... she was a little plastered and removed
    java.io.File by mistake .... :)

  • HT1752 My MacBook on startup shows file with question mark ,, what now?

    My Mac shows file with question mark, what now.

    Press the power button down to force a emergency use only hardware shutdown.
    Press and hold the Option/alt key on the built in keyboard, boot the machine.
    If MacintoshHD appears, select it and click the arrow, then in System Preferences > Startup Disk reset that. Done.
    Sometimes a NVRAM reset is required then the above done again.
    Folder with question mark issue
    ..Step by Step to fix your Mac
    If MacintoshHD doesn't appear, or if it boots to gray screen or other issues, then it's more complicated of a fix, but once inside OS X then reset the Startup Disk.
    Gray, Blue or White screen at boot, w/spinner/progress bar
    ..Step by Step to fix your Mac
    You might need this if you don't have a recent backup and your problem is software related, Disk Utiltiy can't fix the drive and recommends you backup, erase and install.
    .Create a data recovery/undelete external boot drive
    If Disk Utility + Hardware Test shows no boot drive, then you have a dead drive or cable, or Mac issue.
    My computer is not working, is my personal data lost?

  • 'Basic' external actionscript file/class usage

    Hey All,
    I'm a bit baffled by something that should, in my humble opinion, be a piece of cake...
    The setup is really easy. A simple application with a single textinput, a button and a text component.
    When the button is clicked a function is called that sets the text from the inputfield to the text component.
    code:
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
        <mx:Script>
            <![CDATA[
                public function click(inputText:String):void {
                    outPutTxt.text = inputText;
            ]]>
        </mx:Script>
        <mx:TextInput x="10" y="10" id="inputTxt"/>
        <mx:Button x="178" y="10" label="Button" id="Btn" click="{click(inputTxt.text)}"/>
        <mx:Text x="251" y="12" text="Text" id="outPutTxt"/>
    </mx:Application>
    As I said, really easy.
    What I want to do is put the function 'click' into an external actionscript file or class. And still be able to, from within the external file, modify the outPutTxt stuff.
    I'm aware that I could create a public bindable variable inside of the external file and then bind the outPutTxt to that variable, but I would really like to be able to 'simply' force the value...
    Is that possible? If yes: how plix
    If it's not possible, I'd like to know as well! And in that case, how would I then re-use the same instance of a class/file in multiple seperate components? Say I got a 'login' thingie where a user logs in (data stored externally in a seperate actionscript file/class) and I want to re-use that information on other pages... Or is this done automatically?
    Any and all comments/answers are higly appreciated!
    Pieter

    Hey Levancho,
    thanks for your answer!! Much appreciated.
    However, the code you proposed threw an error...
    Errormessage:
    ReferenceError: Error #1069: Property outPutTxt not found on controls.Control and there is no default value.
        at controls::Control/btnClick()[C:\...\FlexTest_03\src\controls\Control.as:13]
        at FlexTest_03/__Btn_click()[C:\...\FlexTest_03\src\FlexTest_03.mxml:11]
    So I tried something else where I bind the text in my OutPutTxt to a variable inside of the external class file.
    Code:
    Class:
    package controls
        public class Control
            [Bindable]
            public var outPutText:String = "";
            public function Control()
            public function btnClick(inputText:String):void {
                this.outPutText =  inputText;
    Mxml:
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
        <mx:Script>
            <![CDATA[
                import controls.Control;
                [Bindable]
                private var control:Control = new Control();
            ]]>
        </mx:Script>
        <mx:TextInput x="10" y="10" id="inputTxt"/>
        <mx:Button x="178" y="10" label="Button" id="Btn" click="{control.btnClick(inputTxt.text)}"/>
        <mx:Text x="251" y="12" id="outPutTxt" text="{control.outPutText}"/>
    </mx:Application>
    And this basically did what I wanted to do... so I just need to do some major binding between my application and the classes I'll be using for my business logic.
    Now for something else entirely...
    Is there a way for me to pass the instance of my class between flex components?
    Pieter

  • Problemes with the method listFiles() in File class. I'm using JDeveloper 3.0

    Hi!
    Why can't I compile this snip of code?
    I get:
    Error: (19) method listFiles() not found in class java.io.File.
    Here is the code:
    package stream;
    import java.io.*;
    import java.util.*;
    public class myStream {
    public static void main(String[] args) {
    File myFile = new File("D:/myjava");
    File[] str = myFile.listFiles();
    Thanks in advance
    Henning

    There is no method "listFiles()" in "File" class. (JDK 1.1.8)
    You can try to use " list() " method which
    Returns a list of the files in the directory specified by this File object.
    raghu
    null

  • How to print the image data stored in object of 'java.io.File' Class in jsp

    I have created a file object for the image file in the system temporary directory.Now I want to display it in my page. please tell,How to print the image data stored in object(in my program it is imgr) of 'java.io.File' Class in jsp

    Create a servlet which gets an InputStream of the file and writes it to the OutputStream of the response and call this servlet in `src` attribute. That's basically all.
    Here's an example which covers the most of the minimum needs: [http://balusc.blogspot.com/2007/04/imageservlet.html].

Maybe you are looking for

  • 1 : n interface

    HI I have an interface which gets a file from a sending system and XI has to send  it to 10 different receiver. I have gone through couple of weblogs and forum threads but I dint get the clear picture for the development approach. Qs 1: Is it possibl

  • CFLOGIN not maintaining cfauthrization session variable

    I have a simple CF web site where all of the .cfm is in the same directory. I can not use client cookies as the people I am writing this for have cookies turned off in IE, therefore I am using J2EE session variables and CFLOGIN in an application.cfm

  • How can i play Videos as part of a playlist, along with songs?

    Hi I've had an ongoing problem for some time now, and i cant really find a method, or an option, to make it work. Basically, i have Videos (Films), that are music, and i've put them into the same playlist as some of my other similar songs, however th

  • Can't open .wmf files with Illustrator

    I'm using Creative Suite CS2 on a PC/Windows XP operating system. I'm suddenly unable to launch Illustrator when I try to open a .wmf file from within Windows Explorer. Instead, I get an error message that the file itself is not a valid program (DUH!

  • How to make PC database like VB6+Access with java?

    I mean the application has its own database and can be installed everywhere without SQL server. How can i do it?