• Post Reply Bookmark Topic Watch Topic
  • New Topic

java to scan folder  RSS feed

 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to write a java code that will
1. go a path (eg: C:/dev/myxmls)
2. pick one by one all files (all are having *.xml extension) from 'myxmls' folder
3. print that file name on console (System.out.printly(filename))
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiya Khambadkone wrote:I want to write a java code that will
1. go a path (eg: C:/dev/myxmls)
2. pick one by one all files (all are having *.xml extension) from 'myxmls' folder
3. print that file name on console (System.out.printly(filename))


We would much rather see you post your own code, working or not, and give you tips to help you get it working or point you toward resources that will help you to learn how to solve your problem using best practices.


show us the code you have so far so that we can advise on it.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiya Khambadkone wrote:I want to write a java code that will
1. go a path (eg: C:/dev/myxmls)
2. pick one by one all files (all are having *.xml extension) from 'myxmls' folder
3. print that file name on console (System.out.printly(filename))

And the question is...?

You seem to have a pretty good idea of what steps are necessary to accomplish your task. Implement them exactly in the order you've listed them, and if you encounter a problem then post an actual question and your code. That way, you might get answer.

Edit: No such thing as System.out.printly in Java.
 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public static void main(String[] args) throws IOException {
String file ="C:/dev/myXMLs";
File[] f = finder(file);

while(f!=null){
System.out.println(f[0].getName());
BufferedWriter out = new BufferedWriter(new FileWriter("C:/dev/abcd.txt"));
out.write(f[0].getName());
out.close();
}
}
public static File[] finder( String dirName){
File dir = new File(dirName);
return dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String filename){
return filename.endsWith(".xml"); }
} );
}

This goes into infinite loop.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please put your code inside code tags.

Your f array contains files (supposing your method works well). So, basically, it's never null so your while loop never ends.
Why not try different approach? Since you're working with array, for loop seems like a better solution.
 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got fixed using for loop . Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!