Page 1 of 20 12311 ... LastLast
Results 1 to 20 of 395
  1. #1
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default [SOLVED] Read From LOGFILE

    Hi All

    I have code which is working .But my problem is i want to Show from file one string only one time.

    Example of logfile:-

    123 kjhkjhlk
    123 jhijio
    234 fdfgd
    989 nbnbnb
    676 njnkj


    Now i want ot show like this:-
    123
    234
    989
    676

    So plz help me...

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Here are some examples,

    Java Code:
    void ReadFile(File f) throws Exception {
          Scanner scan=new Scanner(f);
          while(scan.hasNext()){
                System.out.println(scan.next());
                scan.next();
          }scan.close();
    }
    or

    Java Code:
    void ReadFile(File f) throws Exception {
          Scanner scan=new Scanner(f);
          while(scan.hasNextLine()){
                System.out.println(scan.nextLine().split(" ")[0]);        
          }scan.close();
    }
    I haven't test it yet, try it....
    freedom exists in the world of ideas

  3. #3
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default Read From LOGFILE

    i use both code in my programm.But out put is,It is printing may time.As i say i want to print only one time..

    Plz help me

  4. #4
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Plz help me any one

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Can you get all numbers, with duplicates.

    which type of number...my log file number like is

    123
    122
    1233
    145
    165
    789
    122
    122
    123
    122

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, now what you can do is, store them in a list and add one by one. Before adding the element check it's exist or not.

  8. #8
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    can you give me code of store in list and add one by one

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, here is a way to do it.

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    /**
     *
     * @author Eranga Tennakoon
     */
    public class FileRead {
        
        private ArrayList<String> str;
        
        public FileRead() {
            this.str = new ArrayList<String>();
        }
        
        private void getFileInfor(File myFile) {
            try {
                Scanner fileScanner = new Scanner(myFile);
                while(fileScanner.hasNextLine()) {
                    doProcessing(fileScanner.nextLine());
                }
            } 
            catch (FileNotFoundException ex) {
                System.out.println(ex.getMessage());
            }
            printResult();
        }
        
        private void doProcessing(String strr) {
            String newStr = strr.split(" ")[0];
            removeDuplicates(newStr);
        }
        
        private void removeDuplicates(String s) {
            
            if(str.isEmpty()) {
                str.add(s);
            }
            else {
                for(int i = 0; i < str.size(); i++) {
                    if(!str.contains(s)) {
                        str.add(s);
                    }
                }
            }
        }
        
        private void printResult() {
            for(int i = 0; i < str.size(); i++) {
                System.out.println(str.get(i));
            }
        }
        
        public static void main(String[] args) {
            File dd = new File("files/mir_001.txt");
            new FileRead().getFileInfor(dd);
        }
        
    }

  11. #11
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    yaa i got it.it's good if it working..

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  13. #13
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    it is not working accourding to you and me.It is simply print all string which is present in log file..

  14. #14
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    Thxs for code but it's out put like this
    Java Code:
    192.158.5.9	
    192.158.5.9	
    192.158.5.9
    192.158.5.9	
    192.158.5.9	
    192.158.5.9	
    192.158.5.9	
    192.158.5.9	
    192.158.5.9	
    192.158.5.9	
    192.158.5.9
    And i want to show only one time
    Java Code:
    192.158.5.9
    i mean all duplicate 192.158.5.9 don't want to show.

    plz help me

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  17. #17
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    plz see me log formate then try to run

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Are you talking about this.

    Java Code:
    192.158.5.9	
    192.158.5.9	
    192.158.5.9
    192.158.5.9	
    192.158.5.9	
    192.158.5.9	
    192.158.5.9	
    192.158.5.9	
    192.158.5.9	
    192.158.5.9	
    192.158.5.9

  19. #19
    Mir
    Mir is offline Senior Member
    Join Date
    Mar 2008
    Posts
    447
    Rep Power
    7

    Default

    yes

    Java Code:
    192.158.5.9	kjnkjnkn
    192.158.5.9	kasasdas
    192.158.5.9          asdsad
    192.158.5.9	sadasdsad
    192.158.5.9	sadsad
    192.158.5.9	rtertre
    192.158.5.9	rtret
    192.158.5.9	cbvv
    192.158.5.9	cvbnghh
    192.158.5.9	fghfgn
    192.158.5.9          tyty

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yep, you get all of them as output. Reason is this.

    The way of formated you log file and the way it handle on the code is not compatible. On a single line, number split depend on a single character space.

    Java Code:
    String newStr = strr.split(" ")[0];
    But in your log file there is no space. I can see tab, in some lines more than one tab. So what happened is all are counted as not duplicated on each.

    So, you get the output as

    Java Code:
    192.158.5.9	kjnkjnkn
    192.158.5.9	kasasdas
    192.158.5.9          asdsad
    192.158.5.9	sadasdsad
    192.158.5.9	sadsad
    192.158.5.9	rtertre
    192.158.5.9	rtret
    192.158.5.9	cbvv
    192.158.5.9	cvbnghh
    192.158.5.9	fghfgn
    192.158.5.9          tyty
    Because no one is similar to each other.

Page 1 of 20 12311 ... LastLast

Similar Threads

  1. Must Read.....
    By sanjeevtarar in forum Forum Lobby
    Replies: 10
    Last Post: 03-03-2010, 07:16 PM
  2. how to read a form with JFS
    By crispy in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 05-01-2008, 03:07 PM
  3. Read file
    By tajinvillage in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-29-2008, 09:10 AM
  4. How to read the following
    By rrp in forum New To Java
    Replies: 0
    Last Post: 12-03-2007, 06:16 PM
  5. read txt file
    By sureshsri1981 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-05-2007, 03:49 PM

Posting Permissions

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