📄 configurationfile.java
字号:
package PUB;
import java.io.*;
import java.io.FileOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 这是个配置文件操作类,用来读取和设置ini配置文件
* @author 由月
* @version 2004-08-18
*/
public final class ConfigurationFile
{
private static String dir;
private static String filename;
private static String file;
public ConfigurationFile(String d, String f)
{
dir = d;
filename = f;
file = dir + filename;
try
{
if(!(new File(dir).isDirectory()))
{
new File(dir).mkdir();
}
}
catch(SecurityException e)
{
System.out.println("can not make directory");
}
try
{
File myFile = new File(file);
if (!myFile.exists())
{
FileOutputStream fileOut = new FileOutputStream(file);
//write the string to the file as a byte array
//fileOut.write(file.getBytes(),0,file.length());
fileOut.close();
}
}
catch (Exception f2) {}
}
/**
* 从ini配置文件中读取变量的值
* @param file 配置文件的路径
* @param section 要获取的变量所在段名称
* @param variable 要获取的变量名称
* @param defaultValue 变量名称不存在时的默认值
* @return 变量的值
* @throws IOException 抛出文件操作可能出现的io异常
*/
public static String getProfileString(
String variable,
String defaultValue)
throws IOException {
String allLine,strLine, newLine;
String getValue;
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
boolean haveclose = false;
try {
while ( (allLine = bufferedReader.readLine()) != null)
{
strLine = allLine.trim();
String[] strArray = strLine.split("=");
getValue = strArray[0].trim();
if (strLine.length()-strArray[0].length()>1 && getValue.equalsIgnoreCase(variable))
{
String value = strArray[1];
if(value==null) value = "";
if(value.length()>0)value = value.trim();
bufferedReader.close();
haveclose = true;
return value;
// out.println("ok");
}
}//while(alline=null)
}catch(IOException ex){
throw ex;
} finally {
bufferedReader.close();
}
if(!haveclose) bufferedReader.close();
return defaultValue;
}
/**
* 修改ini配置文件中变量的值
* @param file 配置文件的路径
* @param section 要修改的变量所在段名称
* @param variable 要修改的变量名称
* @param value 变量的新值
* @throws IOException 抛出文件操作可能出现的io异常
*/
public static boolean setProfileString(
String variable,
String value)
throws IOException
{
String file =dir +filename;
String fileContent, allLine,strLine, newLine;
String getValue;
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
fileContent = "";
boolean havedo = false;
try {
while ( (allLine = bufferedReader.readLine()) != null)
{
strLine = allLine.trim();
String[] strArray = strLine.split("=");
getValue = strArray[0].trim();
if (getValue.equalsIgnoreCase(variable))
{
//out.println(" getValue="+getValue);
newLine = getValue + " = " + value ;
fileContent += newLine + "\r\n";
havedo = true;
}
else
{
fileContent += allLine + "\r\n";
}
}//while(alline=null)
if(!havedo)
{
newLine = variable + " = " + value ;
fileContent += newLine ;
havedo = true;
}
bufferedReader.close();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false));
bufferedWriter.write(fileContent);
bufferedWriter.flush();
bufferedWriter.close();
if(havedo) return true;
}catch(IOException ex){
throw ex;
} finally {
bufferedReader.close();
}
return false;
}
/**
* 程序测试
*/
/*
public static void main(String[] args) {
//String value = Config.getProfileString("sysconfig.ini", "Option", "OracleDB", "default");
//System.out.println(value);
try {
System.out.println(ConfigurationFile.setProfileString("d:/1.ini", "Settings", "SampSize", "111"));
} catch (IOException e) {
System.out.println(e.toString());
}
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -