2016-07-28 3 views
0

해결할 수없는 문제가 있습니다. 속성 파일을 읽어야하지만 올바른 경로를 설정할 수 없습니다. java.io.File에 대한 문서에서 src/...에서 설정해야한다고 말하고 있습니다. ... 현재 작동하지 않으며 같은 파일에서 경로를 만들었습니다.속성 파일의 올바른 경로를 설정하는 방법은 무엇입니까?

예외입니다 : FileNotFound

PropertyReader 클래스 : C에서

@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"; 

내가하고 있어요 경우 경로 : PropertyReader 사용

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; 
    } 
} 

그리고 ApplicationController.class : // .. 잘 작동합니다.

Project structure

당신을 주셔서 감사하고 좋은 하루 되세요!

+0

는 속성을 작성 했는가 자원 폴더 –

+0

에 파일을 넣어 당신' 파일 '개체 – Sanjeev

+0

파일 파일 = 새 파일 ("여기에 파일 경로"); –

답변

0

나는 주석 @PropertySource를 사용 @Value() 예를 들어

하여 해결 :

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

0

다음 예제를 사용하여 속성 파일을 읽으십시오.

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(); 
      } 
     } 
    } 

    } 
} 

또한 SpringMVC, JSF 및 Struts와 같은 프레임 워크에 따라 다릅니다. 이러한 모든 프레임 워크에는 속성 파일에 액세스하는 데 필요한 바로 가기가 있습니다.

+0

저는 SpringMVC를 사용하고 있습니다. 어쨌든 당신의 대답은 내 PropertyReader.class와 같습니다. –