일반적으로 Java 애플리케이션의 구성 매개변수는 속성 파일에 저장됩니다. Java 애플리케이션의 속성 파일은 속성이 확장자로 포함된 키-값 쌍을 기반으로 하는 일반 파일일 수도 있고
이 경우 Java 프로그램을 통해 이 두 가지 형식의 속성 파일을 출력하는 방법과 클래스 경로에서 이 두 가지 속성 파일을 로드하여 사용하는 방법을 소개합니다.
다음은 케이스 프로그램 코드입니다.
PropertyFilesUtil.java
package com.journaldev.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.Set; public class PropertyFilesUtil { public static void main(String[] args) throws IOException { String propertyFileName = "DB.properties"; String xmlFileName = "DB.xml"; writePropertyFile(propertyFileName, xmlFileName); readPropertyFile(propertyFileName, xmlFileName); readAllKeys(propertyFileName, xmlFileName); readPropertyFileFromClasspath(propertyFileName); } /** * read property file from classpath * @param propertyFileName * @throws IOException */ private static void readPropertyFileFromClasspath(String propertyFileName) throws IOException { Properties prop = new Properties(); prop.load(PropertyFilesUtil.class.getClassLoader().getResourceAsStream(propertyFileName)); System.out.println(propertyFileName +" loaded from Classpath::db.host = "+prop.getProperty("db.host")); System.out.println(propertyFileName +" loaded from Classpath::db.user = "+prop.getProperty("db.user")); System.out.println(propertyFileName +" loaded from Classpath::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(propertyFileName +" loaded from Classpath::XYZ = "+prop.getProperty("XYZ")); } /** * read all the keys from the given property files * @param propertyFileName * @param xmlFileName * @throws IOException */ private static void readAllKeys(String propertyFileName, String xmlFileName) throws IOException { System.out.println("Start of readAllKeys"); Properties prop = new Properties(); FileReader reader = new FileReader(propertyFileName); prop.load(reader); Set<Object> keys= prop.keySet(); for(Object obj : keys){ System.out.println(propertyFileName + ":: Key="+obj.toString()+"::value="+prop.getProperty(obj.toString())); } //loading xml file now, first clear existing properties prop.clear(); InputStream is = new FileInputStream(xmlFileName); prop.loadFromXML(is); keys= prop.keySet(); for(Object obj : keys){ System.out.println(xmlFileName + ":: Key="+obj.toString()+"::value="+prop.getProperty(obj.toString())); } //Now free all the resources is.close(); reader.close(); System.out.println("End of readAllKeys"); } /** * This method reads property files from file system * @param propertyFileName * @param xmlFileName * @throws IOException * @throws FileNotFoundException */ private static void readPropertyFile(String propertyFileName, String xmlFileName) throws FileNotFoundException, IOException { System.out.println("Start of readPropertyFile"); Properties prop = new Properties(); FileReader reader = new FileReader(propertyFileName); prop.load(reader); System.out.println(propertyFileName +"::db.host = "+prop.getProperty("db.host")); System.out.println(propertyFileName +"::db.user = "+prop.getProperty("db.user")); System.out.println(propertyFileName +"::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(propertyFileName +"::XYZ = "+prop.getProperty("XYZ")); //loading xml file now, first clear existing properties prop.clear(); InputStream is = new FileInputStream(xmlFileName); prop.loadFromXML(is); System.out.println(xmlFileName +"::db.host = "+prop.getProperty("db.host")); System.out.println(xmlFileName +"::db.user = "+prop.getProperty("db.user")); System.out.println(xmlFileName +"::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(xmlFileName +"::XYZ = "+prop.getProperty("XYZ")); //Now free all the resources is.close(); reader.close(); System.out.println("End of readPropertyFile"); } /** * This method writes Property files into file system in property file * and xml format * @param fileName * @throws IOException */ private static void writePropertyFile(String propertyFileName, String xmlFileName) throws IOException { System.out.println("Start of writePropertyFile"); Properties prop = new Properties(); prop.setProperty("db.host", "localhost"); prop.setProperty("db.user", "user"); prop.setProperty("db.pwd", "password"); prop.store(new FileWriter(propertyFileName), "DB Config file"); System.out.println(propertyFileName + " written successfully"); prop.storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file"); System.out.println(xmlFileName + " written successfully"); System.out.println("End of writePropertyFile"); } }
이 코드가 실행되면 writePropertyFile 메소드는 위 두 가지 형식의 속성 파일을 생성하고 해당 파일을 루트에 저장합니다. 프로젝트 디렉토리.
writePropertyFile 메소드에 의해 생성된 두 속성 파일의 내용:
DB.properties
#DB Config file#Fri Nov 16 11:16:37 PST 2012db.user=user db.host=localhost db.pwd=password
DB.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"><properties><comment>DB Config XML file</comment> <entry key="db.user">user</entry><entry key="db.host">localhost</entry><entry key="db.pwd">password</entry> </properties>
주석 요소는 우리가 prop.storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file");
코드를 조각할 때 두 번째 매개변수가 주석 내용에 전달됩니다. null이 전달되면 생성된 xml 속성 파일에 주석 요소가 없습니다.
콘솔 출력은 다음과 같습니다.
Start of writePropertyFile DB.properties written successfully DB.xml written successfully End of writePropertyFile Start of readPropertyFileDB.properties::db.host = localhostDB.properties::db.user = userDB.properties::db.pwd = passwordDB.properties::XYZ = nullDB.xml::db.host = localhostDB.xml::db.user = userDB.xml::db.pwd = passwordDB.xml::XYZ = null End of readPropertyFile Start of readAllKeysDB.properties:: Key=db.user::value=userDB.properties:: Key=db.host::value=localhostDB.properties:: Key=db.pwd::value=passwordDB.xml:: Key=db.user::value=userDB.xml:: Key=db.host::value=localhostDB.xml:: Key=db.pwd::value=password End of readAllKeys Exception in thread "main" java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434) at java.util.Properties.load0(Properties.java:353) at java.util.Properties.load(Properties.java:341) at com.journaldev.util.PropertyFilesUtil.readPropertyFileFromClasspath(PropertyFilesUtil.java:31) at com.journaldev.util.PropertyFilesUtil.main(PropertyFilesUtil.java:21)
여기서 널 포인터 예외가 보고됩니다. 그 이유는 생성된 파일이 프로젝트의 루트 디렉터리에 저장되며 읽을 때 다음에서 읽혀지기 때문입니다. 위의 생성된 파일은 두 개의 속성 파일을 src에 복사하고 프로그램을 다시 실행합니다.
일반적으로 Java 애플리케이션의 구성 매개변수는 속성 파일에 저장됩니다. Java 애플리케이션의 속성 파일은 속성이 확장자로 포함된 키-값 쌍을 기반으로 하는 일반 파일일 수도 있고 XML 파일일 수도 있습니다.
이 경우 Java 프로그램을 통해 이 두 가지 형식의 속성 파일을 출력하는 방법과 클래스 경로에서 이 두 가지 속성 파일을 로드하고 사용하는 방법을 소개합니다.
다음은 케이스 프로그램 코드입니다.
PropertyFilesUtil.java
package com.journaldev.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.Set; public class PropertyFilesUtil { public static void main(String[] args) throws IOException { String propertyFileName = "DB.properties"; String xmlFileName = "DB.xml"; writePropertyFile(propertyFileName, xmlFileName); readPropertyFile(propertyFileName, xmlFileName); readAllKeys(propertyFileName, xmlFileName); readPropertyFileFromClasspath(propertyFileName); } /** * read property file from classpath * @param propertyFileName * @throws IOException */ private static void readPropertyFileFromClasspath(String propertyFileName) throws IOException { Properties prop = new Properties(); prop.load(PropertyFilesUtil.class.getClassLoader().getResourceAsStream(propertyFileName)); System.out.println(propertyFileName +" loaded from Classpath::db.host = "+prop.getProperty("db.host")); System.out.println(propertyFileName +" loaded from Classpath::db.user = "+prop.getProperty("db.user")); System.out.println(propertyFileName +" loaded from Classpath::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(propertyFileName +" loaded from Classpath::XYZ = "+prop.getProperty("XYZ")); } /** * read all the keys from the given property files * @param propertyFileName * @param xmlFileName * @throws IOException */ private static void readAllKeys(String propertyFileName, String xmlFileName) throws IOException { System.out.println("Start of readAllKeys"); Properties prop = new Properties(); FileReader reader = new FileReader(propertyFileName); prop.load(reader); Set<Object> keys= prop.keySet(); for(Object obj : keys){ System.out.println(propertyFileName + ":: Key="+obj.toString()+"::value="+prop.getProperty(obj.toString())); } //loading xml file now, first clear existing properties prop.clear(); InputStream is = new FileInputStream(xmlFileName); prop.loadFromXML(is); keys= prop.keySet(); for(Object obj : keys){ System.out.println(xmlFileName + ":: Key="+obj.toString()+"::value="+prop.getProperty(obj.toString())); } //Now free all the resources is.close(); reader.close(); System.out.println("End of readAllKeys"); } /** * This method reads property files from file system * @param propertyFileName * @param xmlFileName * @throws IOException * @throws FileNotFoundException */ private static void readPropertyFile(String propertyFileName, String xmlFileName) throws FileNotFoundException, IOException { System.out.println("Start of readPropertyFile"); Properties prop = new Properties(); FileReader reader = new FileReader(propertyFileName); prop.load(reader); System.out.println(propertyFileName +"::db.host = "+prop.getProperty("db.host")); System.out.println(propertyFileName +"::db.user = "+prop.getProperty("db.user")); System.out.println(propertyFileName +"::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(propertyFileName +"::XYZ = "+prop.getProperty("XYZ")); //loading xml file now, first clear existing properties prop.clear(); InputStream is = new FileInputStream(xmlFileName); prop.loadFromXML(is); System.out.println(xmlFileName +"::db.host = "+prop.getProperty("db.host")); System.out.println(xmlFileName +"::db.user = "+prop.getProperty("db.user")); System.out.println(xmlFileName +"::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(xmlFileName +"::XYZ = "+prop.getProperty("XYZ")); //Now free all the resources is.close(); reader.close(); System.out.println("End of readPropertyFile"); } /** * This method writes Property files into file system in property file * and xml format * @param fileName * @throws IOException */ private static void writePropertyFile(String propertyFileName, String xmlFileName) throws IOException { System.out.println("Start of writePropertyFile"); Properties prop = new Properties(); prop.setProperty("db.host", "localhost"); prop.setProperty("db.user", "user"); prop.setProperty("db.pwd", "password"); prop.store(new FileWriter(propertyFileName), "DB Config file"); System.out.println(propertyFileName + " written successfully"); prop.storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file"); System.out.println(xmlFileName + " written successfully"); System.out.println("End of writePropertyFile"); } }
이 코드가 실행되면 writePropertyFile 메소드는 위 두 가지 형식의 속성 파일을 생성하고 해당 파일을 루트에 저장합니다. 프로젝트 디렉토리.
writePropertyFile 메소드에 의해 생성된 두 속성 파일의 내용:
DB.properties
#DB Config file#Fri Nov 16 11:16:37 PST 2012db.user=user db.host=localhost db.pwd=password
DB.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE properties SYSTEM " <properties><comment>DB Config XML file</comment><entry key="db.user">user</entry><entry key="db.host">localhost</entry> <entry key="db.pwd">password</entry></properties>
주석 요소는 우리가 prop.storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file");
코드를 조각할 때 두 번째 매개변수가 주석 내용에 전달됩니다. null이 전달되면 생성된 xml 속성 파일에 주석 요소가 없습니다.
콘솔 출력 내용은 다음과 같습니다.
Start of writePropertyFile DB.properties written successfully DB.xml written successfully End of writePropertyFile Start of readPropertyFileDB.properties::db.host = localhostDB.properties::db.user = userDB.properties::db.pwd = passwordDB.properties::XYZ = nullDB.xml::db.host = localhostDB.xml::db.user = userDB.xml::db.pwd = passwordDB.xml::XYZ = null End of readPropertyFile Start of readAllKeysDB.properties:: Key=db.user::value=userDB.properties:: Key=db.host::value=localhostDB.properties:: Key=db.pwd::value=passwordDB.xml:: Key=db.user::value=userDB.xml:: Key=db.host::value=localhostDB.xml:: Key=db.pwd::value=password End of readAllKeys Exception in thread "main" java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434) at java.util.Properties.load0(Properties.java:353) at java.util.Properties.load(Properties.java:341) at com.journaldev.util.PropertyFilesUtil.readPropertyFileFromClasspath(PropertyFilesUtil.java:31) at com.journaldev.util.PropertyFilesUtil.main(PropertyFilesUtil.java:21)
위는 Java&Xml Tutorial(10) XML을 속성 파일로 담은 내용입니다. 더 많은 관련 내용은 PHP 중국어 홈페이지(www.kr)를 참고해주세요. .php.cn)!

XML은 RSS에서 구조화 된 데이터, 확장 성, 크로스 플랫폼 호환성 및 구문 분석 검증의 장점을 가지고 있습니다. 1) 구조화 된 데이터는 컨텐츠의 일관성과 신뢰성을 보장합니다. 2) 확장 성은 콘텐츠 요구에 맞게 맞춤형 태그를 추가 할 수 있습니다. 3) 크로스 플랫폼 호환성은 다른 장치에서 원활하게 작동합니다. 4) 분석 및 검증 도구는 피드의 품질과 무결성을 보장합니다.

