「java操作ini文件」java ini

博主:adminadmin 2022-11-25 21:07:07 60

今天给各位分享java操作ini文件的知识,其中也会对java ini进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java 向ini配置文件中写入值。

Java读取和修改ini配置文件,参考如下:

/*

* ConfigurationFile.java

*

*/

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

* 这是个配置文档操作类,用来读取和配置ini配置文档

* @author 由月

* @version 2004-08-18

* @修改 2008-05-22

*/

public final class ConfigurationFile {

/**

* 从ini配置文档中读取变量的值

* @param file 配置文档的路径

* @param section 要获取的变量所在段名称

* @param variable 要获取的变量名称

* @param defaultValue 变量名称不存在时的默认值

* @return 变量的值

* @throws IOException 抛出文档操作可能出现的io异常

*/

public static String getProfileString(

String file,

String section,

String variable,

String defaultValue)

throws IOException {

String strLine, value = "";

BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

boolean isInSection = false;

try {

while ((strLine = bufferedReader.readLine()) != null) {

strLine = strLine.trim();

//strLine = strLine.split("[;]")[0];

Pattern p;

Matcher m;

p = Pattern.compile("\\["+section+"\\]");

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("=");

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;

}

Java中如何设置读取ini配置文件?

// 读取一般的属性文件

FileInputStream fin=new FileInputStream("my.ini"); // 打开文件

Properties props=new Properties();                 // 建立属性类

props.load(fin);                                   // 读入文件

fin.close();                                       // 关闭文件

java读取ini文件,读取出来的文件少了很多字符,比如,字符串"String"读取出来变成"Sri"这个样子

为什么不一行一行的读

public static void main(String[] args) {

String path = "C:\\Windows";

int count = 0;

File file = new File(path);

File[] fileList = file.listFiles();

BufferedReader inputStream;

for (File f : fileList) {

if (f.getPath().toUpperCase().endsWith("INI")) {

count++;

System.out.println("\n" + f.getPath());

System.out.println("***************************************");

try {

inputStream = new BufferedReader(new FileReader(f));

String line = null;

while ((line = inputStream.readLine()) != null) {

System.out.print(line);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

System.out.println(count);

}

java怎么通过读取ini文件来确定要输去文件的路径

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;

public class IniFileReader 

{

    /**

     * 读取文件制定行

     * @param filePath 文件路径

     * @param key 文件中配置项的key 如:path = C:/aaa 中的"path = "(注意空格,以具体配置项为准)

     * @return String 制定的配置项的配置数据 如:path = C:/aaa 中的"C:/aaa"

     * @throws Exception

     */

    public static String readerSpcifyLine(String filePath,String key) throws Exception

    {

     StringBuffer result = new StringBuffer();

    

     FileInputStream fis = new FileInputStream(new File(filePath));

     BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

     String line = "";

     for(line = reader.readLine(); (line != null  !"".equals(line)); line = reader.readLine())

     {

     if(line.startsWith(key))

     {

     result.append(line.substring(line.indexOf(key) + key.length(), line.length()));

     break;

     }

     }

     fis.close();

     reader.close();

     return result.toString();

    }

    

    public static void main(String[] args) throws Exception

    {

     String filePath = "F:/document/path.ini";

     String key = "path = ";

     String configFilePath = readerSpcifyLine(filePath,key);

     System.out.println(configFilePath);

    }

}

上面这个类应该能帮到你,上面有一个文件读取方法和一个main测试函数,可以读出你所说的配置项的信息,方法的说明看代码注释。

java读取ini文件导入到mysql数据库中

用properties,先load那个.ini文件然后 迭代这个properties赋给jdbc里sql的占位符 然后执行语句就可以了,propertise是map的子类直接用keyset迭代就好了;

关于java操作ini文件和java ini的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。