본문 바로가기
java

Properties

by 신방동불주먹 2022. 10. 12.

<  Properties >

 

-  java.util.Properties 클래스

- 파일 입출력을 지원

- HashMap의 구버전인 Hashtables을 상속받았기 때문에 서브클래스 Map의 key와 values를 String 형태로 가짐

- String 형태로 get, set, put 메소드 제공

- 중복을 허용하지 않음

 

 

 

 

1. 선언

Properties prop = new Properties();

 

2. 데이터 삽입 : setProperty

prop.setProperty("myid", "1234");
prop.setProperty("qwer", "1244");
prop.setProperty("qwer", "1111");

 

3. 데이터 가져오기 : getProperty (입력 타입 그대로)

String value = prop.getProperty("myid");
System.out.println(value);

4. load(FileInputStream file) : 스트림으로 열린 Properties 파일 객체를 로드함

 

 

 

< 파일과 연결된 데이터 가져오기 >

Properties prop = new Properties();
		
//1.로딩작업(위치 알려주기) - 예외처리 해야함
		try {
			prop.load(new FileInputStream("input.txt"));
		}catch(IOException e) {
			System.out.println("지정한 파일이 없습니다.");
			System.exit(0); //프로그램 강제 종료 명령어
		}
	
String name = prop.getProperty("name"); //txt파일의 name의 문자열 값이 데이터로 넘어온다. //Hong Kil Dong
String[] data = prop.getProperty("data").split(","); //패턴값 지정해서 쪼개서 가져옴 -> String의 1차원 배열로 반환
  • load 메서드에 예외선언이 되어있기 때문에 반드시 예외처리 해주어야 한다
  • IOException : 파일을 찾지 못할 때 발생하는 오류.

 

 

 

 

 

 < Enumeration 인터페이스 >

- 데이터를 순회하여 가져온다

 

< 주요메서드 >

- hasMoreElements()  : boolean 타입을 반환(현재의 커서 이후에 요소들이 있는지 여부를 체크)

- nextElement()  : 값을 가져옴

Enumeration e = prop.propertyNames();
while(e.hasMoreElements()){  //true일때
	String element = (String)e.nextElement(); //String밖에 없어서 제네릭 필요없다. 
	String value = prop.getProperty(element);
	System.out.println(value);
}

 

 

<간단 연습문제>

//이름 max min sum 평균 구하기
		 
		int max = 0;
		int min = 0;
		int sum = 0;

		for(int i = 0; i < data.length;i++) {
			int value = Integer.parseInt(data[i]);
			if(i==0) {
				max = min = value;
			}
			
			if(max < value) {
				max = value;
			}else if(min>value) {
				min = value;
			}			
			sum += value;
		
			}
		
		
		
	/*		//min값 구하기 추가
			min = max;
			
			for(int i=0;i<data.length;i++) {
				int value = Integer.parseInt(data[i]);
				
				if(min>value) {
					min = value;
				}
			}

		System.out.println("이름 " + name + " min " + min+" max "+max+" sum "+sum+" 평균 "+(double)sum / data.length);
		
		}
	
}
  • 문자열 -> 숫자 변환 : Integer.parseInt(문자열)
  • 숫자 -> 문자 변환 : String.valueOf(숫자)

 

 

 

 

 

https://joooootopia.tistory.com/13


1. 컬렉션은 기본 데이터 형이 아닌 참조 데이터 형만 저장가능
2. 따라서 object 타입의 객체로서 저장된다.
3. 기본형은 wrapper 클래스를 이용하여 boxing시켜주거나, autoboxing으로 저장할 수 있다 

iterator 메소드 : hasNext(), next()
 (collection map 프레임웤, lsit, set, hs
list
set
- 인덱스로 객체를 검색해서 가져옴
******HashSet에서는 순서 없이, 동일한 객체의 중복 저장 없이 저장을 수행한다
따라서 add() 메소드를 사용하여 해당 HashSet에 이미 존재하고 있는 요소를 추가하려하면, 해당하는 요소를 바로 저장하지 않고 내부적으로 객체의 hashCode()메소드와 equals() 메소드를 호출하며 검사한다. 


- 인덱스가를 가져오는 메소드가 없음.
- 대신 전체 객체를 대상으로 한 번씩 다 가져오는 반복자 iterator를 제공한다
- set 인터페이스를 구현한 주요 클래스 : hash set, treeest, linkedhash set

hashset 기본생성자 : Set<E> set = new HashSet<>

map


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



…or create a new repository on the command line
echo "# wordgame1012" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/yourim0/wordgame1012.git
git push -u origin main

------
…or push an existing repository from the command line
git remote add origin https://github.com/yourim0/wordgame1012.git
git branch -M main
git push -u origin main


---repo 주소
https://github.com/yourim0/wordgame1012.git

'java' 카테고리의 다른 글

정규표현식  (0) 2023.02.26
Collection(Set)  (0) 2022.10.12
Collection(Map)  (0) 2022.10.12
Iterator (반복자 패턴)  (0) 2022.10.11
제네릭과 컬렉션  (0) 2022.10.11