Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-24-2008, 03:28 PM
Mir Mir is offline
Senior Member
 
Join Date: Mar 2008
Posts: 447
Mir is on a distinguished road
[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...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-24-2008, 03:44 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Here are some examples,

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

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....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-24-2008, 04:23 PM
Mir Mir is offline
Senior Member
 
Join Date: Mar 2008
Posts: 447
Mir is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-26-2008, 08:42 AM
Mir Mir is offline
Senior Member
 
Join Date: Mar 2008
Posts: 447
Mir is on a distinguished road
Plz help me any one
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-26-2008, 08:47 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Can you get all numbers, with duplicates.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-26-2008, 09:02 AM
Mir Mir is offline
Senior Member
 
Join Date: Mar 2008
Posts: 447
Mir is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-26-2008, 09:24 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-26-2008, 09:27 AM
Mir Mir is offline
Senior Member
 
Join Date: Mar 2008
Posts: 447
Mir is on a distinguished road
can you give me code of store in list and add one by one
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-26-2008, 09:30 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Got you what I say there?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-26-2008, 09:31 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ok, here is a way to do it.

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); } }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-26-2008, 09:32 AM
Mir Mir is offline
Senior Member
 
Join Date: Mar 2008
Posts: 447
Mir is on a distinguished road
yaa i got it.it's good if it working..
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 05-26-2008, 09:34 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Try an let me know is it work.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 05-26-2008, 09:50 AM
Mir Mir is offline
Senior Member
 
Join Date: Mar 2008
Posts: 447
Mir is on a distinguished road
it is not working accourding to you and me.It is simply print all string which is present in log file..
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 05-26-2008, 09:52 AM
Mir Mir is offline
Senior Member
 
Join Date: Mar 2008
Posts: 447
Mir is on a distinguished road
Thxs for code but it's out put like this
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
Code:
192.158.5.9
i mean all duplicate 192.158.5.9 don't want to show.

plz help me
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 05-26-2008, 09:56 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
No you are wrong, it's perfectly work for me. I don't know what happened in your end.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 05-26-2008, 09:57 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
removeDuplicates() done it
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 05-26-2008, 10:00 AM
Mir Mir is offline
Senior Member
 
Join Date: Mar 2008
Posts: 447
Mir is on a distinguished road
plz see me log formate then try to run
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 05-26-2008, 10:01 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Are you talking about this.

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
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 05-26-2008, 10:07 AM
Mir Mir is offline
Senior Member
 
Join Date: Mar 2008
Posts: 447
Mir is on a distinguished road
yes

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
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 05-26-2008, 10:17 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

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

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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
<