Results 1 to 20 of 24
Thread: Parse part file
- 05-30-2011, 11:24 AM #1
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
Parse part file
Hi Guys, how could i parse a specific elements from a .part file using java.
BEGIN:VCARD
VERSION:3.0
N:Arun_niit;;
FN:Arun_niit
EMAIL;type=internet:nura_ice@yahoo.co.in
END:VCARD
BEGIN:VCARD
VERSION:3.0
N:Bống Mũn;Hải;
FN:Hải Anh Bống Mũn
END:VCARD
BEGIN:VCARD
VERSION:3.0
N:Colombo;Giorgia;
FN:Giorgia Colombo
EMAIL;type=internet:giorgiacolombo89@libero.it
END:VCARD
This is the content and I want to read "N: FN: Email:" and put in an array and show the output.
This is my code and i'm not familiarized with split, scan, useDelimiter in java. I need some assistance.
Thank you guys.
Java Code://This code just reads all the line without the : symbol. import java.io.*; import java.util.Scanner; public class ReadWithScanner { public static void main(String... aArgs) throws FileNotFoundException { ReadWithScanner parser = new ReadWithScanner("D:/Hfm.part"); parser.processLineByLine(); //log("Done."); } /** Constructor. @param aFileName full name of an existing, readable file. */ public ReadWithScanner(String aFileName){ fFile = new File(aFileName); } /** Template method that calls {@link #processLine(String)}. */ public final void processLineByLine() throws FileNotFoundException { //Note that FileReader is used, not File, since File is not Closeable Scanner scanner = new Scanner(new FileReader(fFile)); try { //first use a Scanner to get each line while ( scanner.hasNextLine() ){ processLine( scanner.nextLine() ); } } finally { //ensure the underlying stream is always closed //this only has any effect if the item passed to the Scanner //constructor implements Closeable (which it does in this case). scanner.close(); } } /** Overridable method for processing lines in different ways. BEGIN:VCARD VERSION:3.0 N:Arun_niit;; FN:Arun_niit EMAIL;type=internet:nura_ice@yahoo.co.in END:VCARD **/ protected void processLine(String aLine){ //use a second Scanner to parse the content of each line Scanner scanner = new Scanner(aLine); scanner.useDelimiter(":"); if ( scanner.hasNext() ){ String name = scanner.next(); String value = scanner.next(); log("Name is : " + quote(name.trim()) + ", and Value is : " + quote(value.trim()) ); //log("Value is : " + quote(value.trim())); } else { log("Empty or invalid line. Unable to process."); } //no need to call scanner.close(), since the source is a String } // PRIVATE private final File fFile; private static void log(Object aObject){ System.out.println(String.valueOf(aObject)); } private String quote(String aText){ //String QUOTE = "'"; //return QUOTE + aText + QUOTE; return aText; } }
- 05-30-2011, 11:31 AM #2
What happens with your current code? Errors? Exceptions? Something else?
.gif)
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 05-30-2011, 11:37 AM #3
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
Everything is fine. I would like to read "N: FN: Email:" from the file and put it in an array. I don't know how to do it using split or using Delimiter. Delimiter, no option to delimit the characters. so, i have to use "split" to do this. I need some assistance from this. thank you.
- 05-30-2011, 11:48 AM #4
Here is what API documentation has to say about it : String.split()
You can also search the web for the example which can give you better idea. Here is one that I found : Java String Split Example
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 05-30-2011, 12:05 PM #5
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
Thank u very much for your help. if you could tell me how could i read those elements from a file. Thank
- 05-30-2011, 12:09 PM #6
Last edited by DarrylBurke; 05-30-2011 at 12:12 PM.
- 05-30-2011, 12:14 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Maybe the following little method can help you out (halfway); it reads a String line and a prefix (also a String). If the prefix exists in the line it returns the suffix of the line, otherwise it returns null. Here it is:
kind regards,Java Code:String suffix(String line, String prefix) { int l= prefix.length(); if (line.startsWith(prefix)) return line.substring(l); return null; }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-30-2011, 12:28 PM #8
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
thnak U. It helps.
- 05-30-2011, 03:55 PM #9
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
Help
Guys at last I'm able to read information from the file but not appropriately:
My output is :Java Code:import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; public class exam { public static void main(String[] args) throws Exception { // File path & name String filepath = ("D:/Hfm.part"); // Read in file to determine how many lines it has FileInputStream in = new FileInputStream(filepath); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; int counter = 0; while ((strLine = br.readLine()) != null) { counter++; } in.close(); // Read in file FileInputStream in2 = new FileInputStream(filepath); BufferedReader br2 = new BufferedReader(new InputStreamReader(in2)); String strLine2; // Declare array String[] myarray; myarray = new String[counter]; // Add each line to the array for (int i = 0; i < myarray.length; i++){ myarray[i] = br2.readLine(); } in2.close(); // Loop through array looking for the correct text, format & print for (int c = 0; c < myarray.length; c++){ if(myarray[c].contains("BEGIN:VCARD") || myarray[c].contains("") ){ String[] split = myarray[c+1].split(" "); String contacts1 = ""; for (int i = 0; i < split.length; i++){ if (split[i].contains("N:") &! split[i].contains("-")){ contacts1 = split[i].replace("\"", ""); } if (split[i].contains("FN:") &! split[i].contains("-")){ contacts1 = split[i].replace("\"", ""); } if (split[i].contains("EMAIL;") &! split[i].contains("-")){ contacts1 = split[i].replace("\"", ""); } if (split[i].contains("TEL") &! split[i].contains("-")){ contacts1 = split[i].replace("\"", ""); } if (split[i].contains("URL;type") &! split[i].contains("-")){ contacts1 = split[i].replace("\"", ""); } } System.out.println(contacts1); } } } }
Still i want to remove the first 2 lines of my output and the error. Thank U guys.Java Code:BEGIN:VCARD VERSION:3.0 N:Veera_Kumar;; FN:Veera_Kumar EMAIL;type=internet:KUMARg_81@yahoo.com Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 88 at exam.main(exam.java:46)
- 05-30-2011, 04:06 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
- 05-30-2011, 04:06 PM #11
Do you understand the error message? Look at line 46 in the exam.java file.Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 88
at exam.main(exam.java:46)
Is there an array used there?
Does the array have 88 elements? The code is trying to get to it.
Look at the index to the array and be sure that it does NOT exceed the size of the array.
- 05-30-2011, 05:00 PM #12
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
Got it & fixed the problem. Thank U.
- 05-31-2011, 09:35 AM #13
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
Guys, I'm able to parse the file right now. I need some assistance that how can i make a Hashmap/Hashtable from the output.
For example
BEGIN:VCARD
VERSION:3.0
N:Rangarajkarthik
FN:karthik Rangaraj
EMAIL:kart2006@gmail.com
EMAIL:karthikrangaraj@yahoo.com
END:VCARD
From the above sample output, how can i make an id for the name and store those email address corresponding to the name.
Ideas and Help will be highly appreciated. Thank u guys.
- 05-31-2011, 09:43 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
If all the lines are of the form key:value where both key and value don't contain a colon you can do this:
kind regards,Java Code:Map<String, String> map= new HasMap<String, String>(); BufferedReader br= // a reader for your data for (String line; (line= br.readLine()) != null;) { String[] pair= line.split(":"); map.put(pair[0], pair[1]); }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-31-2011, 09:58 AM #15
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
Thanks a lot for the idea.
Name Gmail Facebook Yahoo Aol
[ unless you like viagra, don't put your email addresses here ]
I want to make a table like this using HashMap Array linkedlist from the output.
BEGIN:VCARD
VERSION:3.0
N:Rangarajkarthik
FN:karthik Rangaraj
EMAIL:kart2006@gmail.com
EMAIL:karthikrangaraj@yahoo.com
END:VCARD
BEGIN:VCARD
VERSION:3.0
N:Veera_Kumar
FN:Veera_Kumar
EMAIL:KUMARg_81@yahoo.com
END:VCARD
Here is the link for my list : http://tinypic.com/r/14d1w1x/7
Any Ideas?Last edited by JosAH; 05-31-2011 at 10:26 AM. Reason: email addresses removed
- 05-31-2011, 10:27 AM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
- 05-31-2011, 10:35 AM #17
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
Map<String, String> map= new HasMap<String, String>();
From this i can map only two values but if u check my image or the kind of table what i want to make it has 5 values. So, i'm confused! :(
I have started like this§Java Code:import java.io.*; import java.util.*; public class HashMapText { public static void main(String[] args) throws IOException { Map<String, String> map= new HashMap<String, String>(); BufferedReader br= new BufferedReader(new FileReader( "D:/latestcontact.part")); for (String line; (line= br.readLine()) != null;) { String[] pair= line.split(":"); map.put(pair[0], pair[1]); } System.out.println(); } }Last edited by karthinkin; 05-31-2011 at 10:44 AM. Reason: java code added
- 05-31-2011, 02:30 PM #18
Create you own class to hold those 5 values and put an instance of that class in the Mapwhat i want to make it has 5 values
- 05-31-2011, 05:13 PM #19
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
Guys, finally i managed to create a hashtable in the base class itself. And created two sub classes to generate an output using hashtable.
Here is my code :
Now, how could i instantiate the output of my example class to the hashmap and how could i give an automatic id generation for each record.Java Code:Exam.Java import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.Hashtable; public class exam { public static void main(String[] args) throws Exception { // File path & name String filepath = ("D:/cotntactsnew.part"); // Read in file to determine how many lines it has FileInputStream in = new FileInputStream(filepath); BufferedReader br = new BufferedReader(new InputStreamReader(in)); @SuppressWarnings("unused") String strLine; int counter = 0; while ((strLine = br.readLine()) != null) { counter++; } in.close(); // Read in file FileInputStream in2 = new FileInputStream(filepath); BufferedReader br2 = new BufferedReader(new InputStreamReader(in2)); @SuppressWarnings("unused") String strLine2; // Declare array String[] myarray; myarray = new String[counter]; // Add each line to the array for (int i = 0; i < myarray.length; i++){ myarray[i] = br2.readLine(); } in2.close(); // Loop through array looking for the correct text, format & print for (int c = 0; c < myarray.length; c++){ //System.out.println(""); if(myarray[c].contains("")) { //| myarray[c].contains("") ) String[] split = myarray[c].split(" "); String contacts = " "; //System.out.println(""); for (int i = 0; i < split.length; i++){ if (split[i].contains("N:") &! split[i].contains(";")){ contacts = split[i].replace("\"", ""); } if (split[i].contains("FN:") &! split[i].contains(";")){ contacts = split[i].replace("\"", ""); } if (split[i].contains("EMAIL:") &! split[i].contains(";")){ contacts = split[i].replace("\"", ""); } if (split[i].contains("TEL:") &! split[i].contains(";")){ contacts = split[i].replace("\"", ""); } if (split[i].contains("URL:") &! split[i].contains(";")){ contacts = split[i].replace("\"", ""); } /*if (split[i].contains("END:") &! split[i].contains("-")){ contacts = split[i].replace("\"", ""); }*/ } System.out.println(contacts); Hashtable<Integer,Person> hmap= new Hashtable<Integer,Person>(); } } } @Override public String toString() { return "exam [getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]"; } } Person.java import java.lang.String; public class Person { String Name; String FName; Contat ct = new Contat(); public String getName() { return Name; } public void setName(String name) { Name = name; } public String getFName() { return FName; } public void setFName(String fName) { FName = fName; } public Contat getCt() { return ct; } public void setCt(Contat ct) { this.ct = ct; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((FName == null) ? 0 : FName.hashCode()); result = prime * result + ((Name == null) ? 0 : Name.hashCode()); result = prime * result + ((ct == null) ? 0 : ct.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (FName == null) { if (other.FName != null) return false; } else if (!FName.equals(other.FName)) return false; if (Name == null) { if (other.Name != null) return false; } else if (!Name.equals(other.Name)) return false; if (ct == null) { if (other.ct != null) return false; } else if (!ct.equals(other.ct)) return false; return true; } @Override public String toString() { return "Person [Name=" + Name + ", FName=" + FName + ", ct=" + ct + "]"; } } Contat.java public class Contat { String Facebook; String Gmail; String Yahoo; String Url; public String getFacebook() { return Facebook; } public void setFacebook(String facebook) { Facebook = facebook; } public String getGmail() { return Gmail; } public void setGmail(String gmail) { Gmail = gmail; } public String getYahoo() { return Yahoo; } public void setYahoo(String yahoo) { Yahoo = yahoo; } public String getUrl() { return Url; } public void setUrl(String url) { Url = url; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((Facebook == null) ? 0 : Facebook.hashCode()); result = prime * result + ((Gmail == null) ? 0 : Gmail.hashCode()); result = prime * result + ((Url == null) ? 0 : Url.hashCode()); result = prime * result + ((Yahoo == null) ? 0 : Yahoo.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Contat other = (Contat) obj; if (Facebook == null) { if (other.Facebook != null) return false; } else if (!Facebook.equals(other.Facebook)) return false; if (Gmail == null) { if (other.Gmail != null) return false; } else if (!Gmail.equals(other.Gmail)) return false; if (Url == null) { if (other.Url != null) return false; } else if (!Url.equals(other.Url)) return false; if (Yahoo == null) { if (other.Yahoo != null) return false; } else if (!Yahoo.equals(other.Yahoo)) return false; return true; } @Override public String toString() { return "Contat [Facebook=" + Facebook + ", Gmail=" + Gmail + ", Yahoo=" + Yahoo + ", Url=" + Url + "]"; } }
For ex : ID 1 Karthik kart2006@gmail.com karthikrangaraj@yahoo.com.
Thaning u guys.
- 05-31-2011, 05:14 PM #20
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
Guys, finally i managed to create a hashtable in the base class itself. And created two sub classes to generate an output using hashtable.
Here is my code :
Now, how could i instantiate the output of my example class to the hashmap and how could i give an automatic id generation for each record.Java Code:Exam.Java import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.Hashtable; public class exam { public static void main(String[] args) throws Exception { // File path & name String filepath = ("D:/cotntactsnew.part"); // Read in file to determine how many lines it has FileInputStream in = new FileInputStream(filepath); BufferedReader br = new BufferedReader(new InputStreamReader(in)); @SuppressWarnings("unused") String strLine; int counter = 0; while ((strLine = br.readLine()) != null) { counter++; } in.close(); // Read in file FileInputStream in2 = new FileInputStream(filepath); BufferedReader br2 = new BufferedReader(new InputStreamReader(in2)); @SuppressWarnings("unused") String strLine2; // Declare array String[] myarray; myarray = new String[counter]; // Add each line to the array for (int i = 0; i < myarray.length; i++){ myarray[i] = br2.readLine(); } in2.close(); // Loop through array looking for the correct text, format & print for (int c = 0; c < myarray.length; c++){ //System.out.println(""); if(myarray[c].contains("")) { //| myarray[c].contains("") ) String[] split = myarray[c].split(" "); String contacts = " "; //System.out.println(""); for (int i = 0; i < split.length; i++){ if (split[i].contains("N:") &! split[i].contains(";")){ contacts = split[i].replace("\"", ""); } if (split[i].contains("FN:") &! split[i].contains(";")){ contacts = split[i].replace("\"", ""); } if (split[i].contains("EMAIL:") &! split[i].contains(";")){ contacts = split[i].replace("\"", ""); } if (split[i].contains("TEL:") &! split[i].contains(";")){ contacts = split[i].replace("\"", ""); } if (split[i].contains("URL:") &! split[i].contains(";")){ contacts = split[i].replace("\"", ""); } /*if (split[i].contains("END:") &! split[i].contains("-")){ contacts = split[i].replace("\"", ""); }*/ } System.out.println(contacts); Hashtable<Integer,Person> hmap= new Hashtable<Integer,Person>(); } } } @Override public String toString() { return "exam [getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]"; } } Person.java import java.lang.String; public class Person { String Name; String FName; Contat ct = new Contat(); public String getName() { return Name; } public void setName(String name) { Name = name; } public String getFName() { return FName; } public void setFName(String fName) { FName = fName; } public Contat getCt() { return ct; } public void setCt(Contat ct) { this.ct = ct; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((FName == null) ? 0 : FName.hashCode()); result = prime * result + ((Name == null) ? 0 : Name.hashCode()); result = prime * result + ((ct == null) ? 0 : ct.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (FName == null) { if (other.FName != null) return false; } else if (!FName.equals(other.FName)) return false; if (Name == null) { if (other.Name != null) return false; } else if (!Name.equals(other.Name)) return false; if (ct == null) { if (other.ct != null) return false; } else if (!ct.equals(other.ct)) return false; return true; } @Override public String toString() { return "Person [Name=" + Name + ", FName=" + FName + ", ct=" + ct + "]"; } } Contat.java public class Contat { String Facebook; String Gmail; String Yahoo; String Url; public String getFacebook() { return Facebook; } public void setFacebook(String facebook) { Facebook = facebook; } public String getGmail() { return Gmail; } public void setGmail(String gmail) { Gmail = gmail; } public String getYahoo() { return Yahoo; } public void setYahoo(String yahoo) { Yahoo = yahoo; } public String getUrl() { return Url; } public void setUrl(String url) { Url = url; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((Facebook == null) ? 0 : Facebook.hashCode()); result = prime * result + ((Gmail == null) ? 0 : Gmail.hashCode()); result = prime * result + ((Url == null) ? 0 : Url.hashCode()); result = prime * result + ((Yahoo == null) ? 0 : Yahoo.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Contat other = (Contat) obj; if (Facebook == null) { if (other.Facebook != null) return false; } else if (!Facebook.equals(other.Facebook)) return false; if (Gmail == null) { if (other.Gmail != null) return false; } else if (!Gmail.equals(other.Gmail)) return false; if (Url == null) { if (other.Url != null) return false; } else if (!Url.equals(other.Url)) return false; if (Yahoo == null) { if (other.Yahoo != null) return false; } else if (!Yahoo.equals(other.Yahoo)) return false; return true; } @Override public String toString() { return "Contat [Facebook=" + Facebook + ", Gmail=" + Gmail + ", Yahoo=" + Yahoo + ", Url=" + Url + "]"; } }
For ex : ID-1 Karthik kart2006@gmail.com karthikrangaraj@yahoo.com.
Thaning u guys.
Similar Threads
-
Parse file into arrayList
By zenitis in forum New To JavaReplies: 8Last Post: 05-02-2011, 08:01 AM -
help to parse an xhtml file
By nijil in forum New To JavaReplies: 0Last Post: 02-20-2010, 10:37 AM -
How to parse a .db file ?
By aminov in forum JDBCReplies: 0Last Post: 08-06-2009, 04:11 PM -
How to parse the CSV(Comma separation values)file and validate the file using java
By padmajap13 in forum Advanced JavaReplies: 7Last Post: 05-23-2008, 03:46 AM -
how to parse an xml file
By oregon in forum XMLReplies: 3Last Post: 08-01-2007, 04:53 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks