Important alert: (current site time 7/15/2013 3:12:54 PM EDT)
 

VB icon

CommaReader.java

Email
Submitted on: 12/17/2002 9:10:40 PM
By: Paul R. Richards 
Level: Beginner
User Rating: By 2 Users
Compatibility: Java (JDK 1.2)
Views: 18968
author picture
(About the author)
 
     Extend BufferedReader to read a comma delineated file.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: CommaReader.java
// Description:Extend BufferedReader to read a comma delineated file.
// By: Paul R. Richards
//
// Inputs:comma delineated text file
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=3340&lngWId=2//for details.//**************************************

import java.util.*;
import java.io.*;
// Extend BufferedReader to parse comma-delineated files
/* Example usage:
try {
File fl = new File("data.txt");
FileReader fileReader = new FileReader(fl);
bufferedReader = new CommaReader(fileReader);
while (true) {
 if (bufferedReader.eof()) break;
 System.out.println ( bufferedReader.readToken());
}
} catch (Exception e) { 
System.out.println ("Error: " + e ); 
}
*/
public class CommaReader extends BufferedReader {
// Return true if the specified String can be interpretted
// as a base 10 number.
public static boolean isNumeric ( String token ) { 
 boolean ok = false;
 try {
double i = Double.valueOf ( token ).doubleValue();
ok = true;
 } catch (Exception e) { }
 return ok;
}
CommaReader ( Reader in ) {
 super ( in );
}
// return true if we are at the end of the buffer.
public boolean eof () { 
 int ch;
 try {
mark(100);
ch = read();
if (ch == -1) return true;
reset();
 } catch (Exception e) { 
return true;
 }
 return false;
}
public String readToken () { 
 String token = "";
 char c;
 int ch;
 try {
while ( true ) { 
 ch = read();
 if (ch == -1) break;
 if (ch == ',') break;
 c = (char) ch;
 token += Character.toString (c);
} 
 } catch (Exception e) {
return token; 
 }
 return token;
}
public String nextToken () { 
 String token = "";
 try {
mark(100);
token = readToken();
reset();
 } catch (Exception e) { }
 return token;
}
public double readNumber () { 
 String token = readToken ();
 double number = 0.0; 
 try {
number = Double.valueOf ( token ).doubleValue();
 } catch (Exception e) { }
 return number;
}
public int readByte () { 
 return (int) readNumber();
}
public int readSignedByte() { 
 int total = readByte();
 if (total >= 128) // Sign bit is set
total = total - 256; 
 return total;
} 
public int readSignedWord() { 
 int total = readWord();
 if (total >= 32768) // Sign bit is set
total = total - 65536; 
 return total;
}
public int readWord() { 
 int total = readByte();
 if (eof()) return 0;
 total = (total * 256) + readByte();
 return total;
}
 }


Other 8 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
10/15/2004 6:59:48 PMRicky Ong

i can't find the static void main class, so how do i compile and run it?
(If this comment was disrespectful, please report it.)

 
10/16/2004 11:46:11 PMPaul R. Richards

Check the example usage for how to use
(If this comment was disrespectful, please report it.)

 
4/14/2006 6:23:02 AMcihan

intersting
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.