public class DeTab {
Tabs ts; // iniitialized in Constructor
public static void main(String[] argv) throws IOException {
DeTab dt = new DeTab(8);
dt.detab(new BufferedReader(new InputStreamReader(System.in)),
new PrintWriter(System.out));
}
/** detab one file (replace tabs with spaces)
* @param is - the file to be processed
* @param out - the updated file
*/
public void detab(BufferedReader is, PrintWriter out) throws IOException {
String line;
char c;
int col;
while ((line = is.readLine( )) != null) {
out.println(detabLine(line));
}
}
/** detab one line (replace tabs with spaces)
* @param line - the line to be processed
* @return the updated line
*/
public String detabLine(String line) {
char c;
int col;
StringBuffer sb = new StringBuffer( );
col = 0;
for (int i = 0; i < line.length( ); i++) {
// Either ordinary character or tab.
if ((c = line.charAt(i)) != '\t') {
sb.append(c); // Ordinary
++col;
continue;
}
do { // Tab, expand it, must put >=1 space
sb.append(' ');
} while (!ts.isTabStop(++col));
}
return sb.toString( );
}
}
The program that goes in the opposite direction—putting tabs in rather than taking them out—is the DeTab class shown in Example; only the core methods are shown.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.