Java Reference
In-Depth Information
PrintStream out
) {
int
int
nLines
=
0
;
try
try
{
String inputLine
;
while
while
((
inputLine
=
is
.
readLine
()) !=
null
null
) {
iif
(
inputLine
.
trim
().
equals
(
START_MARK
)) {
iif
(
printing
)
// These go to stderr, so you can redirect the output
System
.
err
.
println
(
"ERROR: START INSIDE START, "
+
fileName
+
':'
+
is
.
getLineNumber
());
printing
=
true
true
;
}
else
else iif
(
inputLine
.
trim
().
equals
(
END_MARK
)) {
iif
(!
printing
)
System
.
err
.
println
(
"ERROR: STOP WHILE STOPPED, "
+
fileName
+
':'
+
is
.
getLineNumber
());
printing
=
false
false
;
}
else
else iif
(
printing
) {
iif
(
number
) {
out
.
print
(
nLines
);
out
.
print
(
": "
);
}
out
.
println
(
inputLine
);
++
nLines
;
}
}
is
.
close
();
out
.
flush
();
// Must not close - caller may still need it.
iif
(
nLines
==
0
)
System
.
err
.
println
(
"ERROR: No marks in "
+
fileName
+
"; no output generated!"
);
}
catch
catch
(
IOException e
) {
System
.
out
.
println
(
"IOException: "
+
e
);
}
}
Parsing Comma-Separated Data
Problem
You have a string or a file of lines containing comma-separated values (CSV) that you need
to read. Many Windows-based spreadsheets and some databases use CSV to export data.