我正在尝试使用StreamReader读取文件的内容,StreamReader接收FileStream.该文件中有一些空格(char 32),StreamReader将它们读为0(char 48).屏幕截图显示了FileStream缓冲区和StreamReader缓冲区.两者都有值32,但是当我调用Read()时,它返回48.我错过了什么吗?顺便说一句,代码在.NET Compact Framework下运行.
alt text http://www.freeimagehosting.net/uploads/9f72b61bbe.png
读取数据的代码:
public void Read() {
using (StreamReader reader = new StreamReader(InputStream, Encoding.UTF8)) {
foreach (var property in DataObject.EnumerateProperties()) {
OffsetInfo offset = property.GetTextOffset();
reader.BaseStream.Position = offset.Start - 1;
StringBuilder builder = new StringBuilder(offset.Size);
int count = 0;
while (reader.Peek() >= 0 && count < offset.Size) {
char c = (char)reader.Read();
if ((int)c != 32 && c != '\r' && c != '\n') {
builder.Append(c);
count++;
} else {
reader.BaseStream.Position++;
}
}
property.SetValue(DataObject, …Run Code Online (Sandbox Code Playgroud) 我从.NET应用程序运行exe并尝试将标准重定向到streamreader.问题是,当我这样做
myprocess.exe >> out.txt
out.txt接近14mb.当我执行命令行版本时,它非常快,但是当我从我的csharp应用程序运行该进程时,它非常慢,因为我相信默认的streamreader每4096字节刷新一次.
有没有办法更改Process对象的默认流读取器?
如何将包含1000行的单个文本文件拆分为多个较小的文件,例如每行300行?请记住,原始文件可能有多于或少于一千行.
file1.txt 300 lines -> rest
file2.txt 300 lines -> rest
file3.txt 300 lines -> rest
file4.txt 100 lines
Run Code Online (Sandbox Code Playgroud)
我试过以下但是没有用.
int counter = 0;
string line;
string lineoutput = (current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".txt");
System.IO.StreamReader inputfile;
inputfile = new System.IO.StreamReader(new_path);
while ((line = inputfile.ReadLine()) != null)
{
System.IO.StreamWriter file = new System.IO.StreamWriter(current_dir + "\\" + DateTime.Now.ToString("HHmmss") + ".txt", true);
string _replaceBackspace = ReplaceBackspace(read_file.ReadLine().ToLower());
using (StreamWriter writer = new StreamWriter(lineoutput, true))
{
if (counter == 5000)
{
counter = …Run Code Online (Sandbox Code Playgroud) 我有一个数组从文本文件中获取其值.每一行都是一个数组值.我试图让每个数组值进入列表框.如
[0] - Hello
[1] - Goodbye
Run Code Online (Sandbox Code Playgroud)
在列表框中,第一个选项是hello,第二个选项是再见等.
文本文件是分开的行上的Hello和Goodbye
继承我的代码从文本文件中排序数组:
StreamReader sr = new StreamReader("text.txt");
int counter = 0;
string line;
string[] linea;
linea = new string[100];
while ((line = sr.ReadLine()) != null)
{
linea[counter] = line;
counter++;
}
sr.Close();
Run Code Online (Sandbox Code Playgroud)
这是我的列表框代码:
this.listBox1.Items.AddRange(new object[] {
// Values go here
// i want the arrays here, so that its like "hello", "goodbye",
});
Run Code Online (Sandbox Code Playgroud)
非常感谢帮助.我正在使用MS Visual Studio 2010.
我正在使用一些不是我编写的最佳代码......: -
我有以下代码:
string fmtLine = "";
string[] splitedFmtLine;
int counterFMTlines = 0;
foreach (string fmtF in fmtFiles)
{
using (StreamReader sr = new StreamReader(fmtF))
{
while ((fmtLine = sr.ReadLine()) != null)
{
Console.WriteLine(counterFMTlines++);
foreach (L3Message message in rez)
{
splitedFmtLine = Regex.Split(fmtLine, "\t");
if (message.Time == splitedFmtLine[0])
{
message.ScramblingCode = splitedFmtLine[7];
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当List为空并且只有一个文件(制表符分隔,280000行)时,我测试了这段代码,即使这样,也需要一生(1分钟)来浏览我文件的所有280000行.这意味着执行跳过了foreach循环,其中是myObjs列表.
我不明白为什么花这么久?
例如,我用不同的文本文件(源文件)填充我的myObjs列表(树层次结构),但是大于此制表符分隔(制表符分隔:16MB,源文件:36MB)并且仅花费第二个而不是这个1分钟.
首先:抱歉我的英语不好!
我知道标题不是最好的英文,但我真的不知道如何格式化这个问题...
我想要做的是逐行读取HTML源代码,这样当它看到一个给定的单词时(比如http://)它复制整个句子,所以我可以剥去其余的只保留URL.
这就是我尝试过的:
using (var source = new StreamReader(TempFile))
{
string line;
while ((line = source.ReadLine()) != null)
{
if (line.Contains("http://"))
{
Console.WriteLine(line);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我想从外部文件中读取它,但是当我想读取字符串或字符串构建器时它不起作用,那么这种方法是完美的,你如何逐行阅读?
我已经在这个上挖了很多东西而且找不到我正在寻找的东西.
INPUT:多行(数百,偶尔数千)ASCII文本行,范围从97个字符到500个以上.我是否要保留此数据的标准纯粹包含在前3个字符中(总是数字 - 任意值100,200和300是我感兴趣的).
所需的输出只是那些以100,200或300开头的输出,其余我可以忽略.
这就是我的streamreader,它目前输出到控制台:
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
public void Do
{
// Read in a file line-by-line, and store in a List.
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader("File.dat"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
list.Add(line); // Add to list.
Console.WriteLine(line); // Write to console.
// Console.ReadLine();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望能说出一句话
IF {
FIRST3CHAR != (100,200,300) }
then skip,
Run Code Online (Sandbox Code Playgroud)
但我不确定如何定义FIRST3CHAR类.这是将对原始数据执行的唯一过滤器.
接下来,我将根据其中包含的其他标准分析这个过滤后的数据集,但在请求任何帮助之前,我会自己给出一个镜头.
我正在使用StreamReader将CSV文件的每一行读取为字符串.当我处理每一行时,我需要删除任何仅由其他数字包围的逗号.
例如,如果字符串是:
"textfield1", "textfield2", "100.00", "1,070.00"
我只需要从整个字符串中取出"1,070.00"的逗号,结果是:
"textfield1", "textfield2", "100.00", "1070.00"
从CSV文件读取的每个字符串可以在字段数,长度等方面有所不同,所以我需要使用一些东西(正则表达式可能?)来查看整个字符串而无需对所有逗号进行硬编码或删除.
这是我一直在尝试的方法:
StreamReader sr = new StreamReader(strInputFile);
string nextLine = sr.ReadLine();
try
{
while ((nextLine = sr.ReadLine()) != null)
{
string rawtext = nextLine.Replace("[0-9]"+","+"[0-9]" , "[0-9]"+"[0-9]");
// ....rest of code
}
}
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,因为我不明白该怎么做:)
我是C#的新手并且在Regex中缺乏经验,所以希望这相对简单.
我需要在C#应用程序上同时逐行读取四个非常大(> 2 Gb)的文件.我正在使用四种不同的StreamReader对象及其ReadLine()方法.同时从四个文件中读取线条时性能受到严重影响,但是每个文件到达EoF时都会变得更好(有4个文件的性能<带3个文件的性能<带有2个文件的性能......).
我有这个(简化,假设只有两个文件,更简洁的例子)代码:
StreamReader readerOne = new StreamReader(@"C:\temp\file1.txt");
StreamReader readerTwo = new StreamReader(@"C:\temp\file2.txt");
while(readerOne.Peek() >= 0 || readerTwo.Peek() >= 0)
{
string[] readerOneFields = readerOne.Peek() >= 0 ?
readerOne.ReadLine().Split(',') : null;
string[] readerTwoFields = readerTwo.Peek() >= 0 ?
readerTwo.ReadLine().Split(',') : null;
if (readerOneFields != null && readerTwoFields != null)
{
if (readerOneFields[2] == readerTwoFields[2])
{
// Do some boring things...
}
else if (readerOneFields != null)
{
// ...
}
else …Run Code Online (Sandbox Code Playgroud) 我正在使用这种方式读取巨大的csv文件(按文件大约350K行):
StreamReader readFile = new StreamReader(fi);
string line;
string[] row;
readFile.ReadLine();
while ((line = readFile.ReadLine()) != null)
{
row = line.Split(';');
x=row[1];
y=row[2];
//More code and assignations here...
}
readFile.Close();
}
Run Code Online (Sandbox Code Playgroud)
这里的要点是,逐月读取一个巨大的文件可能会很慢,而且我认为必须采用另一种方法来更快地完成它.