Code:
import java.io.*;
public class SGML2Txt {
private String filePath=null;
public SGML2Txt(String filePath) {
this.filePath=filePath;
}
public void convert () throws IOException {
String baseName=filePath.substring(filePath.lastIndexOf("\\")+1);
String path=filePath.substring(0,filePath.lastIndexOf("\\"));
String newBaseName=baseName.substring(0,baseName.indexOf("."))+".txt";
String newFilePath=path+"\\"+newBaseName;
FileInputStream in=new FileInputStream(filePath);
FileOutputStream out=new FileOutputStream(newFilePath);
int n=-1;
boolean inBrackets=false;
while ((n=in.read())!=-1) {
char c=(char)n;
if (c=='<' || c=='>') {
inBrackets = (c=='<');
out.write(' ');
continue;
}
if (inBrackets) continue;
out.write(n);
}
in.close();
out.close();
System.out.println("File: "+newFilePath+" created");
}
public static void main(String[] args) {
SGML2Txt s2t = new SGML2Txt("c:\\tmp\\data.sgml");
try {
s2t.convert();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
Bookmarks