Ler arquivo .properties mo mesmo pacote

2 respostas
aorocha
Pessoa tenho um arquivo properties no mesmo pacote que a classe, e dessa classe quero ler esse arquivo, estou tentando assim, mas não consigo, alguem pode ajudar?
package br.com.maringa.cliente.action;

import java.io.*;
import java.util.Properties;
import br.com.maringa.action.*;

public class Cliente extends SystemAction {
	
	public Object getModel() {
		return null;
	}
	
	public String execute() {
		
		File file = new File("cliente.propreties");    
		Properties props = new Properties();
		FileInputStream fis = null;
		try {
		    fis = new FileInputStream(file);
		    //lê os dados que estão no arquivo
		    props.load(fis);
		    fis.close();
		}
		catch (IOException ex) {
		    System.out.println(ex.getMessage());
		    System.out.println(file.getPath());
		    System.out.println(file.getAbsolutePath());
		    System.out.println(file.getAbsoluteFile());
		    ex.printStackTrace();
		}
		
		return SUCCESS;
	}

2 Respostas

Pedrosa

Use assim:

public static Properties carregarConfiguracoes() {
		Properties prop = new Properties();
		InputStream is = null;
		try {
			is = SuaClasse.class.getResourceAsStream("seu.properties");
			prop.load(is);

		} catch (Exception e) {
			e.getMessage();
		}

		return prop;
}
Mauricio_Linhares

Faça:

InputStream stream = this.getClass().getResourceAsStream("cliente.properties");
Properties props = new Properties();
props.load(stream);

Você também colocou o nome do arquivo errado no seu código.

Criado 28 de abril de 2006
Ultima resposta 28 de abr. de 2006
Respostas 2
Participantes 3