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

