|
|
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.
|
|

05-24-2008, 03:28 PM
|
|
Senior Member
|
|
Join Date: Mar 2008
Posts: 447
|
|
|
[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...
|
|

05-24-2008, 03:44 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
Here are some examples,
void ReadFile(File f) throws Exception {
Scanner scan=new Scanner(f);
while(scan.hasNext()){
System.out.println(scan.next());
scan.next();
}scan.close();
}
or
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.
|
|

05-24-2008, 04:23 PM
|
|
Senior Member
|
|
Join Date: Mar 2008
Posts: 447
|
|
|
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
|
|

05-26-2008, 08:42 AM
|
|
Senior Member
|
|
Join Date: Mar 2008
Posts: 447
|
|
|
Plz help me any one
|
|

05-26-2008, 08:47 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

05-26-2008, 09:02 AM
|
|
Senior Member
|
|
Join Date: Mar 2008
Posts: 447
|
|
Originally Posted by Eranga
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
|
|

05-26-2008, 09:24 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

05-26-2008, 09:27 AM
|
|
Senior Member
|
|
Join Date: Mar 2008
Posts: 447
|
|
|
can you give me code of store in list and add one by one
|
|

05-26-2008, 09:30 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

05-26-2008, 09:31 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
Ok, here is a way to do it.
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.
|
|

05-26-2008, 09:32 AM
|
|
Senior Member
|
|
Join Date: Mar 2008
Posts: 447
|
|
|
yaa i got it.it's good if it working..
|
|

05-26-2008, 09:34 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
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.
|
|

05-26-2008, 09:50 AM
|
|
Senior Member
|
|
Join Date: Mar 2008
Posts: 447
|
|
|
it is not working accourding to you and me.It is simply print all string which is present in log file..
|
|

05-26-2008, 09:52 AM
|
|
Senior Member
|
|
Join Date: Mar 2008
Posts: 447
|
|
Thxs for code but it's out put like this
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
i mean all duplicate 192.158.5.9 don't want to show.
plz help me
|
|

05-26-2008, 09:56 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

05-26-2008, 09:57 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

05-26-2008, 10:00 AM
|
|
Senior Member
|
|
Join Date: Mar 2008
Posts: 447
|
|
|
plz see me log formate then try to run
|
|

05-26-2008, 10:01 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
Are you talking about this.
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.
|
|

05-26-2008, 10:07 AM
|
|
Senior Member
|
|
Join Date: Mar 2008
Posts: 447
|
|
yes
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
|
|

05-26-2008, 10:17 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
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.
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
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
< | | | |