XML에서 RSS 구현은 구조화 된 XML 형식을 통해 컨텐츠를 구성하는 것입니다. 1) RSS는 채널 정보 및 프로젝트 목록과 같은 요소를 포함하여 XML을 데이터 교환 형식으로 사용합니다. 2) RSS 파일을 생성 할 때는 사양에 따라 컨텐츠를 구성하고 구독을 위해 서버에 게시해야합니다. 3) RSS 파일은 리더 또는 플러그인을 통해 구독하여 컨텐츠를 자동으로 업데이트 할 수 있습니다.

RSS의 고급 기능에는 컨텐츠 네임 스페이스, 확장 모듈 및 조건부 구독이 포함됩니다. 1) 컨텐츠 네임 스페이스는 RSS 기능을 확장합니다. 2) 메타 데이터를 추가하기 위해 Dublincore 또는 iTunes와 같은 확장 된 모듈, 3) 특정 조건에 따라 조건부 구독 필터 항목. 이러한 기능은 XML 요소 및 속성을 추가하여 정보 수집 효율성을 향상시켜 구현됩니다.

rssfeedsusexmltostructurecontentupdates.1) xmlprovideahierarchicalstructurefordata.2) the ElementDefinesThefeed 'sidentityandContainsElements.3) elementsreent indindividualcontentpieces.4) rssisextensible, 허용 Bestpracticesin

