我正在尝试练习文件处理。
这就是我想要做的事情:
我已完成第一部分,但我不知道如何做第二部分。我是编程的新手,所以如果有人可以指导我,我将不胜感激。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace prac
{
class Program
{
static void Main(string[] args)
{
StreamReader rw = new StreamReader("read.txt");
string line = "";
while (line != null)
{
line = rw.ReadLine();
if (line !=null)
{
Console.WriteLine(line);
}
}
rw.Close();
Console.ReadLine();
}
}
}
答案 0 :(得分:2)
以下是完整的应用
private static void Main()
{
// Read all the lines
List<string> currentFileLines = File.ReadAllLines("read.txt").ToList();
// Ask the user the line to start from
Console.WriteLine("Enter the line you wish to delete from:");
// Ask the user for start line
int starLine = Convert.ToInt32(Console.ReadLine());
// Ask the user the line to
Console.WriteLine("Enter the line you wish to delete to:");
// Ask the user for end line
int endLine = Convert.ToInt32(Console.ReadLine());
// Remove the lines
currentFileLines.RemoveRange(starLine - 1, endLine - (starLine - 1));
// Save the file
File.WriteAllLines("read.txt", currentFileLines);
// Show the line on the screen
foreach (string line in currentFileLines)
{
Console.WriteLine(line);
}
Console.ReadLine();
}
答案 1 :(得分:0)
这将实现您想要的
// Read the entire file
List<string> fileLines = new List<string>();
fileLines.AddRange(File.ReadAllLines("read.txt"));
// Indexes start at 0. 9 actually represent line 10,
// and the 10 means to remove 10 items from the list
fileLines.RemoveRange(9, 10); // This is actually line 10
// Write the entire file back out now that it has been modified
File.WriteAllLines("read.txt", fileLines);
如果你想删除&#34;给出&#34;来自文件的行,您可以询问用户输入,以及开始删除过程的行以及要删除的行数。取这两个数字,只需更换9&amp; 10,在我提供的RemoveRange()示例中。
答案 2 :(得分:0)
这是一种方法。你说你会在“控制台”确定要保留或删除哪些行,这就是我在下面实现的内容。
基本上它只是将每一行写入控制台并询问用户是否要删除它。如果他们按“K”,则保留该行,并在内部添加到List<string>
。如果按'D'(删除),则该行不会添加到列表中。
他们也可以选择按'C'(取消),在这种情况下没有对文件做任何事,或'S'(用于保存),在这种情况下,他们标记为删除的任何行都被删除并且他们不必坐在文件的其余部分。
public static void ReadWriteFile(string filePath)
{
if (filePath == null) throw new ArgumentNullException();
if (!File.Exists(filePath)) throw new FileNotFoundException("filePath");
var newLines = new List<string>();
bool cancelled = false;
bool promptForInput = true;
using (var reader = new StreamReader(filePath))
{
int lineNumber = 0;
string currentLine;
while ((currentLine = reader.ReadLine()) != null)
{
if (promptForInput)
{
Console.WriteLine("#{0}: {1}", ++lineNumber, currentLine);
Console.Write("[K]eep or [D]elete this line, [S]ave changes, " +
"or [C]ancel (default = 'K'): ");
var input = Console.ReadKey();
Console.WriteLine();
if (input.Key == ConsoleKey.D)
{
Console.WriteLine("Marked line #{0} for deletion", lineNumber);
continue;
}
if (input.Key == ConsoleKey.C)
{
Console.WriteLine("Cancelling operation. No changes saved.");
cancelled = true;
break;
}
if (input.Key == ConsoleKey.S)
{
promptForInput = false;
}
}
newLines.Add(currentLine);
}
}
if (!cancelled)
{
// By overwriting with the saved lines, we effectively delete the other lines
Console.WriteLine("Deleting selected lines from file...");
File.WriteAllLines(filePath, newLines);
}
}