注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沉默蜂B4A安卓编程

国内资料最全的B4A资料库

 
 
 

日志

 
 

一个可读写的INI类  

2014-04-30 22:24:25|  分类: B.文件 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

来源《Tutorial Text files》32楼
http://www.basic4ppc.com/android/forum/threads/text-files.6690/

 

——————————————  下面是类模拟代码  ——————————————

 

' Code module
' Subs in this code module will be accessible from all modules.
' ###################################################################################
' Functions to Read and Write ini-file from Activity

' ini.writeini(File.DirDefaultExternal,"test.ini","Section 1","TestKey1","Sunday")
' ini.writeini(File.DirDefaultExternal,"test.ini","Section 1","TestKey2","Monday")
' ini.writeini(File.DirDefaultExternal,"test.ini","Section 2","TestKey1","Tuesday")
' ini.writeini(File.DirDefaultExternal,"test.ini","Section 2","TestKey2","Wednesday")
' Creates this
'
' (File  test.ini)
' [Section 1]
' TestKey1=Sunday
' TestKey2=Monday
' [Section 2]
' TestKey1=Tuesday
' TestKey2=Wednesday
'
' To read 1 of above Keys into variable 'str'
' str = ini.readini(File.DirDefaultExternal,"test.ini","Section 1","TestKey1")
' str will be set to Sunday
' #####################################################################################
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Reader As TextReader
Dim section As String
Dim str As String
Dim str2 As String
Dim l As List
Dim l2 As List
l.Initialize
l2.Initialize
End Sub
Sub loadini(fpath As String, f As String)
l.Clear
l2.Clear
section = ""
If File.Exists(fpath, f) Then
 Reader.Initialize(File.OpenInput(fpath, f))
 Dim line As String
 line = Reader.ReadLine
 Do While line <> Null 
  If line.StartsWith("[") Then
   section = line.Replace("[","")
   section = section.Replace("]","")
  Else
   If section <> "" Then
    l.Add(section & "=" & line)
   End If
  End If
  line = Reader.ReadLine
 Loop
 Reader.Close
End If
l.Sort(True)
section = ""
End Sub
Sub readini(fpath As String,f As String,sections As String,keys As String)
loadini(fpath,f)
str = ""
str2 = sections & "=" & keys & "="
For i = 0 To (l.Size - 1)
 str = l.Get(i)
 If str.StartsWith(str2) Then
  splitit(str)
  splitit(str2)
  DoEvents
  Return str2
 End If
Next
Return ""
End Sub
Sub writeini(fpath As String, f As String, Sections As String,keys As String,value As String)
loadini(fpath,f)
Dim replace As Int
replace = 0
str = ""
str2 = Sections & "=" & keys & "="
For i = 0 To (l.Size - 1)
 str = l.Get(i)
 If str.StartsWith(str2) Then
  l.RemoveAt(i)
  l.Add(str2 & value)
  replace = 1
 End If
Next
If replace = 0 Then
 l.Add(str2 & value)
Else
 replace = 0
End If
l.Sort(True)
Dim Writer As TextWriter
Writer.Initialize(File.OpenOutput(fpath, f, False))
Dim laststr As String
Dim fullstr As String
fullstr = ""
For i = 0 To l.Size - 1
 splitit(l.Get(i))
 If str <> laststr Then
  l2.Add("[" & str & "]")
  l2.Add(str2)
 Else
  l2.Add(str2)
 End If
 laststr = str
Next
Writer.WriteList(l2)
Writer.Close
End Sub
Sub splitit(inp As String)
str = ""
str2 = ""
If inp.IndexOf("=") > 0 Then
 str = inp.SubString2(0,inp.IndexOf("="))
 str2 = inp.SubString(inp.IndexOf("=")+ 1)
End If
End Sub

  评论这张
 
阅读(274)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017