Results 1 to 10 of 10
  1. #1
    mridulbirla is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default problem in searching

    import org.apache.commons.io.FileUtils;

    import java.io.File;
    import java.util.Collection;
    import java.util.Iterator;

    import java.awt.*;
    import javax.swing.*;


    public class JAppletExample extends JApplet {
    public void init() {

    Container content = getContentPane();
    content.setBackground(Color.white);
    content.setLayout(new FlowLayout());
    content.add(new JButton("Button 1"));
    content.add(new JButton("Button 2"));

    File root = new File("c:/fl");

    try {
    String[] extensions = {"c"};
    String yahya=new String();
    boolean recursive = true;

    //
    // Finds files within a root directory and optionally its
    // subdirectories which match an array of extensions. When the
    // extensions is null all files will be returned.
    //
    // This method will returns matched file as java.io.File
    //
    Collection files = FileUtils.listFiles(root, extensions, recursive);

    for (Iterator iterator = files.iterator(); iterator.hasNext();) {
    File file = (File) iterator.next();
    content.add(new JButton("Button 3"));
    content.add(new JButton(file.getAbsolutePath()));
    }
    } catch (Exception e) {
    e.printStackTrace();
    }

    }
    }


    The button 3 is not getting made ie for loop is not getting executed.
    If anyone can help, please help.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    This:
    Java Code:
    Collection files = FileUtils.listFiles(root, extensions, recursive);
    is presumably returning an empty collection.

    And since FileUtils is not a JDK class I can't say why.

  4. #4
    mridulbirla is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Can you please suggest the solution.

    My main aim is to search user directory(ie C:\) for for files with particular extension using a japplet which i then embed it in the html code.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    i just tested your code but instead of a for i used a while loop

    Java Code:
    Iterator iterator = files.iterator();
    
    while (iterator.hasNext()) {
    	System.out.println(iterator.next());
    	content.add(new JButton("Button 3"));
    }

    the result is: if no file is found with the extension c no button 3 is added and shown and if there is one or more files with the extension c one or more buttons 3 are shown.

  6. #6
    mridulbirla is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    but still it is not working.
    I tried to debug it.
    code:

    import org.apache.commons.io.FileUtils;

    import java.io.File;
    import java.util.Collection;
    import java.util.Iterator;

    import java.awt.*;
    import javax.swing.*;

    public class JAppletExample extends JApplet {
    public void init() {

    Container content = getContentPane();
    content.setBackground(Color.white);
    content.setLayout(new FlowLayout());
    content.add(new JButton("Button 1"));
    content.add(new JButton("Button 2"));

    File root = new File("c:/fl");

    try {
    String[] extensions = {"c"};
    String yahya=new String();
    boolean recursive = true;
    content.add(new JButton("Button 4"));
    Collection files = FileUtils.listFiles(root, extensions, recursive);
    content.add(new JButton("Button 5"));
    Iterator iterator = files.iterator();

    while (iterator.hasNext()) {
    System.out.println(iterator.next());
    content.add(new JButton("Button 3"));
    }

    // for (Iterator iterator = files.iterator(); iterator.hasNext();) {
    // File file = (File) iterator.next();
    // System.out.println("HELLO");
    // System.out.println("File = " + file.getAbsolutePath());
    // content.add(new JButton("Button 3"));
    // yahya=file.getName();
    // content.add(new JButton(yahya));

    // }
    // content.add(new JButton("Button 5"));

    } catch (Exception e) {


    }

    }
    }


    in this button1,2,4 are only getting printed.
    @tolls
    if i make this as simple java application without applet. It is working fine

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    Log the exception.
    You are eating exceptions in your catch block and I'd lay good odds on there being an exception, so printStackTrace().

  8. #8
    mridulbirla is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    @toll it is not going in catch block .I tried to add a button in catch block but it is not getting printed.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    My main aim is to search user directory(ie C:\) for for files with particular extension using a japplet
    Applets are restricted from accessing the local filesystem, unless signed/trusted.

    db

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    Quote Originally Posted by mridulbirla View Post
    @toll it is not going in catch block .I tried to add a button in catch block but it is not getting printed.
    Add a printStackTrace().
    As I suggested.
    Adding a button is meaningless.
    Never eat Exceptions.

    ETA: Then you'd probably get the exception that Darryl is talking about. And save yourself (and us) time trying to figure out what's going on.

Similar Threads

  1. Searching
    By Harmesh Goyal in forum AWT / Swing
    Replies: 0
    Last Post: 03-02-2011, 02:36 PM
  2. Searching Problem
    By kostas885 in forum Lucene
    Replies: 0
    Last Post: 12-08-2010, 10:19 PM
  3. Replies: 4
    Last Post: 07-27-2010, 08:52 PM
  4. Searching In a String Array - Problem
    By DillMan in forum New To Java
    Replies: 4
    Last Post: 12-07-2008, 09:12 PM
  5. Problem with searching while using GUI
    By BHCluster in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2008, 06:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •