读写ini配置文件|如何读写ini配置文件 java

『壹』 unity3d如何读取和写ini配置文件或者其他类型的配置文件

这种个人认为其实都是文本文件,只不过格式不一样了。很遗憾配置文件我用得很低端,我自己用的大多就是文件流(简单粗暴)。对于INI,我似乎只能给出以下方法:[System.Runtime.InteropServices.DllImport("kernel32")]private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);[System.Runtime.InteropServices.DllImport("kernel32")]private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);//读写示例方法void demo(){StringBuilder demos = new StringBuilder (255);WritePrivateProfileString ("section", "name", "theDemo", @"D:\theDemoini.ini");GetPrivateProfileString ("section" , "name" ,"" ,demos,255 , @"D:\theDemoini.ini");print (demos);}

『贰』 java中如何设置读取ini配置文件

/* * IniReader.java * 用Java读取INI文件(带section的) * 示例: * IniReader reader = new IniReader("E:\\james\\win.ini"); * out.println(reader.getValue("TestSect3", "kkk 6")); */ package tmp; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Properties; public class IniReader { protected HashMap sections = new HashMap(); private transient String currentSecion; private transient Properties current; public IniReader(String filename) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(filename)); read(reader); reader.close(); } protected void read(BufferedReader reader) throws IOException { String line; while ((line = reader.readLine()) != null) { parseLine(line); } } protected void parseLine(String line) { line = line.trim(); if (line.matches("\\[.*\\]")) { currentSecion = line.replaceFirst("\\[(.*)\\]", "$1"); current = new Properties(); sections.put(currentSecion, current); } else if (line.matches(".*=.*")) { if (current != null) { int i = line.indexOf('='); String name = line.substring(0, i); String value = line.substring(i + 1); current.setProperty(name, value); } } } public String getValue(String section, String name) { Properties p = (Properties) sections.get(section); if (p == null) { return null; } String value = p.getProperty(name); return value; } }ini文件如下: [TestSect1] kkk 1=VVVVVVVVVVV1 kkk 2=VVVVVVVVVVV2 [TestSect2] kkk 3=VVVVVVVVVVV3 kkk 4=VVVVVVVVVVV4 [TestSect3] kkk 5=VVVVVVVVVVV5 kkk 6=VVVVVVVVVVV6

『叁』 如何用VB读取ini配置文件

