2016-07-28 22 views
0

Ich habe ein Problem, das ich nicht lösen kann. Ich muss die Eigenschaftendatei lesen, aber den richtigen Pfad nicht festlegen. Dokumentation von java.io.File sagt, dass ich in der src/... setzen muss Es funktioniert nicht, ich habe einen Pfad von der aktuellen Datei und habe das gleiche Problem.Wie setze ich den richtigen Pfad der Eigenschaftendatei?

AUSNAHME IST: FileNotFound

PropertyReader Klasse:

public final class PropertyReader { 

    private Properties prop = new Properties(); 
    private InputStream input = null; 

    public Properties getProperties(File file) { 
     try { 
      input = new FileInputStream(file); 
      // load a properties file 
      prop.load(input); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (null != input) { 
       try { 
        input.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     return prop; 
    } 
} 

Und ApplicationController.class die PropertyReader verwendet:

@RequestMapping(value = "/result", method = RequestMethod.GET) 
public String resultPage(ModelMap model) { 
    //Getting property with key "path" 
    model.addAttribute("path", new PropertyReader().getProperties(file).getProperty("path")); 
    return "result"; 

Wenn ich Stellweg von C: //.. es funktioniert gut.

Project structure

Vielen Dank viel und hat einen schönen Tag!

+0

Legen Sie die Eigenschaften in Ressourcen-Ordner-Datei –

+0

Wie Sie erstellt haben Ihre ' Dateiobjekt – Sanjeev

+0

Datei file = new File ("hier Pfad zur Datei"); –

Antwort

0

ich gelöst es durch Anmerkungen @PropertySource und @Value mit()

Zum Beispiel:

//There could be any folder @PropertySource("classpath:file.properties") public class AnyClass { //There could be any property @Value("${some.property}") private String someValue; }

0

Versuchen Sie folgendes Beispiel zum Lesen der Eigenschaftendatei zu verwenden.

import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Properties; 

public class App { 
    public static void main(String[] args) { 

    Properties prop = new Properties(); 
    InputStream input = null; 

    try { 

     input = new FileInputStream("config.properties"); 

     // load a properties file 
     prop.load(input); 

     // get the property value and print it out 
     System.out.println(prop.getProperty("mysqldb")); 
     System.out.println(prop.getProperty("dbuser")); 
     System.out.println(prop.getProperty("dbpassword")); 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } finally { 
     if (input != null) { 
      try { 
       input.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    } 
} 

Sein hängt auch von dem Rahmen, die Sie wie SpringMVC, JSF und Struts verwenden. Alle diese Frameworks verfügen über eigene Verknüpfungsmöglichkeiten für den Zugriff auf Eigenschaftendateien.

+0

finden Ich benutze SpringMVC, sowieso Ihre Antwort die gleiche wie meine PropertyReader.class. –