Leitor em java sequencial

2 respostas
N

Oi, boa tarde, estou tentando ler um arquivo txt. Nele tem salvo", por exemplo:

C:\Users\USUARIO\Documents\Backup\B.txt|C:\Users\USUARIO\Documents\Backup\A.txt|
Começa a ler e para quando encontra o char '|'
Scanner in = new Scanner(tabela); //tabela é o nome do arquivo onde esta salvo o exemplo
        ArrayList<String> resultado = new ArrayList();
        while(in.hasNext())
        {
           
            
            
        }
        
        return resultado;

Tenho duvidas dentro do while, não necessariamente precisa ser com scanner.

Tenho grandes problemas com arquivos, se alguém oferecer um link de um tutorial, ficaria agradecido. :D
obg

2 Respostas

alves.Felipe

ja tentou pesquisar no google?

http://www.google.com.br/#hl=pt-BR&q=ler+arquivo+java&oq=ler+arqui&aq=0&aqi=g10&aql=&gs_sm=e&gs_upl=2122341l2125268l0l2126940l11l11l1l2l2l0l313l1575l1.3.3.1l8l0&bav=on.2,or.r_gc.r_pw.&fp=3e26be485b327bc7&biw=1440&bih=760

paulo1911

olá amigo tente isso, veja:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;


public class Teste {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		File file=new File("D:\arquivo.txt"); //Path do seu areuivo Ex.: c:\arquivo.txt

		String strTmp=null;

		try {
			BufferedReader bf = new BufferedReader(new FileReader(file));
			while((strTmp=bf.readLine())!=null){

			    String[] linhaToArray=strTmp.split(Pattern.quote("|")); // precisa colocar o "|" pipe dentro do Patern, pois pode ser interpretado como caractere da regex

			     // Faz algo com a linha que foi convertida em array de Paths
			     for(String s:linhaToArray){

			       System.out.println(s);

			     }
			}
			bf.close();
		}catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

}

Espero ter ajudado
Fallow
Criado 3 de agosto de 2011
Ultima resposta 3 de ago. de 2011
Respostas 2
Participantes 3