5 Replies Latest reply on Dec 12, 2010 10:40 PM by 823232

    [HELP] Search Files Function

    823232
      Hi,

      Could someone please help me fix this function below. It's suppost to search files in a dir for a particular string. I have files in the "./" dir but the function does not seem to output nothing at all.
      The search is also suppost to ignore the filez ending with .zip, .rar ect...
      public static void searchFiles(String s)
          {
              File file = new File("./");
              String as[] = file.list();
              long l = 0L;
                label0:
              for(int i = 0; i < as.length;)
              {
                  if(as.endsWith(".zip") || as[i].endsWith(".rar") || as[i].endsWith(".gz") || as[i].endsWith(".jar") || as[i].endsWith(".7z"))
      {
      continue;
      }
      try
      {
      long l1 = 0L;
      BufferedReader bufferedreader = new BufferedReader(new FileReader(as[i]));
      Object obj = null;
      do
      {
      String s1;
      do
      {
      if((s1 = bufferedreader.readLine()) == null)
      {
      continue label0;
      }
      l1++;
      } while(!s1.contains(s));
      l++;
      System.out.println((new StringBuilder()).append(" [").append(l).append("] ").append(as[i]).append(" (Line ").append(l1).append(") ").toString());
      System.out.println((new StringBuilder()).append(" ").append(s1).append(" ").toString());
      System.out.println("");
      } while(true);
      }
      catch(IOException ioexception)
      {
      System.out.println("Critical error while opening file for search!");
      ioexception.printStackTrace();
      i++;
      }
      }
      System.out.println("");
      System.out.println("");
      System.out.println((new StringBuilder()).append(" ").append(l).append(" results for ").append(s).append(".").toString());
      }
      Edited by: user13462345 on 12-Dec-2010 11:31                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        • 1. Re: [HELP] Search Files Function
          hsc71
          for(int i = 0; i < as.length;)
          This is an infinite loop, when as.length > 0
          If there is a file and this file doesn't require
          if(as.endsWith(".zip") || as[i].endsWith(".rar") || as[i].endsWith(".gz") || as[i].endsWith(".jar") || as[i].endsWith(".7z"))
          
          there will be no output.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
          • 2. Re: [HELP] Search Files Function
            823232
            Their's still no output.
            I created this long time ago but I lost the source so I had to decompile it. The two things that wasn't their before was the "continues" and "label0:" I cannot remember what was their before.
            • 3. Re: [HELP] Search Files Function
              camickr
              for(int i = 0; i < as.length;)
              i varialble is never incremented. Use the "for each" structure:
              for (String fileName : file.list())
              {
              ...
              }
              • 4. Re: [HELP] Search Files Function
                hsc71
                Never mind.

                Edited by: hsc71 on 12-dec-2010 23:28
                • 5. Re: [HELP] Search Files Function
                  823232
                  Never mind now I have fixed it...

                  Just had to replace this...
                  for(int i = 0; i < as.length;)
                  With this...
                  for(int i = 0; i < as.length; i++)
                  Edited by: user13462345 on 12-Dec-2010 14:40