需求
- 读取指定文件,按行读取,读取成功并执行动作
- 动作成功则删除该行数据
- 失败则停止读取并保留剩余行内容,同时按原文件信息(文件名、日期等)保留剩余文件内容
- 所有行读完则删除该文件。
ai
using System;
using System.IO;
public class LineProcessor
{
public void ProcessAndDeleteLines(string filePath)
{
// 创建临时文件路径以备份原始文件
string tempFilePath = Path.GetTempFileName();
try
{
// 打开源文件和临时文件用于读写
using(var reader = new StreamReader(filePath))
using(var writer = new StreamWriter(tempFilePath))
{
string line;
bool actionFailed = false;
while((line = reader.ReadLine()) != null)
{
// 假设 PerformAction 是你定义的一个方法,它接收一行文本并返回一个布尔值,
// 表示该行处理是否成功
bool actionSucceeded = PerformAction(line);
if(!actionSucceeded)
{
// 动作失败,将当前行写入临时文件,并标记有行处理失败
writer.WriteLine(line);
actionFailed = true;
}
else if(!actionFailed)
{
// 只有在没有行处理失败的情况下,才继续删除成功的行
}
}
}
// 若有行处理失败,将临时文件替换源文件
if(actionFailed)
{
File.Replace(tempFilePath, filePath, null);
}
else
{
// 若所有行都成功处理,则直接删除源文件(此时tempFilePath已经是一个空文件)
File.Delete(filePath);
}
}
catch(Exception ex)
{
Console.WriteLine($ "处理过程中出现错误: {ex.Message}");
// 如果有任何异常,确保临时文件不会覆盖原始文件,这里可以选择保留或删除临时文件
File.Delete(tempFilePath); // 删除临时文件(可选)
return;
}
}
private bool PerformAction(string line)
{
// 在此处编写具体业务逻辑,例如:
// 返回true表示动作成功,应删除该行;返回false表示动作失败,应保留该行
// 这里仅为示例,你需要根据实际需求填充该方法
bool result = YourBusinessLogic(line);
return result;
}
private bool YourBusinessLogic(string line)
{
// 假设你的动作是检查行内容的有效性
// 示例:如果行长度大于0,则认为动作成功
return line.Length > 0;
}
}
ai按需修正,人工修正
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace ReadFileByLine;
public class LineProcessor
{
public void ProcessAndDeleteLines(string filePath)
{
// 创建临时文件路径以备份原始文件
string tempFilePath = Path.GetTempFileName();
DateTime originalLastWriteTime;
try
{
// 记录原文件的最后修改时间
originalLastWriteTime = File.GetLastWriteTime(filePath);
Console.WriteLine($ "Original Last Write Time: {originalLastWriteTime.ToString()}");
// 打开源文件和临时文件用于读写
using(var reader = new StreamReader(filePath))
using(var writer = new StreamWriter(tempFilePath))
{
string line;
bool actionFailed = false;
while((line = reader.ReadLine()) != null)
{
// 假设 PerformAction 是你定义的一个方法,它接收一行文本并返回一个布尔值,
// 表示该行处理是否成功
bool actionSucceeded = PerformAction(line);
if(!actionSucceeded)
{
// 动作失败或尚未出现失败时,将当前行写入临时文件
writer.WriteLine(line);
actionFailed = true;
}
}
// 关闭源文件和临时文件
reader.Close();
writer.Close();
// 检查并等待源文件释放(在跨平台环境下,您可能需要根据实际操作系统选择合适的文件锁检测方式)
// (此处略去文件锁定检测代码)
// 替换原文件为临时文件
File.Replace(tempFilePath, filePath, null);
// 由于我们在写入临时文件时就实时更新了时间戳,所以这里不再需要重新设置时间戳
// 立即更新临时文件的时间戳为原文件的时间戳
File.SetLastWriteTime(filePath, originalLastWriteTime);
//删除临时文件
File.Delete(tempFilePath);
// 关闭源文件和临时文件
reader.Close();
writer.Close();
}
}
catch(Exception ex)
{
Console.WriteLine($ "处理过程中出现错误: {ex.Message}");
// 如果有任何异常,确保临时文件不会覆盖原始文件,这里可以选择保留或删除临时文件
File.Delete(tempFilePath); // 删除临时文件(可选)
return;
}
}
int count = 0;
private bool PerformAction(string line)
{
count++;
if(count > 10) //模拟读取异常
return false;
// 在此处编写具体业务逻辑,例如:
// 返回true表示动作成功,应删除该行;返回false表示动作失败,应保留该行
// 这里仅为示例,你需要根据实际需求填充该方法
bool result = YourBusinessLogic(line);
return result;
}
private bool YourBusinessLogic(string line)
{
// 假设你的动作是检查行内容的有效性
// 示例:如果行长度大于0,则认为动作成功
return line.Length > 0;
}
}