Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

java to scan folder

 
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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))
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got fixed using for loop . Thanks.
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic