Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using opencsv

807603Nov 14 2007 — edited Nov 14 2007
All,
Im am using opencsv to parse csv file. I use it to:
1. get date ou of file
2. read a separate properties file for config info.
3. Finally, read the data for processing.

When I get to step 3, the read does not work.
Any ideas?

Code

package mybeans;

import org.apache.regexp.*;
import java.util.*;
import ClientDataProc.Logging;
import ClientDataProc.BalancesDataClass;
import ClientDataProc.ControlsAccess;
import au.com.bytecode.opencsv.CSVReader;

import java.io.*;



public class ListTest {

String find="";

//lets get headers from config file.
String[] headers;
String finalDate ="";
String date ="";
StringBuffer buf = new StringBuffer();
private Logging log = new Logging();

public ListTest(Logging l){
log = l;
}

public Logging getLog(){
return log;

}


public static void main(String[] args) throws IOException {
Logging log = new Logging();

FileInputStream in = new FileInputStream("royal");
BufferedReader buf = new BufferedReader(new FileReader("Cash.csv"));
ListTest test = new ListTest(log);
LinkedList clients = new LinkedList();
//String[] clients;
clients = test.readData(buf, in,"royal");
System.out.println(clients.size());// here I get 0!?

}

public LinkedList<BalancesDataClass> readData(BufferedReader filename, InputStream config, String bo)throws IOException {




//read our properties file
Properties props = new Properties();

try{
props.load(config);
}catch(IOException ioe){
ioe.printStackTrace();
}

//get our headers.
String headers = props.getProperty("balanceHeaders");
String[] headerResults = headers.split(",");
System.out.println(headers);
//get our row offset. This tells app which line to start processing from.
String offset = props.getProperty("balanceOffset");
int startLine = Integer.parseInt(offset);
System.out.println("StartLine:"+startLine);
String fundco="";


BalancesDataClass balances = new BalancesDataClass(controls,fundco,-1,log);


LinkedList<BalancesDataClass> list = new LinkedList<BalancesDataClass>();




//lets get the date out of the file.
CSVReader readerDate = new CSVReader(filename);
String [] nextLineDate;
while((nextLineDate = readerDate.readNext()) != null){
for(int i = 0;i<nextLineDate.length;i++){
finalDate = findDate(nextLineDate);
if(finalDate != null || finalDate != ""){
buf.append(finalDate);
//System.out.println(buf.toString());
break;

}


break;
}
}
System.out.println(buf.toString());//here I have the date

//step 3
//now we read the data file.

CSVReader reader = new CSVReader(filename,',','\"',startLine);
String[] data;
while((data = reader.readNext()) != null){
System.out.println(data[0]);
for(int i =0;i<headerResults.length;i++){



System.out.println("header"+headerResults[i] + data[i] + buf.toString());

//SAVE DATA
balances.saveData(headerResults[i], data[i]);
}
list.add(balances);

}

System.out.println(list.size());//getting zero here?!?

return list;











}

private static String findDate(String part){
String date="";
String pattern = "[0-9][0-9]" + "/" + "[0-9][0-9]" + "/" + "[0-9][0-9][0-9][0-9]";
RE r = new RE(pattern);

if(r.match(part)){
//System.out.println("The match is: " + r.getParen(0));
date = r.getParen(0);

}

return date;
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 12 2007
Added on Nov 14 2007
1 comment
360 views