iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >java 读写 ini 配置文件的示例代码
  • 746
分享到

java 读写 ini 配置文件的示例代码

2024-04-02 19:04:59 746人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

下面通过代码先看下java 读写 ini 配置文件,代码如下所示: package org.fh.util; import java.io.BufferedReader; impo

下面通过代码先看下java 读写 ini 配置文件,代码如下所示:


package org.fh.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IniFileUtil {
    
    public static String readCfgValue(String file, String section, String variable, String defaultValue)
            throws IOException {
        String strLine, value = "";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(URLDecoder.decode(file, "UTF-8"))); //解决中文路径乱码
        boolean isInSection = false;
        try {
            while ((strLine = bufferedReader.readLine()) != null) {
                strLine = strLine.trim();
                strLine = strLine.split("[;]")[0];
                Pattern p;
                Matcher m;
                p = Pattern.compile("\\[\\w+]");// Pattern.compile("file://[//s*.*//s*//]");
                m = p.matcher((strLine));
                if (m.matches()) {
                    p = Pattern.compile("\\[" + section + "\\]");// Pattern.compile("file://[//s*" + section +
                                                                    // "file://s*//]");
                    m = p.matcher(strLine);
                    if (m.matches()) {
                        isInSection = true;
                    } else {
                        isInSection = false;
                    }
                }
                if (isInSection == true) {
                    strLine = strLine.trim();
                    String[] strArray = strLine.split("=");
                    if (strArray.length == 1) {
                        value = strArray[0].trim();
                        if (value.equalsIgnoreCase(variable)) {
                            value = "";
                            return value;
                        }
                    } else if (strArray.length == 2) {
                        value = strArray[0].trim();
                        if (value.equalsIgnoreCase(variable)) {
                            value = strArray[1].trim();
                            return value;
                        }
                    } else if (strArray.length > 2) {
                        value = strArray[0].trim();
                        if (value.equalsIgnoreCase(variable)) {
                            value = strLine.substring(strLine.indexOf("=") + 1).trim();
                            return value;
                        }
                    }
                }
            }
        } finally {
            bufferedReader.close();
        }
        return defaultValue;
    }
    
    public static boolean writeCfgValue(String file, String section, String variable, String value) throws IOException {
        String fileContent, allLine, strLine, newLine;
        String getValue = null;
        BufferedReader bufferedReader = new BufferedReader(new FileReader(URLDecoder.decode(file, "UTF-8"))); //解决中文路径乱码
        boolean isInSection = false;
        boolean canAdd = true;
        fileContent = "";
        try {
            while ((allLine = bufferedReader.readLine()) != null) {
                allLine = allLine.trim();
                strLine = allLine.split(";")[0];
                Pattern p;
                Matcher m;
                p = Pattern.compile("\\[\\w+]");
                m = p.matcher((strLine));
                if (m.matches()) {
                    p = Pattern.compile("\\[" + section + "\\]");
                    m = p.matcher(strLine);
                    if (m.matches()) {
                        isInSection = true;
                    } else {
                        isInSection = false;
                    }
                }
                if (isInSection == true) {
                    strLine = strLine.trim();
                    String[] strArray = strLine.split("=");
                    getValue = strArray[0].trim();
                    if (getValue.equalsIgnoreCase(variable)) {
                        newLine = getValue + "=" + value;
                        fileContent += newLine;
                        while ((allLine = bufferedReader.readLine()) != null) {
                            fileContent += "\r\n" + allLine;
                        }
                        bufferedReader.close();
                        canAdd = false;
                        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false));
                        bufferedWriter.write(fileContent);
                        bufferedWriter.flush();
                        bufferedWriter.close();
                        return true;
                    }
                }
                fileContent += allLine + "\r\n";
            }
            if (canAdd) {
                String str = variable + "=" + value;
                fileContent += str + "\r\n";
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false));
                bufferedWriter.write(fileContent);
                bufferedWriter.flush();
                bufferedWriter.close();
            }
        } catch (IOException ex) {
            throw ex;
        } finally {
            bufferedReader.close();
        }
        return false;
    }
    public static void main(String[] args) {
        try {
            
            String value = IniFileUtil.readCfgValue("L:/a.ini", "Client", "devNum", "1");
            System.out.println(value);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}

补充:下面看下java 读取ini配置文件

Maven项目中的pom.xml中配置:


<dependency>
    <groupId>org.ini4j</groupId>
    <artifactId>ini4j</artifactId>
    <version>0.5.4</version>
</dependency>

env.ini文件:


[dev]
url="dev-url"
user="dev-user"
passWord="dev-password"

[testing]
url=""
user=""
password=""

代码:


import org.ini4j.Ini;
import org.ini4j.Profile;
import org.ini4j.Wini;
import java.io.File;
import java.util.Map;
import java.util.Set;
public class IniUtils {
    public static void main(String[] args) {
        try {
            readIni();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
    private static void readIni() throws Exception{
        Wini ini = new Wini(new File("yourPath/env.ini"));
        // read
        Ini.Section section = ini.get("dev");
        String url = section.get("url");
        String user = section.get("user");
        String password = section.get("password");
        System.out.println(url);
        System.out.println(user);
        System.out.println(password);
        // or just use java.util.Map interface
        Map<String, String> map = ini.get("dev");
        String url1 = map.get("url");
        String user1 = map.get("user");
        String password1 = map.get("password");
        System.out.println(url1);
        System.out.println(user1);
        System.out.println(password1);
        // get all section names
        // Set<String> sectionNames = ini.keySet();
        // for(String sectionName: sectionNames) {
        //   Profile.Section section1 = ini.get(sectionName);
           // }
        // write
        ini.put("sleepy", "age", 55);
        ini.put("sleepy", "weight", 45.6);
        ini.store();
    }
}

参考:

Http://ini4j.sourceforge.net/tutorial/OneMinuteTutorial.java.html
http://ini4j.sourceforge.net/tutorial/IniTutorial.java.html

到此这篇关于java 读写 ini 配置文件的文章就介绍到这了,更多相关java 读写 ini文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: java 读写 ini 配置文件的示例代码

本文链接: https://www.lsjlt.com/news/162069.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • java 读写 ini 配置文件的示例代码
    下面通过代码先看下java 读写 ini 配置文件,代码如下所示: package org.fh.util; import java.io.BufferedReader; impo...
    99+
    2024-04-02
  • java读写ini配置文件的示例代码怎么编写
    本篇文章为大家展示了java读写ini配置文件的示例代码怎么编写,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。下面通过代码先看下java 读写 ini 配置文件,代码如下所示:package&nbs...
    99+
    2023-06-26
  • C++实现读写ini配置文件的示例代码
    目录1.概述2.ini格式语法3.配置读取4.demo示例5.自动生成读取代码1.概述 配置文件的读取是每个程序必备的功能,配置文件的格式多种多样,例如:ini格式、json格式、x...
    99+
    2023-05-19
    C++读写ini配置文件 C++读写ini文件 C++ ini文件
  • C++实现ini文件读写的示例代码
    目录介绍1.使用INIReader.h头文件1.INIReader.h2.test.ini3.INIReaderTest.cpp2.使用ini.h头文件1.ini.h2.config...
    99+
    2024-04-02
  • Java代码读取properties配置文件的示例代码
    目录读取properties配置文件新手引导PropertiesConcurrentHashMapstaticInputStreamtry...cache...finallyIOEx...
    99+
    2023-05-18
    Java读取properties文件 Java读取properties
  • Python中ini配置文件读写的实现
    导入模块 import configparser # py3 写入 config = configparser.ConfigParser() config["DEFAULT"] ...
    99+
    2024-04-02
  • QT中如何读写ini配置文件
    如图1所示,我们需要在QT界面中实现手动读取参数存放的位置,那么我们该如何做呢? 方法:读取ini格式的配置文件,实现路径的写入与读取。 第一步:界面构造函数中,初始化一个Conf...
    99+
    2024-04-02
  • QT中怎么读写ini配置文件
    这篇文章主要介绍“QT中怎么读写ini配置文件”,在日常操作中,相信很多人在QT中怎么读写ini配置文件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”QT中怎么读写ini配置文件”的疑惑有所帮助!接下来,请跟...
    99+
    2023-06-21
  • python读取/写入配置文件ini方法
    在写测试脚本时,经常有一些需要变动的数据,可以单独放在ini文件里,然后读取传递给 相应的函数,这样程序操作更灵活。具体的方法介绍如下: 文件结构: Cofig.ini内容:[test1]ip = 10.10.10.10 [test2]po...
    99+
    2023-01-31
    配置文件 方法 python
  • Python3 读取 ini 配置文件(
    【背景】  Windows 的记事本会给 UTF-8 文件添加 BOM 头,很烦,搞个通用的读取配置文件的代码。可能报这种错误:configparser.MissingSectionHeaderError: File contains no...
    99+
    2023-01-31
    配置文件 ini
  • Python ini配置文件示例详解
    目录INI介绍关于configparserINI文件格式读取配置文件写入配置文件总结INI介绍 INI是英文“初始化”(initialization)的缩写,...
    99+
    2024-04-02
  • java读写ini文件、FileOutputStream问题
    目录java读写ini文件、FileOutputStream使用properties.set()方法存值new FileOutputStream 的位置有关系吗?总结java读写in...
    99+
    2023-05-15
    java读写ini文件 java FileOutputStream java ini文件
  • GO语言ini配置文件的读取操作示例分析
    小编给大家分享一下GO语言ini配置文件的读取操作示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!go适合做什么go是golang的简称,而golang可以...
    99+
    2023-06-15
  • Python常用配置文件ini、json、yaml读写总结
    本文参考文章,出于学习目的,写本文。 开发项目时,为了维护一些经常需要变更的数据,比如数据库的连接信息、请求的url、测试数据等,需要将这些数据写入配置文件,将数据和代码分离,只需要...
    99+
    2024-04-02
  • Java对xls文件进行读写操作示例代码
    前言本文主要给大家介绍的是关于Java对xls文件进行读写操作的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍: win7_x64 IDEAJava读写xls文件,使用库jxl.jar读写xls文件,这里是在知道...
    99+
    2023-05-31
    java 读写xls文件
  • Java代码实现对properties文件有序的读写的示例
    最近遇到一项需求,要求把properties文件中的内容读取出来供用户修改,修改完后需要再重新保存到properties文件中。很简单的需求吧,可问题是Properties是继承自HashTable的,直接通过keySet()、keys()...
    99+
    2023-05-30
    properties 有序 ava
  • python如何读取ini配置文件
    Python提供了一个标准库`configparser`用于读取和修改INI文件。首先,需要导入`configparser`模块:`...
    99+
    2023-10-08
    python
  • 如何实现Python中ini配置文件读写操作
    这篇文章将为大家详细讲解有关如何实现Python中ini配置文件读写操作,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。导入模块import configparser # py...
    99+
    2023-06-29
  • 通过python读取ini配置文件
    ini是啥你可以理解为就是一个配置文件的统称吧。比如test.conf,这样的你可以理解为他就是ini文件,里面一般存放一些配置信息。比如数据库的基本信息,一会我们进行讲解!那么ta的好处是啥呢?就是把一些配置信息提出去来进行单独管理,如果...
    99+
    2023-01-31
    配置文件 python ini
  • PHP读取和写入CSV文件的示例代码
    目录1. 什么是 CSV 文件2. 从 CSV 文件中读取数据3. 将数据写入 CSV 文件1. 什么是 CSV 文件 CSV(逗号分隔值)文件是使用逗号分隔信息的文本文件。该文件的...
    99+
    2023-05-15
    PHP读取CSV文件 PHP写入CSV文件 PHP CSV文件 PHP CSV
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作