properties 파일 설정 데이터
                                            
 3668 단어  properties매개 변수config
                    
WEB-INF에서 config를 설정합니다.properties 파일, 키 값이 맞는 형식으로 존재합니다.
UserName=name
Password=123
  그 다음에 자바 파일에서 설정 파일의 값을 읽습니다
예를 들어 저희가 파라메터 유틸 파일이 하나 있어요.
public class ParameterUtil{
	public static String getPara(String para) {
		String path = ParameterUtil.class.getResource("/").getPath(); 
		path = path.substring(1, path.indexOf("classes"));
		path = path + "config.properties";
		Properties property = PropertyUtil.getProperties(path);
		
		return property.getProperty(para);
	}
}
  주의: 위에서 생성한 경로에는 스페이스 바가 있을 수 없습니다. 따라서 파일 경로는 스페이스 바를 제거해야 합니다. 예를 들어tomcat 6는 안 되고, 중간의 스페이스 바는 삭제해야 합니다.
Property Util은 다음과 같습니다.
public class PropertyUtil {
	public static String readData(String path, String key) {
		Properties props = new Properties();
		InputStream in = null;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(path);
			in = new BufferedInputStream(fis);
			props.load(in);
			fis.close();
			in.close();
			String value = props.getProperty(key);
			props =null;
			System.gc();
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static void writeData(String path, String key, String value) {
		Properties prop = new Properties();
		try {
			File file = new File(path);
			if (!file.exists())
				file.createNewFile();
			InputStream fis = new FileInputStream(file);
			prop.load(fis);
			fis.close();
			OutputStream fos = new FileOutputStream(path);
			prop.setProperty(key, value);
			prop.store(fos, "Update '" + key + "' value");
			fos.close();
			prop = null;
			System.gc();
		} catch (IOException e) {
			System.err.println("Visit " + path + " for updating " + value
					+ " value error");
		}
	}
	
	public static Map<String, String> getMap(String path) {
		Properties props = new Properties();
		InputStream in = null;
		FileInputStream fis = null;
		Map<String, String> map = null;
		try {
			fis = new FileInputStream(path);
			in = new BufferedInputStream(fis);
			props.load(in);
			fis.close();
			in.close();
			Set<Object> set = props.keySet();
			map = new HashMap<String, String>();
			for (Object o : set) {
				String tempKey = o.toString();
				String value = props.getProperty(tempKey);
				map.put(tempKey, value);
			}
			props = null;
			System.gc();
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Properties getProperties(String path) {
		Properties props = new Properties();
		InputStream in = null;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(path);
			in = new BufferedInputStream(fis);
			props.load(in);
			fis.close();
			in.close();
			System.gc();
			return props;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
  호출 방법은 다음과 같습니다. ParameterUtil.getPara("UserName")
이렇게 하면 매개 변수 설정을 완성하여 바꾸기가 편리하다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Spring] properties 파일에 정의된 값 가져오기실시간 강의 수업 중 Admin key에 관련된 이야기가 나와 1,2차 Python, Flask 프로젝트에서 DB등 보완이 필요한 값들에 대해서 다른 곳에 따로 저장하고 변수에는 Path 설정해주고 github에 올...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.