View Poll Results: 1

Voters
0. You may not vote on this poll
  • 1

    0 0%
  • 1

    0 0%
  • 1

    0 0%
  • 1

    0 0%
  • 1

    0 0%
  • 1

    0 0%
  • 1

    0 0%
  • 1

    0 0%
Results 1 to 6 of 6
  1. #1
    Synetrix is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Recursively find text in text files

    Java Code:
       if(Directory.getText().matches("")){
                           JOptionPane.showMessageDialog(null, "Please search for the directory");
                   		}
                       else if(fileName.getText().matches("")){
                           JOptionPane.showMessageDialog(null, "Please input the correct file name");
                   		}		
               	    
                       else if(txtKeyword.getText().matches("")){
                   		JOptionPane.showMessageDialog(null, "Please input the correct data");
                   		}    	             	
               	try { 		
            	    String hour = (String) comboBox.getSelectedItem();
               		String filename =  fileName.getText();
                    String date = ((JTextField)dateChooser.getDateEditor().getUiComponent()).getText();         
                    String text = txtKeyword.getText();                       	  
              	    String newline = "\n";
                 	String directory = Directory.getText();
                    File path = new File(directory);   
                    File[] listOfFiles = path.listFiles();         
                   
                    for (int i = 0; i < listOfFiles.length; i++) {
                    	 System.out.println("hello");
                        if (listOfFiles[i].isFile()) {
                        	
                        	if(listOfFiles[i].getName().contains(filename + "-" + date + "[" + hour + "]") == true) {  		          		
                        		String line = null;
                        		Reader reader = new InputStreamReader(new FileInputStream(filename), "utf-8");           
                                BufferedReader br = new BufferedReader(reader);                   
                        		 while ((line = br.readLine()) != null) {           	           	    	                   
                                if(line.contains(text)){                 
                                    jTextArea1.append(line + newline);    
                                    btnClear.setEnabled(true); 
                                  	 btnExport.setEnabled(true); 
                                }                                      
                                } 
                        		br.close();
                            }
                        }
                        else if (listOfFiles[i].isFile()) {
                                if(listOfFiles[i].getName().contains(filename) == true) {
                        		String line = null;
                        		Reader reader = new InputStreamReader(new FileInputStream(filename), "utf-8");           
                                BufferedReader br = new BufferedReader(reader);                   
                        		 while ((line = br.readLine()) != null) {           	           	    	                   
                                if(line.contains(text)){                 
                                    jTextArea1.append(line + newline);    
                                    btnClear.setEnabled(true); 
                                  	 btnExport.setEnabled(true);
                                  	i++;
                                }                                      
                                } 
                        			br.close();
                            }
                        }
                    }
                   }	
              	catch(Exception e){    	       	  
                }




    This is actually my question:

    Java Code:
    1. billing-20140527[09].txt has 
    
     a)XGMS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.
    
     b)XGMS,2034-05-27 30:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.
    
    
    2. billing-20140527[10].txt has
    
     a)XCGS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.
    
     b)HELO
    
    
    [B]I try to find the number 1 in both text files, if lets say I input the text file name is
            billing, I can find the number 1 in both text file and output them:[/B]
    
    a) XGMS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.
    
    b) XCGS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.
    
    
    
    [B]However, if I specify the text file name: billing-20140527[09].txt and find the number 1 inside the text file, it will only output:[/B]
    
    a) XGMS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.
    So far, I can't get this program to run, I don't know what's wrong with my code. Could anybody explain to me what is the error?

  2. #2
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Recursively find text in text files

    "I can't get this program to run"

    What happens when you try to run it? What's the error message? Does it run and not work? Or simply not compile?

  3. #3
    Synetrix is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Re: Recursively find text in text files

    I mean it can run but can't be compiled. No error messages, I don't get what is wrong with the looping....

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Recursively find text in text files

    @OP: why is this thread a poll and what is this poll about? If you did this accidentally, remove the poll part please.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Recursively find text in text files

    Quote Originally Posted by Synetrix View Post
    I mean it can run but can't be compiled. No error messages, I don't get what is wrong with the looping....
    Sorry , that makes no sense.
    If it can't be compiled then there's no way it can run.
    And if it can't be compiled then that means there's some compilation error messages.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    mousumiy is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: Recursively find text in text files

    Its hard to understand your code as its not complete I am not able to compile it.
    BufferReader should be passed in the resursive function in the call stack. You have not implemented this recurssively.

    Also the If and the Else if are just the same , not sure what you are trying to accomplish...

    Also what does i++ in your else if mean? Recurssive method will not have the file name check , the check will be outside the method which will call the recurssive method...

    Try this code, ,
    Right now the code below just gets all the text files in the current directory and and subdirectories in the directory. For each subdirectory found, it also finds any text files and deeper subdirectories. The problem is that if those deeper subdirectories have yet deeper subdirectories then I am not finding all the text files ....

    File rootDirectory = new File(rootDir);
    if (rootDirectory.isDirectory()) {
    System.out.println("Valid directory");

    File[] listOfFiles = rootDirectory.listFiles();
    for (int i = 0; i < listOfFiles.length; i++) {
    String iName = listOfFiles[i].getName();
    if (listOfFiles[i].isFile()) {
    if (iName.endsWith(".txt") || iName.endsWith(".TXT")) {
    System.out.println("File: "+iName);
    }
    }
    if (listOfFiles[i].isDirectory()) {
    System.out.println("Directory: "+iName);

    File[] subList = listOfFiles[i].listFiles();
    for (int j = 0; j < subList.length; j++) {
    String jName = subList[j].getName();
    if (subList[j].isFile()) {
    if (jName.endsWith(".txt") || jName.endsWith(".TXT")) {
    System.out.println("\tFile: "+jName);
    }
    }
    if (subList[j].isDirectory()) {
    System.out.println("\tDirectory: "+jName);
    }
    }
    }
    }
    }
    else System.out.println("Invalid directory");
    Last edited by mousumiy; 05-29-2014 at 10:43 PM.

Similar Threads

  1. Replies: 40
    Last Post: 05-25-2014, 07:38 PM
  2. Replies: 0
    Last Post: 07-24-2012, 08:07 AM
  3. Searching through folder to find text inside all files
    By dazzabiggs in forum New To Java
    Replies: 3
    Last Post: 05-03-2011, 01:20 PM
  4. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  5. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •