Content: Welcome | java links | Articles | Search | Friends&Snaps |News&views 

Srinagesh Susarla  - your Guide to:

java Glossary & FAQ

 

Q1.  How to parse a CSV file (line) ? StringTokenizer gives a pain in the neck by not giving the nulls between the successive commas. So we devised this method to take care of such stuff.

Here it is!! format expected  a,b,c,,d,,,e,f,g

returns a string array - a  b  c null d null null e f g 

/** fileType =','

*/

public static String[] getSplitInput(String line, BufferedReader rdr,char fileType)
throws Exception
{ Vector v;
    StringBuffer s;
   String[] retVal;
   int steht;
   int ctyp;
   int i;
   char c;
   if ((line == null)||(line.trim().length() == 0))
  { if (rdr == null)
    return null;
   line = readInputLine(rdr);
   if (line == null)
  return null;
 }
 v = new Vector();
 s = null;
 steht = 0;
 i = 0;
 while ((line != null)&&
 (i < line.length()))
 {c = line.charAt(i);
  if (((c == ' ')||(c == '\t')||(c == '\n')||(c == '\r')) && ( fileType != '\t'))
 ctyp = 0;
 else if (c == '\"')
 ctyp = 1;
 else if (c == '\\')
 ctyp = 2;
 else if (c == fileType)
 ctyp = 3;
 else
 ctyp = 4;
 switch((steht*5)+ctyp)
 {case 0: /* Ready for next field/White space */
 case 5: /* Scanning left white space/White space */
 case 30: /* At end of field/White space */
 break;
  case 1: /* Ready for next field/Double quote */
  s = new StringBuffer();
  case 6: /* Scanning left white space/Double quote */
  steht = 3;
  break;
  case 2: /* Ready for next field/Escape */
  case 7: /* Scanning left white space/Escape */
  case 11: /* Scanning data, not quoted field/Double quote */
  case 12: /* Scanning data, not quoted field/Escape */
  case 31: /* At end of field/Double quote */
  case 32: /* At end of field/Escape */
  case 34: /* At end of field/Normal char */
  throw new Exception("Data format error: " + line + " in col " + i + " character "   + c );
  case 3: /* Ready for next field/Comma */
  s = new StringBuffer();
  steht = 1;
  case 8: /* Scanning left white space/Comma */
  v.addElement("");
  break;
  case 4: /* Ready for next field/Normal char */
  s = new StringBuffer();
  case 9: /* Scanning left white space/Normal char */
  steht = 2;
  case 10: /* Scanning data, not quoted field/White space */
  case 14: /* Scanning data, not quoted field/Normal char */
  case 15: /* Within quoted field/White space */
  case 18: /* Within quoted field/Comma */
  case 19: /* Within quoted field/Normal char */
  s.append(c);
  break;
  case 13: /* Scanning data, not quoted field/Comma */
  case 33: /* At end of field/Comma */
  v.addElement(s.toString());
  s = new StringBuffer();
  steht = 1;
  break;
  case 16: /* Within quoted field/Double quote */
  if ((i < (line.length() - 1))&&(line.charAt(i+1)=='\"'))
  {i++;
  s.append(c);
  }
  else
  steht = 6;
  break;
  case 17: /* Within quoted field/Escape */
  steht = 5;
  break;
  case 25: /* Just hit \, quoted field/White space */
  case 26: /* Just hit \, quoted field/Double quote */
  case 27: /* Just hit \, quoted field/Escape */
  case 28: /* Just hit \, quoted field/Comma */
  case 29: /* Just hit \, quoted field/Normal char */
  steht = 3;
  s.append(c);
  break;
 }
  i++;
  if (i >= line.length())
  switch(steht)
  {case 1:
  case 2:
  case 6:
  v.addElement(s.toString());
  break;
  case 5:
  steht = 3;
  case 3:
  s.append('\r');
  s.append('\n');
  line = readInputLine(rdr);
  i = 0;
  case 0:
  break;
  }
 }
  if (v.size() == 0)
  return null;
  retVal = new String[v.size()];
  v.copyInto(retVal);
  return retVal;
  }

/**
* reads a single line.
* @return line in the form of a String
* @param buffered reader
*/
public static String readInputLine(BufferedReader rdr) throws IOException
{
String lbf;
if (rdr == null)
return null;
do
{
lbf = rdr.readLine();
}while ((lbf != null)&&
((lbf.trim().length() == 0)||
(lbf.startsWith(";"))));
if (lbf != null)
lbf = lbf.trim();
return lbf;
}

Q2.  Proxies and URLConnection:-My boss gave me a simple assignment to connect to other sites and get their pages and also post data and retrieve results. My Boss thought it was simple enough for me to get it done. Neither, did i think i had to prove myself with such a simple assignment. But since the network in our company goes through a proxy, I had to spend two sleepless caffinated  nights with no result. 

URLConnection simply didnt work with proxies. I kept getting an IOException with "no -further  information".

Thanks to folks on deja's newsgroups, i got the stuff to get this done

  System.getProperties().put("prxySet","true");

  System.getProperties().put("proxyHost","my.proxy.com");

  System.getProperties().put("proxyport","80");

  Thats it! then continue as usual with the URLConnections methods

 


 


Copyright @nagi  All rights reserved to those who want to copy content from these pages!