Erro no reader?

9 respostas
paulinhohd
FileReader reader = new FileReader("C://arquivo.txt");
					BufferedReader leitor = new BufferedReader(reader);
					
					String linha = null;
					while((linha = leitor.readLine())) {
					    System.out.println("Linha: " + linha);
					}
					leitor.close();
					reader.close();

Pessoal, estou usando o fonte acima que copiei do tutorial daqui, porém mesmo eu colocando a biblioteca java.io.* ocorre o seguinte erro:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from String to boolean

at gui.Tela_Pesq.getBPesquisar(Tela_Pesq.java:83)
at gui.Tela_Pesq.getJContentPane(Tela_Pesq.java:49)
at gui.Tela_Pesq.getJFrame(Tela_Pesq.java:30)
at gui.Tela_Principal$2.actionPerformed(Tela_Principal.java:100)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Alguém pode me ajudar?
Valeu 8)

9 Respostas

X

Na linha:

while((linha = leitor.readLine())) {

É uma string, mas ela deve ser um boolean…

Isso pode te ajudar:

while((linha = leitor.readLine())!=null) {

ai vc verifica se a linha realmente existe, enquanto existir ele faz o loop

abraço

paulinhohd
while((linha = leitor.readLine())!=null) {

Colocando o que me falou piorou a situação veja o erro:

Exception in thread “AWT-EventQueue-0” java.lang.Error: Unresolved compilation problems:
Unhandled exception type FileNotFoundException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException

at gui.Tela_Pesq.getBPesquisar(Tela_Pesq.java:79)
at gui.Tela_Pesq.getJContentPane(Tela_Pesq.java:49)
at gui.Tela_Pesq.getJFrame(Tela_Pesq.java:30)
at gui.Tela_Principal$2.actionPerformed(Tela_Principal.java:100)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
X

Agora funcionou!
hehe

Olha a primeira linha do trace:
FileNotFoundException

Porém, ele não encontrou o arquivo

verifica se ele existe (arquivo.txt)

Voce pode tratar o erro com um try catch, ai caso ocorra esse erro( FileNotFoundException) ele pode criar o arquivo…

abraço

paulinhohd

Como ficaria o código usando o try catch?

Por que o arquvio está lá bonitinho e não acha!

X

Tira uma barra do "C://arquivo.txt"

deixa: "C:/arquivo.txt" ou "C:\\arquivo.txt"

COm try catch ficaria +- assim:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class asd {
	public static void main(String args[]){
		File fl = new File("C:/arquivo.txt");
		BufferedReader leitor=null;
		FileReader reader=null;
		try {
			reader = new FileReader(fl);
		
			 leitor = new BufferedReader(reader);
		
		String linha = null;
		while((linha = leitor.readLine())!=null) {
		    System.out.println("Linha: " + linha);
		}
		
		
		} catch (FileNotFoundException e) {//caso nao encontre o arquivo ele entra neste bloco
			try {
                               System.out.println("Foi criado um novo arquivo!");
				fl.createNewFile();
			} catch (IOException e1) {
				
				e1.printStackTrace();
			}
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}finally{
			try {
				leitor.close();
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}
paulinhohd

Cara é incrível:

Coloquei este código no action de um botão:
public void actionPerformed(java.awt.event.ActionEvent e) {
					
					FileReader reader = null;
					BufferedReader leitor = null;
					
					try{
						reader = new FileReader("C://arquivo.txt");					
						leitor = new BufferedReader(reader);
					
						String linha = null;
						System.out.println("Vou entrar no While");
						while((linha = leitor.readLine())!= null){
							System.out.println("Dentro do While");
							System.out.println("Linha: " + linha);
						}
					}catch (IOException ex) {			               
			            ex.printStackTrace();
			            System.out.println("Erro no Exception EX");
			        }finally{   
			            try {   
			                leitor.close();   
			                reader.close();   
			            } catch (IOException ey) {   
			            	ey.printStackTrace();
			            	System.out.println("Erro no Exception EY");
			            }   			               
			        }					
				}
			});

E ele não entra no While para ler nem com reza brava...se eu tirar o != null não funciona.
Ele entra no try e imprimi o "Vou entrar no While"..

8)

T

Não é para duplicar o “//”. O nome do arquivo é “c:\arquivo.txt” ou “c:/arquivo.txt”.

paulinhohd

Mesmo assim, já havia tentado mudar…
O Tanto de vezes que clico neste botão é o tanto que aparece “Vou entrar no While” mas nunca entra…
E o arquivo existe bonitinho no C:\ com dados dele normal.

:cry:

paulinhohd

Deu certo aqui, valeu pessoal!
Era uma barra invertida na geração do arquivo! :lol:

Criado 11 de agosto de 2008
Ultima resposta 11 de ago. de 2008
Respostas 9
Participantes 3