为了方便用户使用和使系统具有灵活性,大多数Win-dows应用程序将用户所做的选择以及各种变化的系统信息记录在初始化(INI)文件中。因此,当系统的环境发生变化时,可以直接修改INI文件,而无需修改程序。由此可见,INI文件对系统功能是至关重要的。本文将介绍采用VisualBasicforWindows(下称VB)开发Windows应用程序时如何读写INI文件。INI文件是文本文件,由若干部分(section)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键词(keyword)和一个等号,每个关键词会控制应用程序某个功能的工作方式,等号右边的值(value)指定关键词的操作方式。其一般形式如下:[section1]keyword1=valuelkeyword2=value2……[section2]keyword1=value1keyword2=value2……其中,如果等号右边无任何内容(即value为空),那就表示Windows应用程序已为该关键词指定了缺省值,如果在整个文件中找不到某个关键词(或整个一部分),那同样表示为它们指定了缺省值。各个部分所出现的顺序是无关紧要的,在每一个部分里,各个关键词的顺序同样也无关紧要。读写INI文件通常有两种方式:一是在Windows中用"记事本"(Notepad)对其进行编辑,比较简单,无需赘述;二是由Windows应用程序读写INI文件,通常是应用程序运行时读取INI文件中的信息,退出应用程序时保存用户对运行环境的某些修改。关键词的值的类型多为字符串或整数型,应分两种情况读写。为了使程序具有可维护性和可移植性,最好把对INI文件的读写封装在一个模块(RWINI.BAS)中,在RWI-NI.BAS中构造GetIniS和GetIniN函数以及SetIniS和Se-tIniN过程,在这些函数和过程中需要使用WindowsAPI的"GetPrivateprofileString"、"GetPrivateProfileInt"和"WritePrivateProfileString"函数。RWINI.BAS模块的程序代码如下:在General-Declearation部分中声明使用到的WindowsAPI函数:Declare Function GetprivateprofileString Lib"Ker-nel"(ByVallpAppName As String,ByVallpKeyName As String,ByVallpDefault As String,ByVal lpRetrm-String As String,ByVal cbReturnString As Integer,ByVal Filename As String)As IntegerDeclare FunctionGetPrivatePfileInt Lib "Kernel"(ByVal lpAppName As String,ByVal lpKeyName As String,ByVal lpDefault As Integer,ByVal Filename As String)As IntegerDeclare Lib "Kernel"(ByVal lpApplicationName As String,ByVal lpKeyName As String,ByVal lpString As String,ByVal lplFileName As String)As IntegerFunction GetIniS(ByVal SectionName As String,ByVal KeyWord As String,ByVal DefString As String)As StringDim ResultString As String * 144,Temp As IntegerDims As String,i As IntegerTemp%=GetPrivateProfileString(SectionName,KeyWord,"",ResultString,144,AppProfileName())‘检索关键词的值IfTemp%>0Then‘关键词的值不为空s=""Fori=1To144IfAsc(Mid$(ResultString,I,1))=0ThenExitForElses=s&Mid$(ResultString,I,1)EndIfNextElseTemp%=WritePrivateProfilesString(sectionname,KeyWord,DefString,ppProfileName())‘将缺省值写入INI文件s=DefStringEndIfGetIniS=sEndFunctionFunctionGetIniN(ByValSectionNameAsString,ByValKeyWordAsString,ByValDefValueAsIneger)AsIntegerDimdAsLong,sAsStringd=DefValueGetIniN=GetPrivateProfileInt(SectionName,KeyWord,DefValue,ppProfileName())Ifd<>DefValueThens=""&dd=WritePrivateProfileString(SectionName,KeyWord,s,AppProfileName())EndIfEndFunctionSubSetIniS(ByValSectionNameAsString,BtVaKeyWordAsString,ByValValStrAsString)Dimres%res%=WritePrivateprofileString(SectionName,KeyWord,ValStr,AppProfileName())EndSubSubSetIniN(ByValSectionNameAsString,ByValKeyWordAsString,ByValValIntAsInteger)Dimres%,s$s$=Str$(ValInt)res%=WriteprivateProfileString(SectionName,KeyWord,s$,AppProfileName())EndSubSectionName为每一部分的标题,KeyWord为关键词,GetIniS和GetIniN中的DefValue为关键词的缺省值,SetIniS和SetIniN的ValStr和ValInt为要写入INI文件的关键词的值。为了能更好地说明如何使用以上函数和过程,下面举两个实例。实例1:开发应用程序通常要使用数据库和其它一些文件,这些文件的目录(包括路径和文件名)不应在程序中固定,而是保存在INI文件中,程序运行时由INI文件中读入。读入数据库文件的代码如下:DimDatabasenameAsStringDatabasename=GetIniS("数据库","职工","")IfDatabaseName=""ThenDatabaseName=InputBox("请输入数据库《职工》的目录"),App.Title)’也可通过"文件对话框"进行选择OnErrorResumeNextSetdb=OpenDatabas(DatabaseName)IfErr<>0ThenMsgBox"打开数据库失败!",MB-ICONSTOP,App.Title:GotoErrorProcessingElseSetIniS"数据库","职工",DatabaseNameEndIfOnErrorGoTo0……实例2:为了方便用户操作,有时需要保存用户界面的某些信息,例如窗口的高度和宽度等。装载窗体时,从INI文件中读入窗体高度和宽度,卸载窗体时将窗体当前高度和宽度存入INI文件,代码如下:Sub Form1_Load()……Forml.Height=GetIniN("窗体1","高度",6000)Form1.Width=GetIniN("窗体1","高度",4500)EndSub……Sub Form1_Unload()……SetIniN"窗体1","高度",Me.HeightSetIniN"窗体1,"宽度",Me.Width……End Sub

『肆』 读写INI文件怎么指定INI文件的位置在自己程序

