Leitura de arquivo - caractere a caractere

4 respostas
vivi_grieco

Ola,
estou tentando fazer a leitura de um arquivo .txt… ateh aih, blz, peguei o exemplo do artigo daqui do guj e funcionou bacana.
o problema eh q, alem da leitura de linhas, eu preciso ler caractere por caractere do arquivo.
ha algum metodo q faca isso??

4 Respostas

Amanweb

read

public int read()
throws IOException

Read a single character.

Overrides:
read in class Reader

Returns:
The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached

Throws:
IOException - If an I/O error occurs

p5f8

olá vivi,

não testei o código abaixo... mas creio que funcione...

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/*
 * Created on 18/12/2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Teste
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class LendoArquivoCaracteraCaracter {
	
	public static void Ler(InputStream is) throws IOException {
		
		BufferedReader bf = new BufferedReader(new InputStreamReader(is));
		
		char caracter;
		
			
		while ( (caracter = (char) bf.read()) != -1) {
			
			System.out.println(caracter);
		}
	}

	public static void main(String[] args) throws IOException {
		
		if (args.length == 0) {
			
			System.out.println("nomes dos arquivos nao informados.");
			System.out.println("sintaxe:");
			System.out.println("\t\tLendoArquivo [arquivo1.ext] [arquivo2.ext] ...\n\n");
			System.exit(1);
		}
		
		for (int i = 0; i < args.length; i++) {
			
			String arquivo = args[i];
			System.out.println("conteúdo do arquivo: " + arquivo);
			
			try {
				Ler(new FileInputStream(arquivo));
			} catch (IOException ioe) {
				System.out.println("arquivo " + arquivo + " nao encontrado!");
				System.out.println("programa abortado!");
				System.exit(1);
			}
		}
	}
	
}
nandinho_vip
import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

/*

  • Created on 18/12/2004
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
    */

/**

  • @author Teste

  • TODO To change the template for this generated type comment go to

  • Window - Preferences - Java - Code Style - Code Templates
    */
    public class LendoArquivoCaracteraCaracter {

    public static void Ler(InputStream is) throws IOException {

    BufferedReader bf = new BufferedReader(new InputStreamReader(is));
    
    char caracter;
    
    	
    while ( (caracter = (char) bf.read()) != -1) {
    	
    	System.out.println(caracter);
    }
    

    }

    public static void main(String[] args) throws IOException {

    if (args.length == 0) {
    	
    	System.out.println("nomes dos arquivos nao informados.");
    	System.out.println("sintaxe:");
    	System.out.println("\t\tLendoArquivo [arquivo1.ext] [arquivo2.ext] ...\n\n");
    	System.exit(1);
    }
    
    for (int i = 0; i < args.length; i++) {
    	
    	String arquivo = args[i];
    	System.out.println("conteúdo do arquivo: " + arquivo);
    	
    	try {
    		Ler(new FileInputStream(arquivo));
    	} catch (IOException ioe) {
    		System.out.println("arquivo " + arquivo + " nao encontrado!");
    		System.out.println("programa abortado!");
    		System.exit(1);
    	}
    }
    

    }

}

Já tentei e este código não funciona, tem algum outro aí?

p5f8

cara... fiz uma cagada.. desculpe...
ai vai o código corrigido...

import java.io.BufferedReader;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 
 /*
  * Created on 18/12/2004
  *
  * TODO To change the template for this generated file go to
  * Window - Preferences - Java - Code Style - Code Templates
  */
 
 /**
  * @author Teste
  *
  * TODO To change the template for this generated type comment go to
  * Window - Preferences - Java - Code Style - Code Templates
  */
 public class LendoArquivoCaracteraCaracter {
 	
 	public static void Ler(InputStream is) throws IOException {
 		
 		BufferedReader bf = new BufferedReader(new InputStreamReader(is));
 		
                int caracter = 0;
 		
                while ( (caracter = bf.read()) != -1) {
 			
                        System.out.print((char)caracter);
 		}
 	}
 
 	public static void main(String[] args) throws IOException {
 		
 		if (args.length == 0) {
 			
 			System.out.println("nomes dos arquivos nao informados.");
 			System.out.println("sintaxe:");
 			System.out.println("\t\tLendoArquivo [arquivo1.ext] [arquivo2.ext] ...\n\n");
 			System.exit(1);
 		}
 		
 		for (int i = 0; i < args.length; i++) {
 			
 			String arquivo = args[i];
 			System.out.println("conteúdo do arquivo: " + arquivo);
 			
 			try {
 				Ler(new FileInputStream(arquivo));
 			} catch (IOException ioe) {
 				System.out.println("arquivo " + arquivo + " nao encontrado!");
 				System.out.println("programa abortado!");
 				System.exit(1);
 			}
 		}
 	}
 	
 }
Criado 15 de março de 2005
Ultima resposta 16 de mar. de 2005
Respostas 4
Participantes 4