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. POJ3253Babelfish

    http://poj.org/problem?id=3253 就是一个哈夫曼树,题目下边有提示,所以题意还是好理解的 #include<cstdio> #include<cstrin ...

  2. hdu 3951 Coin Game 博弈论

    思路: 当n<=k时,先手必胜: 当k=1时,n为奇数先手胜,否则后手胜: 当k>1时,先手操作之后必定形成链,后手操作后形成二条一样的链,之后,先手怎么操作,后手就怎么操作,则后手必胜. ...

  3. A. Sorting Railway Cars

    A. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. [itint5]最短路径遍历点

    http://www.itint5.com/oj/#50 此题有点难,参考了这篇文章,是个两条路的DP: http://blog.csdn.net/a83610312/article/details/ ...

  5. 缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例

    之前为大家介绍了如何使用spring注解来进行缓存配置 (EHCache 和 OSCache)的简单的例子,详见 Spring基于注解的缓存配置--EHCache AND OSCache 现在介绍一下 ...

  6. MIT算法导论——第四讲.Quicksort

    本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...

  7. Solr查询详解

    前言:上节是关于Solr的开发准备工作:.NET开发过程中的全文索引使用技巧之Solr(http://www.cnblogs.com/johnwood/p/3447242.html) 这节重点是讲So ...

  8. Android:AlertDialog对话框

    1.简单的ALertDialog: Dialog alertDialog = new AlertDialog.Builder(this) .setTitle("标题") .setM ...

  9. Java API —— ArrayList类 & Vector类 & LinkList类

    1.ArrayList类     1)ArrayList类概述         · 底层数据结构是数组,查询快,增删慢         · 线程不安全,效率高     2)ArrayList案例   ...

  10. 10个实用的PHP正则表达式

    正则表达式是程序开发中一个重要的元素,它提供用来描述或匹配文本的字符串,如特定的字符.词或算式等.但在某些情况下,用正则表达式去验证一个字符串比较复杂和费时.本文为你介绍10种常见的实用PHP正则表达 ...