Java Reference
In-Depth Information
* entab: process one file, replacing blanks with tabs.
*
* @param is A BufferedReader opened to the file to be read.
* @param out a PrintWriter to send the output to.
*/
public
public
void
void
entab
(
BufferedReader is
,
PrintWriter out
)
throws
throws
IOException
{
String line
;
// main loop: process entire file one line at a time.
while
while
((
line
=
is
.
readLine
()) !=
null
null
) {
out
.
println
(
entabLine
(
line
));
}
}
/**
* entab: process one file, replacing blanks with tabs.
*
* @param is A BufferedReader opened to the file to be read.
* @param out A PrintStream to write the output to.
*/
public
public
void
void
entab
(
BufferedReader is
,
PrintStream out
)
throws
throws
IOException
{
entab
(
is
,
new
new
PrintWriter
(
out
));
}
/**
* entabLine: process one line, replacing blanks with tabs.
*
* @param line -
* the string to be processed
*/
public
public
String
entabLine
(
String line
) {
int
int
N
=
line
.
length
(),
outCol
=
0
;
StringBuffer sb
=
new
new
StringBuffer
();
char
char
ch
;
int
int
consumedSpaces
=
0
;
for
for
(
int
int
inCol
=
0
;
inCol
<
N
;
inCol
++) {
ch
=
line
.
charAt
(
inCol
);
// If we get a space, consume it, don't output it.
// If this takes us to a tab stop, output a tab character.
iif
(
ch
==
' '
) {
Debug
.
println
(
"space"
,
"Got space at "
+
inCol
);
iif
(!
tabs
.
isTabStop
(
inCol
)) {
consumedSpaces
++;
}
else
else
{
Debug
.
println
(
"tab"
,
"Got a Tab Stop "
+
inCol
);
sb
.
append
(
'\t'
);