C# 替换文本文件中的某一行 (要求此文件存在)

     /// <summary>
/// LineIndex 表示新的内容所在的行位置
/// </summary>
/// <param name="sFileName"></param>
/// <param name="sOrigionContent"></param>
/// <param name="sNewLineContent"></param>
/// <param name="LineIndex"></param>
public static void SaveTxtReplaceOneLine(string sFileName, string sOrigionContent, string sNewLineContent, int iNewLineIndex)
{
try
{
StringBuilder sbNewSave = new StringBuilder();
sFileName = sFileName.Replace("|", "_");
string sFolder = Directory.GetParent(sFileName).ToString();
if (!System.IO.Directory.Exists(sFolder))
{
System.IO.Directory.CreateDirectory(sFolder);
} if (!System.IO.File.Exists(sFileName))
{
throw new Exception("file not exist");
} string[] arrLine = sOrigionContent.Trim().Split(Convert.ToChar(Keys.Return));
for (int i = ; i < iNewLineIndex; i++)
{
sbNewSave.AppendLine(arrLine[i].Trim('\r').Trim('\n'));
}
sbNewSave.AppendLine(sNewLineContent);
for (int i = iNewLineIndex + ; i < arrLine.Length; i++)
{
sbNewSave.AppendLine(arrLine[i].Trim('\r').Trim('\n'));
} FileStream fsTxtWrite = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
StreamWriter srWrite = new StreamWriter(fsTxtWrite, System.Text.Encoding.UTF8);
//StreamWriter srWrite = new StreamWriter(fsTxtWrite, System.Text.Encoding.Default); srWrite.Write(sbNewSave.ToString().Trim()); srWrite.Close();
srWrite.Dispose();
fsTxtWrite.Dispose(); }
catch (Exception ex)
{
//MessageBox.Show(ex.Message); #region ### error info
try
{
string sFolder = Directory.GetParent(sFileName).ToString();
string sErrFullName = sFolder + "_err_" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
if (!System.IO.File.Exists(sErrFullName))
{
FileStream fs;
fs = File.Create(sErrFullName); StreamWriter srWrite = new StreamWriter(fs, System.Text.Encoding.UTF8); srWrite.Write(sFileName); srWrite.Close();
srWrite.Dispose(); fs.Close();
}
}
catch (Exception ex2)
{ }
#endregion ###error }
}

C# 替换文本文件中的某一行的更多相关文章

  1. 【Shell】使用sed命令替换文件中的某一行

    原始文件内容 -bash-3.2# cat configTest.xml <?xml version="1.0" encoding="UTF-8"?> ...

  2. 替换文本:将文本文件中的所有src替换为dst

    题意: 将文本文件中的所有src替换为dst 方法一:使用String import java.io.File; import java.io.FileNotFoundException; impor ...

  3. 使用 sed 命令查找和替换文件中的字符串的 16 个示例

    当你在使用文本文件时,很可能需要查找和替换文件中的字符串.sed 命令主要用于替换一个文件中的文本.在 Linux 中这可以通过使用 sed 命令和 awk 命令来完成. 在本教程中,我们将告诉你使用 ...

  4. Java查找替换文本文件内容

    文本替换几乎是所有文本编辑器都支持的功能,但是要限制在编辑其中才可以执行该功能.本实例实现了制定文本文件的内容替换,并且不需要再编辑其中打开文本文件. 思路: 先看视图层,要有一个JButton控件用 ...

  5. linux中批量替换文本中字符串--转载

    (一)通过vi编辑器来替换.vi/vim 中可以使用 :s 命令来替换字符串.:s/well/good/ 替换当前行第一个 well 为 good:s/well/good/g 替换当前行所有 well ...

  6. java往文本文件中写入信息并修改

    题目要求: 1.可以往一个文本文档中写入员工信息:name,id和详情 2.可以更改name package FanCQ.Xue.practice; import java.io.*;import j ...

  7. linux sed 批量替换文件中的字符串或符号

    sed -i :直接修改读取的文件内容,而不是输出到终端.   sed -i 就是直接对文本文件进行操作的   替换每行第一次出现的字符串 sed -i 's/查找的字符串/替换的字符串/' 文件   ...

  8. subst - 替换文件中的定义

    总览 (SYNOPSIS) subst [ -e editor ] -f substitutions victim ... 描述 (DESCRIPTION) Subst 能够 替换 文件 的 内容, ...

  9. 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图

    [源码下载] 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Co ...

随机推荐

  1. TCP 流模式与UDP数据报模式(转)

    TCP流模式与UDP数据报模式http://blog.csdn.net/s3olo/article/details/7914717 数据报(datagram)通常是指起始点和目的地都使用无连接网络服务 ...

  2. poj 3358 Period of an Infinite Binary Expansion

    由乘2取整得到分数的小数位,可以找到规律!!! 例如:1/10,2/10,4/10,8/10,16/10,32/10,64/10…… 取整后:1/10,2/10,4/10,8/10,6/10,2/10 ...

  3. longene QQ 安装目录

    longene QQ 在Ubuntu 12.04上的安装目录如下: wy@wy-Inspiron-7420:~/.longene/qq/drive_c/Program Files/Tencent/QQ ...

  4. apache common-io.jar FileUtils

    //复制文件  void copyFile(File srcFile, File destFile)   //将文件内容转化为字符串 String readFileToString(File file ...

  5. 【Apache运维基础(1)】Apache的安装与使用

    安装 yum -y install httpd httpd-devel # 在Ubuntu里面叫做Apache2,输入localhost能打开就算成功了 额...当然专业的运维还是老老实实的去编译吧; ...

  6. TCL语言笔记:TCL中的数组

    一.介绍 Tcl 中的数组和其他高级语言的数组有些不同:Tcl 数组元素的索引,或称键值,可以是任意的字符串,而且其本身没有所谓多维数组的概念.数组的存取速度要比列表有优势,数组在内部使用散列表来存储 ...

  7. 在屏幕上显示C盘根目录下的所有文件和文件夹

    1 import java.io.File; //在屏幕上显示C盘根目录下的所有文件和文件夹 public class ListDemo { public static void main(Strin ...

  8. MFC多文档中opencv处理图像打开、保存

    需要在C**Doc和C**View中进行相应修改 图像打开: Doc.cpp中: BOOL CCVMFCDoc::Load(IplImage** pp, LPCTSTR csFilename) { I ...

  9. 20-语言入门-20-Financial Management

    题目地址: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=72    描述Larry graduated this year and fina ...

  10. ubuntu下android源码下载

    步骤一: 首先保证你的ubuntu系统电脑可以顺利游览google,我们是将etc下 hosts替换掉,推荐hosts: http://laod.cn/hosts/2015-google...host ...