What is a configuration file in Java
Configuration file names in Java generally end with ".properties" and ".xml" , the structure of these configuration files is the same as Java's HashMap structure. Its function is to change the parameters in the code by modifying the configuration file, thereby achieving flexible parameter changes.
properties usage
driver=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/user user=root password=123456
/** * 读取config.properties文件中的内容,放到Properties类中 * @param filePath 文件路径 * @param key 配置文件中的key * @return 返回key对应的value */ public static String readConfigFiles(String filePath,String key) { Properties prop = new Properties(); try{ InputStream inputStream = new FileInputStream(filePath); prop.load(inputStream); inputStream.close(); return prop.getProperty(key); }catch (Exception e) { e.printStackTrace(); System.out.println("未找到相关配置文件"); return null; } }
xml usage
<?xml version="1.0" encoding="utf-8" ?> <class> <student> <firstname>cxx1</firstname> <lastname>Bob1</lastname> <nickname>stars1</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>cxx2</firstname> <lastname>Bob2</lastname> <nickname>stars2</nickname> <marks>85</marks> </student> <student rollno="593"> <firstname>cxx3</firstname> <lastname>Bob3</lastname> <nickname>stars3</nickname> <marks>85</marks> </student> </class>
package com.cxx.xml; import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; /** * @Author: cxx * Dom操作xml * @Date: 2018/5/29 20:19 */ public class DomDemo { //用Element方式 public static void element(NodeList list){ for (int i = 0; i <list.getLength() ; i++) { Element element = (Element) list.item(i); NodeList childNodes = element.getChildNodes(); for (int j = 0; j <childNodes.getLength() ; j++) { if (childNodes.item(j).getNodeType()==Node.ELEMENT_NODE) { //获取节点 System.out.print(childNodes.item(j).getNodeName() + ":"); //获取节点值 System.out.println(childNodes.item(j).getFirstChild().getNodeValue()); } } } } public static void node(NodeList list){ for (int i = 0; i <list.getLength() ; i++) { Node node = list.item(i); NodeList childNodes = node.getChildNodes(); for (int j = 0; j <childNodes.getLength() ; j++) { if (childNodes.item(j).getNodeType()==Node.ELEMENT_NODE) { System.out.print(childNodes.item(j).getNodeName() + ":"); System.out.println(childNodes.item(j).getFirstChild().getNodeValue()); } } } } public static void main(String[] args) { //1.创建DocumentBuilderFactory对象 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //2.创建DocumentBuilder对象 try { DocumentBuilder builder = factory.newDocumentBuilder(); Document d = builder.parse("src/main/resources/demo.xml"); NodeList sList = d.getElementsByTagName("student"); //element(sList); node(sList); } catch (Exception e) { e.printStackTrace(); } } }
Recommended tutorial:《JavaTutorial》
The above is the detailed content of What is a configuration file in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor