Results 1 to 10 of 10
Thread: problem in searching
- 04-01-2011, 11:19 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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.
- 04-01-2011, 11:41 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Where is the button 3?
Ans please use code tags when you are posting code segments next time.
- 04-01-2011, 02:31 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
This:
is presumably returning an empty collection.Java Code:Collection files = FileUtils.listFiles(root, extensions, recursive);
And since FileUtils is not a JDK class I can't say why.
- 04-03-2011, 05:56 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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.
- 04-03-2011, 09:53 AM #5
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.
- 04-03-2011, 08:48 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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
- 04-04-2011, 10:39 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Log the exception.
You are eating exceptions in your catch block and I'd lay good odds on there being an exception, so printStackTrace().
- 04-05-2011, 11:15 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
@toll it is not going in catch block .I tried to add a button in catch block but it is not getting printed.
- 04-05-2011, 11:19 AM #9
Applets are restricted from accessing the local filesystem, unless signed/trusted.My main aim is to search user directory(ie C:\) for for files with particular extension using a japplet
db
- 04-05-2011, 11:22 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
Searching
By Harmesh Goyal in forum AWT / SwingReplies: 0Last Post: 03-02-2011, 02:36 PM -
Searching Problem
By kostas885 in forum LuceneReplies: 0Last Post: 12-08-2010, 10:19 PM -
problem searching for a String element in a paralell array
By david185000 in forum New To JavaReplies: 4Last Post: 07-27-2010, 08:52 PM -
Searching In a String Array - Problem
By DillMan in forum New To JavaReplies: 4Last Post: 12-07-2008, 09:12 PM -
Problem with searching while using GUI
By BHCluster in forum AWT / SwingReplies: 1Last Post: 04-24-2008, 06:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks