我正在尝试将数据从文本文件加载到数据库。我的源文件NUL以某种方式包含空字符(图片1)。
我只是将所有字段都设为一列(以分隔{CR}{LF})。然后我进行数据预览。
数据正是我们所需要的。但是,当我运行该程序包时,数据发生了变化,这与我在数据预览中看到的不一样。我添加了一个数据查看器以查看数据。
数字1在第一行消失(请参见红色)。似乎平面文件的读取以NUL字符结尾。但是我的行定界符是{CR}{LF},最后消失的数字1没有意义。谁能告诉我为什么?
我正在尝试编写的代码是替换文本文件中的一串字.虽然我能够将文件的内容读取到控制台,但我无法替换字符串并将新字符串写入文件.
这是我的代码:
private static void filesys_created (object sender, FileSystemEventArgs e)
{
using (StreamReader sr = new StreamReader(e.FullPath))
{
Console.WriteLine(sr.ReadToEnd());
File.ReadAllText(e.FullPath);
sr.Close();
}
using (StreamWriter sw = new StreamWriter(e.FullPath))
{
string text = e.FullPath.Replace("The words I want to replace");
string newtext = "text I want it to be replaced with";
sw.Write(e.FullPath, text);
sw.Write(newtext);
sw.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是.Replace正在删除文本文件中的所有内容,只插入目录的路径.
我正试图解决这个问题:
编写一个程序,用文本文件中的"finish"替换每个子串"start".你能改写程序只替换整个单词吗?该程序是否适用于大文件(例如800 MB)?
我一直试图这样做,但显然你不能同时读写.如果有人可以查看我的代码并帮助我,那就太棒了.这是一个例外:
The process cannot access the file 'C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt' because it is being used by another process.
你不必直接给我答案,而是告诉我这个过程.谢谢!
这是我目前的代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Chapter_15_Question_7
{
class Program
{
static void Main(string[] args)
{
StreamReader reader = new StreamReader(
@"C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter 15 Question 7\TextFile.txt");
StreamWriter writer = new StreamWriter(
@"C:\Users\Nate\Documents\Visual Studio 2015\Projects\Chapter 15\Chapter 15 Question 7\Chapter …Run Code Online (Sandbox Code Playgroud)