123456789101112131415161718192021222324252627282930313233343536373839404142importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.InputStreamReader;publicclassIniFileReader{/***读取文件制定行*@paramfilePath文件路径*@paramkey文件中配置项的key如:path=C:/aaa中的"path="(注意空格,以具体配置项为准)

『伍』 VB读写INI文件

'API函数的声明'ini配置文件Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _ ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As LongPrivate Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, _ ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As LongPrivate Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _ ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long '自定义函数'读取/设置ini配置文件Private Function GetIniS(FilePath As String, SectionName As String, KeyWord As String, DefString As String) As String Dim ResultString As String * 255, Temp As Long Dim s As String, i As Integer Temp = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 255, FilePath) If Temp > 0 Then '关键词的值不为空 s = "" For i = 1 To 255 If Asc(Mid(ResultString, i, 1)) = 0 Then Exit For Else s = s & Mid(ResultString, i, 1) End If Next i Else If DefString <> "" Then '将缺省值写入INI文件 Temp = WritePrivateProfileString(SectionName, KeyWord, DefString, FilePath) s = DefString End If End If GetIniS = sEnd FunctionPrivate Function GetIniN(FilePath As String, SectionName As String, KeyWord As String, DefValue As Long) As Long Dim d As Long, s As String d = DefValue GetIniN = GetPrivateProfileInt(SectionName, KeyWord, DefValue, FilePath) 'If d <> DefValue Then ' s = CStr(d) ' d = WritePrivateProfileString(SectionName, KeyWord, s, FilePath) 'End IfEnd FunctionPrivate Sub SetIniS(FilePath As String, SectionName As String, KeyWord As String, ValStr As String) Dim res As Long res = WritePrivateProfileString(SectionName, KeyWord, ValStr, FilePath)End SubPrivate Sub SetIniN(FilePath As String, SectionName As String, KeyWord As String, ValLon As Long) Dim res As Long, s As String s = CStr(ValLon) res = WritePrivateProfileString(SectionName, KeyWord, s, FilePath)End Sub'读取过程Private Sub Command1_Click() Dim fname As String Dim SectionName As String, KeyWord As String, DefString As String, DefValue As Long Dim s As String, sValue As String Dim i As Integer, j As Integer Dim Result() As String fname = "……" '你所要读取的文件(包括其绝对路径) SectionName = "KeyName" '字段 KeyWord = "Key" '相同的部分 DefString = "???" '一般为空字符,我这里是用于区分Value1为空是是否继续进行用的 i = 0 j = -1 Do i = i + 1 s = KeyWord & CStr(i) sValue = GetIniS(fname, SectionName, KeyWord, DefString) If sValue <> DefString Then j = j + 1 ReDim Preserve Result(j) Result(j) = sValue Else Exit Do End If Loop End Sub你再结合自己的具体情况,修改下就行了

『陆』 如何读写ini配置文件 java

//读取一般的属性文件FileInputStreamfin=newFileInputStream("my.ini");//打开文件Propertiesprops=newProperties();//建立属性回类props.load(fin);//读入答文件fin.close();

『柒』 如何使用Python3读写INI配置文件

python读取ini配置需要用到configparser包,所以要先加载它。importconfigparser之后我们需要载入配置文件。config=configparser.configparser()#ipconfig.ini可以是一个不存在的文件,意味着准备新建配置文件。config.read("ipconfig.ini")

『捌』 VB读写配置INI文件

用INI文件的读写函数比较好 Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long Private Function WriteIniFileString(StandKey As String, keyName As String, keyValue As String, FileName) As Long '写INI文件函数 Dim leninikey As Long Dim strkey As String * 255 WriteIniFileString = WritePrivateProfileString(StandKey, keyName, keyValue, FileName) End Function Private Function GetIniFileString(StandKey As String, keyName As String, Default As String, FileName As String) As String '读取INI文件函数 Dim leninikey As Long Dim strkey As String * 255 leninikey = GetPrivateProfileString(StandKey, keyName, Default, strkey, Len(strkey), FileName)

『玖』 c#读取ini配置文件

在作应用系统开发时,管理配置是必不可少的。例如数据库服务器的配置、安装和更新配置等等。由于Xml的兴起,现在的配置文件大都是以xml文档来存储。比如Visual Studio.Net自身的配置文件Mashine.config,Asp.Net的配置文件Web.Config,包括我在介绍Remoting中提到的配置文件,都是xml的格式。传统的配置文件ini已有被xml文件逐步代替的趋势,但对于简单的配置,ini文件还是有用武之地的。ini文件其实就是一个文本文件,它有固定的格式,节Section的名字用[]括起来,然后换行说明key的值:[section]key=value如数据库服务器配置文件:DBServer.ini[Server]Name=localhost[DB]Name=NorthWind[User]Name=sa在C#中,对配置文件的读写是通过API函数来完成的,代码很简单:using System;using System.Text;using System.IO;using System.Runtime.InteropServices;namespace PubOp{public class OperateIniFile{#region API函数声明[DllImport("kernel32")]//返回0表示失败,非0为成功private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);[DllImport("kernel32")]//返回取得字符串缓冲区的长度private static extern long GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);#endregion#region 读Ini文件public static string ReadIniData(string Section,string Key,string NoText,string iniFilePath){if(File.Exists(iniFilePath)){StringBuilder temp = new StringBuilder(1024);GetPrivateProfileString(Section,Key,NoText,temp,1024,iniFilePath);return temp.ToString();}else{return String.Empty;}}#endregion#region 写Ini文件public static bool WriteIniData(string Section,string Key,string Value,string iniFilePath){if(File.Exists(iniFilePath)){long OpStation = WritePrivateProfileString(Section,Key,Value,iniFilePath);if(OpStation == 0){return false;}else{return true;}}else{return false;}}#endregion}}简单说明以下方法WriteIniData()和ReadIniData()的参数。Section参数、Key参数和IniFilePath不用再说,Value参数表明key的值,而这里的NoText对应API函数的def参数,它的值由用户指定,是当在配置文件中没有找到具体的Value时,就用NoText的值来代替


赞 (0)