Java program: Comma-separated values, or CSV, is widely-used format for represen
ID: 3664183 • Letter: J
Question
Java program:
Comma-separated values, or CSV, is widely-used format for representing tabular data. Each row of a table is a line of text; the fields on each line are separated by commas.
For example, the CSV representation of the table:
Heading 1
Heading 2
Heading 3
Row 1
x1
y1
z1
Row 2
x2
y2
z2
is given by:
,Heading 1,Heading 2,Heading 3
Row 1,x1,y1,z1
Row 2,x2,y2,z2
Implement a library to handle CSV files. This library should be designed so that it is worthy of general use.(This is java programming)
Heading 1
Heading 2
Heading 3
Row 1
x1
y1
z1
Row 2
x2
y2
z2
Explanation / Answer
using System.IO; using System.Text.RegularExpressions; public sealed class CsvReader : System.IDisposable { public CsvReader( string fileName ) : this( new FileStream( fileName, FileMode.Open, FileAccess.Read ) ) { } public CsvReader( Stream stream ) { __reader = new StreamReader( stream ); } public System.Collections.IEnumerable RowEnumerator { get { if ( null == __reader ) throw new System.ApplicationException( "I can't start reading without CSV input." ); __rowno = 0; string sLine; string sNextLine; while ( null != ( sLine = __reader.ReadLine() ) ) { while ( rexRunOnLine.IsMatch( sLine ) && null != ( sNextLine = __reader.ReadLine() ) ) sLine += " " + sNextLine; __rowno++; string[] values = rexCsvSplitter.Split( sLine ); for ( int i = 0; i -1 ) s = QUOTE + s + QUOTE; return s; } public static string Unescape( string s ) { if ( s.StartsWith( QUOTE ) && s.EndsWith( QUOTE ) ) { s = s.Substring( 1, s.Length - 2 ); if ( s.Contains( ESCAPED_QUOTE ) ) s = s.Replace( ESCAPED_QUOTE, QUOTE ); } return s; } private const string QUOTE = """; private const string ESCAPED_QUOTE = """"; private static char[] CHARACTERS_THAT_MUST_BE_QUOTED = { ',', '"', ' ' }; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.