2013-02-22 3 views
1

다음 코드를 통해 속성 파일의 값을 업데이트하려고합니다.속성 파일을 읽고 저장하는 중

import java.io. *; 가져 오기 java.util.Properties;

public class Sample { 
    public static void main(String a[]) throws IOException { 
     InputStream is = Sample.class.getClassLoader().getResourceAsStream("myfile.properties"); 
     Properties p = new Properties(); 
     p.load(is); 

     p.setProperty("myProperty", "updated"); 

     OutputStream os = new FileOutputStream("myfile.properties"); 
     p.store(os, "update"); 
     os.close(); 
     System.out.print(p.getProperty("myProperty")); 
    } 
} 

출력 :

를 업데이트하지만 값을 업데이 트하지 않는 것 같습니다. 사실, 속성이나 파일 자체가 없어도 오류가 발생하지 않습니다.

+0

이 등록 정보 파일은 JAR에서 buldled입니까? –

+0

아니요, 저는 그것들 모두를 별도의 디렉토리에 넣었습니다. – sweetcode

답변

1
// Read properties file. 
Properties prop = new Properties(); 

try { 
    prop.load(new FileInputStream("filename.properties")); 
} catch (IOException e) { 
} 

// Write properties file. 
try { 
    prop.store(new FileOutputStream("filename.properties"), null); 
} catch (IOException e) { 
} 
+0

위와 같이 코드를 변경했습니다. 이제 문제는 속성 파일이 업데이트되지만 프로그램이 끝날 때까지 새로 고쳐지지 않는다는 것입니다. 따라서 나중에 같은 프로그램에서 업데이트 된 값을 검색 할 수 없습니다. – sweetcode

관련 문제