:?:Pessoal, alguém pode me ajudar? Preciso abrir um arquivo RTF (documento) via código, e substituir constantes que criei neste documento: por exemplo, #TABELA_DISTRIBUICAO_SEXO# ou #NOME_DO_MEDICAMENTO# (Digitei estas constantes exatamente desta forma, começando e terminando com o sinal #). Bom preciso substituir essas constantes por resultados de queries (selects) que criei no SQL 2000, ou seja, além de abrir o arquivo, tenho de trocar cada constante criada pelo valor correspondente da query do SQL. Meu chefe me passou algumas funções para auxiliar, que posto logo abaixo, mas não consigo entender como encaixá-la no meu caso. Alguém pode me ajudar a analisar estas funções ou então mostrar para mim alguma mais “clara” ?
String texto = readFile(new File(“PSUR Luxfacta1.rtf”));
private String readFile(File file) throws Exception {
FileInputStream fis = null;
byte[] buf = new byte[4 * 1024];
StringBuffer bf = new StringBuffer();
try {
fis = new FileInputStream(file);
while (fis.read(buf) >= 0) {
String arquivo = new String(buf);
bf.append(arquivo);
}
} finally {
if (fis != null)
fis.close();
}
return bf.toString();
}
public String replace (
final String aInput, // texto
final String aOldPattern, // de
final String aNewPattern // para
){
if ( aOldPattern.equals("") ) {
throw new IllegalArgumentException("Old pattern must have content.");
}
final StringBuffer result = new StringBuffer();
//startIdx and idxOld delimit various chunks of aInput; these
//chunks always end where aOldPattern begins
int startIdx = 0;
int idxOld = 0;
while ((idxOld = aInput.indexOf(aOldPattern, startIdx)) >= 0) {
//grab a part of aInput which does not include aOldPattern
result.append( aInput.substring(startIdx, idxOld) );
//add aNewPattern to take place of aOldPattern
result.append( aNewPattern );
//reset the startIdx to just after the current match, to see
//if there are any further matches
startIdx = idxOld + aOldPattern.length();
}
//the final chunk will go to the end of aInput
result.append( aInput.substring(startIdx) );
return result.toString();
}
