Hola José,
Puedes usar el siguiente código de ejemplo para leer un archivo texto
caracter por caracter en java, espero que te sea de ayuda:
*public class *FileParser
{
*private *BufferedReader br;
*public *static void main(String[] args) *throws *FileNotFoundException,
IOException
{
FileParser pf = *new *FileParser();
pf.processFile();
}
public void processFile() throws FileNotFoundException, IOException
{
//se abre el archivo para poder leer su contenido.
br = new
BufferedReader(*new*InputStreamReader(getClass().getResourceAsStream(
*"test.txt"*)));
*char *tipo;
//se lee el primer caracter para determinar el tipo de registro.
*int *intChar = -1;
*while *((intChar = br.read())!= -1) {
tipo = (*char*)intChar;
/**
* se lee y procesan los registros
* Los 3 tipos de registros contemplados, a saber son:
* A que contiene textos de 446, 18, 20 y 15 caracteres
respectivamente,
* B que contiene textos de 18, 20, 15 y 446 caracteres
respectivamente, y
* C que cuenta con textos de 20, 15, 446 y 18 caracteres
respectivamente.
*/
if (tipo=='A') {
System.out.printf("\nTipo
:\t%s\nCampo1:\t%s\nCampo2:\t%s\nCampo3:\t%s\nCampo4:\t%s\n",
tipo, read(446), read(18), read(20), read(15));
}
if (tipo=='B') {
System.out.printf("\nTipo
:\t%s\nCampo1:\t%s\nCampo2:\t%s\nCampo3:\t%s\nCampo4:\t%s\n",
tipo, read(18), read(20), read(15), read(446));
}
if (tipo=='C') {
System.out.printf("\nTipo
:\t%s\nCampo1:\t%s\nCampo2:\t%s\nCampo3:\t%s\nCampo4:\t%s\n",
tipo, read(20), read(15), read(446), read(18));
}
}
}
*public String *read(final int charCount) throws IOException
{
*char *[]data = new char[charCount];
StringBuffer sb = *new *StringBuffer();
*for *(*int *i = 0; i < charCount; i++) {
data[i] = (char)br.read();
}
*return *String.valueOf(data);
}
}
--
Eivar A. Montenegro. M.
¡El mundo es mejor cuando eres libre de elegir!
Linux user: #461766