数据存入读取txt文件。。。。。。

use_zj 2011-03-25 04:36:17



按上面一样存入txt ,
读取的时候读一行输入放入数组中。
...全文
356 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
用分隔符拼成一条作为数组的一个元素,用的时候拆分
或者用一个N*3的数组,一条纪录分别存入一个数组中
huangweizhao 2011-03-30
  • 打赏
  • 举报
回复
读取再加上
sr.Close()
huangweizhao 2011-03-30
  • 打赏
  • 举报
回复

Private Sub Write_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Write.Click
Dim dataLength As Integer = 8 '每个元素的长度
Dim strPad As String = Chr(32).ToString() '填充时用的字符,这里是空格
Dim strSplit As String = Chr(9).ToString() '分隔每个元素的字符,这里是tab建
If File.Exists("C:\123.txt") Then
File.Delete("C:\123.txt")
End If
Dim sw As New StreamWriter("C:\123.txt", True)
Dim strLine As String = ""
strLine = LPad("x", strPad, dataLength) + strSplit + LPad("y", strPad, dataLength) + strSplit + LPad("z", strPad, dataLength)
sw.WriteLine(strLine)
strLine = LPad("3455", strPad, dataLength) + strSplit + LPad("34", strPad, dataLength) + strSplit + LPad("3212", strPad, dataLength)
sw.WriteLine(strLine)
strLine = LPad("56", strPad, dataLength) + strSplit + LPad("4", strPad, dataLength) + strSplit + LPad("2", strPad, dataLength)
sw.WriteLine(strLine)
sw.Flush()
sw.Close()
End Sub

Private Function LPad(ByVal strSrc As String, ByVal strPad As Char, ByVal intLength As Integer) As String
LPad = ""
If strSrc.Length > intLength Then
strSrc = strSrc.Substring(0, intLength)
End If

LPad = strSrc
While LPad.Length < intLength
LPad = strPad.ToString() + LPad
End While
End Function

Private Sub Read_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Read.Click
If Not File.Exists("C:\123.txt") Then
MsgBox("文件不存在")
End If

Dim sr As New StreamReader("C:\123.txt")
Dim strLine As String = ""
While Not sr.EndOfStream
'如果想取strline中的各个元素,可以用split(strSplit)来分割
strLine = sr.ReadLine().ToString()
MessageBox.Show(strLine)
End While
End Sub
wolftop 2011-03-28
  • 打赏
  • 举报
回复
Delimited columns
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\txtFilesFolder\;Extended Properties="text;HDR=Yes;FMT=Delimited";
The delimiter can be specified in the registry at the following location:
HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Jet \ 4.0 \ Engines \ Text
"Format" = "TabDelimited"
or
"Format" = "Delimited(;)"
use_zj 2011-03-28
  • 打赏
  • 举报
回复
有没有关于保存方面的 要我这个格式保存
music_0000 2011-03-28
  • 打赏
  • 举报
回复
奇怪,为什么不用xml,多好的技术啊
叶子 2011-03-26
  • 打赏
  • 举报
回复
参考一:
http://zhidao.baidu.com/question/108081383.html

参考二:
http://sunxubo44.blog.163.com/blog/static/17124006200772091024718/
Tosp2012 2011-03-25
  • 打赏
  • 举报
回复
存入时加分隔符,之后读取时split获取字符。
机器人 2011-03-25
  • 打赏
  • 举报
回复
数组 -> 文件: System.IO.File.WriteAllLines
文件 -> 数组: System.IO.File.ReadAllLines
ljch7499 2011-03-25
  • 打赏
  • 举报
回复
数据不长的话用这个方便:
dim s as string =My.Computer.FileSystem.ReadAllText("filename")
然后
dim ss as string = s.Split(Chr(13))(0)
取得第一行数据
dim sss as string =ss.split(chr(10))(0)
取得第一组数据
(chr(13)是回车,chr(10)是tab键
沐NeMo 2011-03-25
  • 打赏
  • 举报
回复
參考:http://topic.csdn.net/u/20110324/12/d4e41ad7-3973-4013-a44d-49f0e9dacd72.html
'另外一种方式:
Dim sr As StreamReader
sr = File.OpenText("aa.txt")
Dim str As String = sr.ReadToEnd()
Dim lines As String() = str.Split(ControlChars.Lf)
For Each item As string In lines
Dim array As String() = item.Split("="C)
'按=号拆分字符串
Dim ctlName As String = array(0)
'文本框名称
Dim text As String = array(1)
'文本框内容
Next

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