13 Replies Latest reply: May 17, 2007 4:14 AM by 800282 RSS

    reading files in a directory...

    807606
      Hi techies,,
      I am trying to read all the text files in a particular directory.I tried using the File class. but it didnt help..

      Can anyone provide any help....

      thanks in advance..
      Nadir
        • 1. Re: reading files in a directory...
          800282
          Hi techies,,
          I am trying to read all the text files in a
          particular directory.I tried using the File class.
          but it didnt help..

          Can anyone provide any help....
          Sure.
          Post the code that "didnt help" and I (or someone else) can probably see why it "didnt help".
          When posting code, please use code tags:
          http://forum.java.sun.com/help.jspa?sec=formatting
          Also post compiler/runtime errors exactly as they are printed.
          Thanks.
          • 2. Re: reading files in a directory...
            807606
            Hi just a snapshot of the codes
            if (directory.isDirectory() == false)
                           {
                                if (directory.exists() == false)
                                {
                                     System.out.println("There is no such directory!");
                                }
                                     else
                                     {
                                          System.out.println("That file is not a directory.");
                                     }
                           }
                           else
                           {
                                files = directory.list();
                                System.out.println("Files in directory \"" + directory + "\":");
                                     for (int i = 0; i < files.length; i++)
                                     {
                                          //do something
                                          System.out.println(" " + files);
            
                                     }
                           }
                 
            iam also trying to use this..
            import java.io.*;
            import java.util.*;
            
            public class Poland
            {
                 public static void main(String args[])
                 {
                  try {
                         String path = "c:\\nadir\\.txt";
                         BufferedReader in = new BufferedReader(new FileReader(path));
                         String str;
                         while ((str = in.readLine()) != null) {
                             System.out.println(str);
                        }
            
                           //File file = new File("16052007-963.txt");
            
                               // Get the number of bytes in the file
                           //    long length = file.length();
                           //    System.out.println(length);
            
                           if(in.readLine().equals("963"))
                           {
                                System.out.println("try");
                           }
                           else
                           {
                                          System.out.println("Not found");
                           }
            
            
                         in.close();
                     } catch (IOException e) {
                           System.out.println(e);
                     }
            i am bit confused which one will be suitable... please advice
            • 3. Re: reading files in a directory...
              800282
              Hi just a snapshot of the codes

              ...
              And what is the problem?
              • 4. Re: reading files in a directory...
                807606
                i want to read all the files in a directory..
                i exactly dont know how to go about it...
                i am using this way..
                String path = "c:\\nadir\\.txt";
                             BufferedReader in = new BufferedReader(new FileReader(path));
                .
                to read the complete directory...
                is this right..
                can u provide some suggestions
                • 5. Re: reading files in a directory...
                  800282
                  i want to read all the files in a directory..
                  i exactly dont know how to go about it...
                  i am using this way..
                  ...
                  No. First read specific files from a directory, then process those files.
                  Here's how you read .txt files from a specific directory:
                  import java.io.File;
                  import java.util.ArrayList;
                  import java.util.List;
                  
                  public class Main {
                      
                      private static List<File> readDirectory(File directory, String extension) {
                          List<File> found = new ArrayList<File>();
                          if(!directory.isDirectory()) {
                              throw new IllegalArgumentException("Invalid directory: "+directory);
                          }
                          File[] contents = directory.listFiles();
                          for(File file : contents) {
                              if(file.getName().endsWith(extension)) {
                                  found.add(file);
                              }
                          }
                          return found;
                      }
                      
                      public static void main(String[] args) {
                          // Get all txt files from C:\Temp
                          List<File> files = readDirectory(new File("C:\\Temp"), ".txt");
                          System.out.println("Found: "+files);
                          
                          // your code here to process the files
                      }
                  }
                  • 6. Re: reading files in a directory...
                    807606
                    i will try this code..
                    but with what will i replace the <FILE> tag...
                    • 7. Re: reading files in a directory...
                      807606
                      thanks
                      it worked..
                      it displays me the file types..
                      but how do i read all the files....
                      • 8. Re: reading files in a directory...
                        800282
                        thanks
                        it worked..
                        You're welcome.

                        it displays me the file types..
                        but how do i read all the files....
                        How about you try that yourself, and ask a specific question here when you get stuck, OK?

                        http://www.google.com/search?hl=en&q=reading+files+java&btnG=Google+Search
                        • 9. Re: reading files in a directory...
                          807606
                          An alternative that I use is the FilenameFilter class:

                          import java.io.File;
                          import java.io.FilenameFilter;

                          public class Main {

                          public static class TXTFilenameFilter implements FilenameFilter {
                          public boolean accept( File file, String name ) {
                          return name.endsWith( "txt" );
                          }
                          }

                          public static void main( String[] args ) {
                          File file = new File( "c:\\somedir\\" );
                          String[] list = file.list( new TXTFilenameFilter() );
                          }

                          }
                          • 10. Re: reading files in a directory...
                            807606
                            i am trying it myself...
                            just wanted to know some alternatives...
                            thanks for your help once again..

                            i greatly appreciate your help..
                            Many Thanks.
                            Nadir Firfire
                            • 11. Re: reading files in a directory...
                              800282
                              An alternative that I use is the FilenameFilter
                              class:

                              ...
                              Good call remcollier!
                              Next time when posting code, use code tags*: it makes it so much easier to read.
                              ; )

                              * http://forum.java.sun.com/help.jspa?sec=formatting
                              • 12. Re: reading files in a directory...
                                807606
                                no problems!

                                thanks for the heads up :o)
                                • 13. Re: reading files in a directory...
                                  800282
                                  i am trying it myself...
                                  just wanted to know some alternatives...
                                  thanks for your help once again..

                                  i greatly appreciate your help..
                                  Many Thanks.
                                  Nadir Firfire
                                  No problem, you're most welcome.
                                  You can always post follow-up question here if you get stuck. Be sure to post the code you're working on and explain exactly what it is you don't understand.