File Parsing

PrakashVarma
PrakashVarma used Ask the Experts™
on
I have a flat file with coloums header.

CMD_ID||CAI_CMD||DEPENDANCY_CMD_ID
1||CREATE:HLRSUB:MSISDN,@Param_MSISDN:IMSI,@Param_IMSI:PROFILE,2:RSA,1;||-1
2||SET:HLRSUB:MSISDN,@Param_MSISDN:BS3G,0;||1
3||SET:HLRSUB:IMSI,@Param_IMSI:OBO,1:OBI,2:PDPCP,250;||1
4||SET:ICSSUB:MSISDN,@Param_MSISDN:PAYTYPE,2:SCREENTYPE,1;||-1

could please help me how to parse above flat file.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Unless there are complications, a Scanner 's' with


	s.useDelimiter("[\r\n\\|]+");
	while(s.hasNext()) {
	    System.out.printf("Next is '%s'\n", s.next());
	}

Open in new window


should do it
try openCSV, its simple

http://opencsv.sourceforge.net/
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Top Expert 2016

Commented:
Generally speaking, k is right in that you should use a proper csv api but

a. a strict csv parse will leave you with two empty fields because of the improper use of delimiters
b. if it's simple (e.g. the fields don't contain embedded delimiters) you should be ok

Author

Commented:
if it's .txt file,how can i do it with opencsv api and delimiter is pipline(||)?
it doesn't need to be .csv file, you can input any file as long as the content in proper format.
try single pipe( | ) as delimiter

Author

Commented:
requirement is should use double pipe(||)

please tell me how can i do it with open csv?
double || is not possible, as mentioned by CEHJ, if the content will not have single pipe, don't worry about single | as delimiter, just ignore alternate values.

otherwise, read each line of the file and split it,

String[] tokens = str.slit( "\\|\\|" ) ;
Top Expert 2016

Commented:
Did you try the suggestion i made earlier?

Author

Commented:
Hi Friends,
Thanks For Your Replies and Suggestions.,

I'm using JFlat API for my requirement .


		Map<String, String> templateColomsMap = new HashMap<String, String>();
		templateColomsMap.put("CMD_ID", "CMD_ID");
		templateColomsMap.put("CAI_CMD", "CAI_CMD");
		templateColomsMap.put("DEPENDANCY_CMD_ID", "DEPENDANCY_CMD_ID");


		ColumnMapping tempMappingStrategy = new HeaderColumnNameMapping(
				templateColomsMap);
		ReaderRowMapper<Template> templateReaderRowMapperner = new BeanReaderRowMapper<Template>(
				Template.class, tempMappingStrategy);
		FlatFileReader<Template> templateFlatFileReader = new FlatFileReader<Template>(
				new FileReader("D://data//Template_ Batch120408.txt"),new LineParser() {

					@Override
					public String[] parseLine(String arg0) throws IOException {
						// TODO Auto-generated method stub
						return arg0.split("\\|\\|");
					}
				}, templateReaderRowMapperner);


		List<Template> templateList = templateFlatFileReader.readAll();

		for (int i = 0; i < templateList.size(); i++) {
			System.out.print(templateList.get(i).getCMD_ID());
			System.out.print("\t" + templateList.get(i).getCAI_CMD());
			System.out.print("\t" + templateList.get(i).getDEPENDANCY_CMD_ID());
			System.out.println();

		}

Open in new window

Author

Commented:
Hi Friends,
like above,I did same for another text file but getting null values,please find Attachment.
and i'm sending my code,please help


public class BatchParser {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub

		
		Map<String, String> batchColomsMap = new HashMap<String, String>();
		batchColomsMap.put("Param_MSISDN", "Param_MSISDN");
		batchColomsMap.put("Param_IMSI", "Param_IMSI");

		ColumnMapping batchMappingStrategy = new HeaderColumnNameMapping(batchColomsMap);
		
		System.out.print(batchMappingStrategy.getNumberOfColumns());
		
		ReaderRowMapper<Batch> batchReaderRowMapperner = new BeanReaderRowMapper<Batch>(Batch.class, batchMappingStrategy);
		FlatFileReader<Batch> batchFlatFileReader = new FlatFileReader<Batch>(new FileReader("D://data//Batch120408.txt"), new LineParser() {

					@Override
					public String[] parseLine(String arg0) throws IOException {
						// TODO Auto-generated method stub
						return arg0.split("\\|\\|");
					}
				}, batchReaderRowMapperner);
		
		List<Batch> batchList = batchFlatFileReader.readAll();
		for (int i = 0; i < batchList.size(); i++) {
			System.out.print(batchList.get(i).getParam_MSISDN());
			System.out.print("\t"+batchList.get(i).getParam_IMSI());
			System.out.println();

		}


	}

}

Open in new window

Top Expert 2016

Commented:
I'm using JFlat API for my requirement .

Not an api i know unfortunately, and since there's not even any public javadoc available that i can find, i can't help sorry
Top Expert 2016

Commented:
return arg0.split("\\|\\|");

Open in new window


How many tokens is that returning?
Top Expert 2016

Commented:
?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial