XML (파일 생성하기)

이민경·2024년 3월 19일

JDBC

목록 보기
2/2

📘 XML(eXtensible Markup Language)

단순화된 데이터 기술 형식

  • XML에 저장되는 데이터 형식은 모두 Key : Value 형식이고, (HTML과 같이..?!)
    Key , Value 모두 String(문자열) 형식이다.

XML 파일을 읽고, 쓰기 위한 IO 관련 클래스 필요한데..! Properties 컬렉션 객체가 필요하다.

📖 Properties 컬렉션 객체

  • Map 후손 클래스
  • Key, Value 모두 String(문자열)
  • XML 파일을 읽고, 쓰는데 특화된 메서드 제공

👩‍💻 그렇다면 xml 파일을 생성해보자!

try {
	Scanner sc = new Scanner(System.in);
			
	// ✔ Properties 객체 생성
			Properties prop = new Properties();
		
			System.out.print("생성할 파일 이름 : ");
			String fileName = sc.next();
			
	// ✔ FileOutputStream 생성 
			FileOutputStream fos = new FileOutputStream(fileName + ".xml");
			
			
	// ✔ Properties 객체를 이용해서 XML 파일 생성
			prop.storeToXML(fos, ".xml file!!!");
	
	 System.out.println(fileName+ ".xml 파일 생성 완료");
			
}catch (Exception e) {
			e.printStackTrace();
}

XML file 생성 시 Exception이 발생하기에 try~catch문 안에 작성해주면 된다! 🙋‍♀️

profile
풀스택 개발자

0개의 댓글