在文本文件中的特定位置附加文本 [英] Append Text at a Particular Position in Text File

查看:100
本文介绍了在文本文件中的特定位置附加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个文本文件(.txt).其结构如下所示

1. qwerty ^^ saasd ^^^ adadasd ^^
2. asdasdas ^^ adasdasd ^^^ asasdasd ^^
3. weqweqwew ^^^ dasdadasd ^^^ asdasd ^^^

它会相应地继续
我想通过文本框传递文本
并希望将其附加到
的最后一个位置 第一行.

让要传递的文本为1234567

这样我的新TextFile应该如下所示

1. qwerty ^^ saasd ^^^ adadasd ^^ 1234567
2. asdasdas ^^ adasdasd ^^^ asasdasd ^^
3. weqweqwew ^^^ dasdadasd ^^^ asdasd ^^^

我已经尝试过

  Dim 文件路径 As  字符串 = " 
         Dim  strFileName  As  字符串 =文件路径

         lines() As  字符串
        行数= File.ReadAllLines(Filepath)
        ' 将cim as Integer = lines.Count 
        '  MsgBox(cnt)
         Dim  newFile( 65535 ) As  字符串
        newFile( 0 )=线路( 0 )& " 
        File.WriteAllLines(" ,newFile)



F26Q2V1.txt是原始txt文件,F26Q2V1c.txt是附加后的结果文件. 但我在F26Q2V1c.txt中得到结果


1. qwerty ^^ saasd ^^^ adadasd ^^ 1234567

其余行将被删除
但我也希望所有的台词.
需要帮助

解决方案

这里不需要其他数组.

简单地:

  Dim 文件路径 As  字符串 = " 
 Dim  strFileName  As  字符串 =文件路径

 lines() As  字符串
行数= File.ReadAllLines(Filepath)
lines( 0 )= 字符串.Concat(lines( 0 )," )
File.WriteAllLines(" ,行)



或者,如果您想要另一个数组:

  Dim 文件路径 As  字符串 = " 
 Dim  strFileName  As  字符串 =文件路径
 
 lines() As  字符串
行数= File.ReadAllLines(Filepath)
 Dim 计数 As  整数 =行数.
 Dim  newFile(count) As   String  = Array.Copy(行, 0 ,计数)
newFile( 0 )= 字符串.Concat(newFile( 0 )," )
File.WriteAllLines(" ,newFile)


在这里看看:
http://msdn.microsoft.com/en-us/library/3zc0w663.aspx [ ^ ]
http://www.homeandlearn.co.uk/net/nets8p5.html [ ^ ]

看来您想将数据写入ini文件.
使用VB.NET的IniFile类 [Dim Filepath As String = "E:\F26Q2V1.txt" Dim strFileName As String = Filepath Dim lines() As String lines = File.ReadAllLines(Filepath) 'Dim cnt As Integer = lines.Count 'MsgBox(cnt) Dim newFile(65535) As String newFile(0) = lines(0) & "1234567" File.WriteAllLines("E:\F26Q2V1c.txt", newFile)



F26Q2V1.txt is the Original txt file and F26Q2V1c.txt is the result file after appending..
but I am getting the result in F26Q2V1c.txt
as

1. qwerty^^saasd^^^adadasd^^1234567

the rest of the lines are getting deleted
but i want all the lines also.
Help needed

解决方案

There''s no need to have another array here.

Simply :

Dim Filepath As String = "E:\F26Q2V1.txt"
Dim strFileName As String = Filepath

Dim lines() As String
lines = File.ReadAllLines(Filepath)
lines(0) = string.Concat(lines(0), "1234567")
File.WriteAllLines("E:\F26Q2V1c.txt", lines)



Or, if you want another array :

Dim Filepath As String = "E:\F26Q2V1.txt"
Dim strFileName As String = Filepath
 
Dim lines() As String
lines = File.ReadAllLines(Filepath)
Dim count As Integer = lines.Count
Dim newFile(count) As String = Array.Copy(lines, 0, count)
newFile(0) = string.Concat(newFile(0), "1234567")
File.WriteAllLines("E:\F26Q2V1c.txt", newFile)


Take a look here:
http://msdn.microsoft.com/en-us/library/3zc0w663.aspx[^]
http://www.homeandlearn.co.uk/net/nets8p5.html[^]

It looks like you want to write data into ini file.
IniFile Class using VB.NET[^]


这篇关于在文本文件中的特定位置附加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