RSS 및 XML은 웹 컨텐츠 관리를위한 도구입니다. RSS는 컨텐츠를 게시하고 구독하는 데 사용되며 XML은 데이터를 저장하고 전송하는 데 사용됩니다. 컨텐츠 게시, 구독 및 업데이트 푸시와 함께 작동합니다. 사용의 예로는 RSS 게시 블로그 게시물 및 XML 저장 도서 정보가 있습니다.

RSS 문서는 자주 업데이트되는 콘텐츠를 게시하고 구독하는 데 사용되는 XML 기반 구조 파일입니다. 주요 기능에는 1) 자동화 된 컨텐츠 업데이트, 2) 컨텐츠 집계 및 3) 브라우징 효율 향상이 포함됩니다. RSSFEED를 통해 사용자는 적시에 다른 소스에서 최신 정보를 구독하고 얻을 수 있습니다.

RSS의 XML 구조에는 다음이 포함됩니다. 1. XML 선언 및 RSS 버전, 2. 채널 (채널), 3. 항목. 이러한 부분은 RSS 파일의 기초를 형성하여 사용자가 XML 데이터를 구문 분석하여 컨텐츠 정보를 얻고 처리 할 수 있도록합니다.

rssfeedsUsexMlTOSYNDICATECONTENT; parsingTheMinVolvesRoadingXML, NavigatingItsStructure 및 extractingData.ApplicationSaggregatorsAngAggeratsAndTrackingPodCastePisOdes.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

Dreamweaver Mac版
시각적 웹 개발 도구

DVWA
DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는
