所以我使用StreamReader使用MemoryStream写入StreamWriter并在此应用程序内部,但内存使用量增加300mb(来自其中一个较大的输入)并且在完成使用后不会释放:
StreamWriter log = new StreamWriter("tempFile.txt");
log.Write(reader.ReadToEnd());
log.Close();
reader.DiscardBufferedData();
reader.Close();
reader.Dispose();
memoryStream.Dispose();
log.Dispose();
GC.Collect();
Run Code Online (Sandbox Code Playgroud)
在此之前和我获得RAM使用之后,它比之前少300 MB,但我不知道为什么.考虑到这里唯一发生的事情就是将读取器中的数据放在文本文件中我不知道为什么甚至需要使用大量内存,我已经做了我能想到的一切来释放内存暂时.有什么我想念的吗?...谢谢.
我想要做的是记住我在输入流中的位置,然后再回到那里.在使用mark()和reset()的java中非常简单,但我不知道如何在c#中实现这一点.没有这样的方法.
例如
public int peek()
{
try
{
file.x; //in java file.mark(1)
int tmp = file.read();
file.+ //in java file.reset();
return tmp;
}
catch (IOException ex) {}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序从文本文件中读取信息,然后对它们进行分类并将它们放到数据库中.对于一个类别,我需要检查当前行之后的行并查找某个关键字?
我如何阅读这一行?这个应该在streamreader已经打开当前行时发生....
我在VS2010上使用c#.
编辑:
下面的所有代码都是一段时间(!sReader.EndOfStream)循环
string line = sReader.ReadLine(); //Note: this is used way above and lots of things are done before we come to this loop
for (int i = 0; i < filter_length; i++)
{
if (searchpattern_queries[i].IsMatch(line) == true)
{
logmessagtype = selected_queries[i];
//*Here i need to add a if condition to check if the type is "RESTARTS" and i need to get the next line to do more classification. I need to get that …Run Code Online (Sandbox Code Playgroud) 我正在开发一个C#程序,它读取非常大的文件并检查它们是否有不同的属性和字段.我一直在测试不到100万行的文件,并按预期进行预编译.我最近在一个包含250万行的文件上进行了测试,花了4个小时才完成.
我正在使用自定义阅读功能读取每个字符,以便我可以找到所有CR和LF,因为每行包含它们非常重要.我已经单独测试了阅读功能,它看起来大约需要14分钟来读取文件,我觉得这个文件足够合理,可以读取250个行中1500个字符的每个字符.我将包括我阅读功能,但这似乎并没有导致问题.
我的阅读功能将每个字符添加到一个字符串,然后我检查字符串中的不同值.例如,行长度是否正确,文件是否包含标题,标题是否包含正确的值.以及特定值如char位置403-404一个数字,是字段1250-1300不为空等.
我的问题是我能做些什么来弄清楚导致速度减慢的原因并提高我的程序效率?我已经尝试检查每个循环开始和结束时的时间,但似乎没有改变.但是,每100,000人需要的时间比以前长得多.例如,处理线10,000到20,000花费不到3秒,830,000到840,000花费约35秒.我曾考虑尝试多个线程,但不认为在我从文件中读取行的情况下它会有所帮助.思考?谢谢您的帮助!
static void ReadMyLine(ref string currentLine, string filePath, ref int asciiValue, ref Boolean isMissingCR, ref Boolean isMissingLF, ref Boolean isReversed, ref StreamReader file)
{
Boolean endOfRow = false;
isMissingCR = false;
isMissingLF = false;
isReversed = false;
currentLine = "";
while (endOfRow == false)
{
asciiValue = file.Read();
if (asciiValue == 10 || asciiValue == 13)
{
int asciiValueTemp = file.Peek();
if (asciiValue == 13 && asciiValueTemp == 10)
{
endOfRow = true;
asciiValue = file.Read();
} …Run Code Online (Sandbox Code Playgroud) 当我仅用\r作行终止符时,该StreamReader.ReadLine()方法不起作用.它可以使用Environment.NewLine,\r\n或者\ra("a"是任何字符).这是一个错误吗?使用MemoryStream而不是NetworkStream时不会发生同样的问题.如果我无法更改协议,我可以使用哪些变通方法?
我的服务
Thread service = new Thread((ThreadStart)delegate
{
TcpListener listener = new TcpListener(IPAddress.Loopback, 2051);
listener.Start();
using (TcpClient client = listener.AcceptTcpClient())
using (NetworkStream stream = client.GetStream())
using (StreamReader reader = new StreamReader(stream))
using (StreamWriter writer = new StreamWriter(stream))
{
writer.AutoFlush = true;
string inputLine = reader.ReadLine(); // doesn't work - freezes here
writer.Write("something\r");
}
});
service.Start();
Run Code Online (Sandbox Code Playgroud)
我的客户
using (TcpClient client = new TcpClient("localhost", 2051))
using (NetworkStream stream = client.GetStream())
using (StreamWriter …Run Code Online (Sandbox Code Playgroud) 我试图将一个5 GB的ISO文件复制到一个具有29 GB可用空间的32 GB闪存驱动器上.
Windows 7拒绝让我将文件拖放到闪存驱动器上,报告文件对于目标文件系统来说太大了.
我最终了解到这是因为驱动器被格式化为FAT32而不是NTFS,但在我编写此例程以复制文件之前没有:
private void copyFile(string from, string to) {
bool ok = true;
using (StreamReader sr = new StreamReader(from, true)) {
using (StreamWriter sw = new StreamWriter(to, false, sr.CurrentEncoding)) {
int FOUR_K = 4048;
char[] buf = new char[FOUR_K];
try {
while (-1 < sr.Peek()) {
int len = sr.Read(buf, 0, FOUR_K);
sw.Write(buf, 0, len);
sw.Flush();
}
}
catch (Exception err) {
ok = false;
throw …Run Code Online (Sandbox Code Playgroud) 我希望得到一个string[]指定的StreamReader.喜欢:
try{
StreamReader sr = new StreamReader("a.txt");
do{
str[i] = sr.ReadLine();
i++;
}while(i < 78);
}
catch (Exception ex){
MessageBox.Show(ex.ToString());
}
Run Code Online (Sandbox Code Playgroud)
我可以做到但不能使用字符串[].我想做这个:
MessageBox.Show(str[4]);
Run Code Online (Sandbox Code Playgroud)
如果您需要更多信息,请随时询问,我会更新.提前致谢...
我有一个逐行读取的流阅读器sr.ReadLine().我的代码用行结尾\r\n和/或来计算行尾\n.
StreamReader sr = new System.IO.StreamReader(sPath, enc);
while (!sr.EndOfStream)
{
// reading 1 line of datafile
string sLine = sr.ReadLine();
...
Run Code Online (Sandbox Code Playgroud)
如何告诉代码(而不是通用sr.ReadLine())我想要计算新行只有一个完整\r\n而不是\n?
目前,我正在读取特定条目"X"的文件.一旦我找到这个条目,我可以在任何后续行中做我需要做的事情但是在"X"出现之前有一条信息需要2行.现在我可以使用var line = reader.ReadLine();在文件中前进并读取"X"之后的行.我如何回过头来读取2行数据?
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (line.Contains("X"))
{
Processing
}
}
Run Code Online (Sandbox Code Playgroud) streamreader ×10
c# ×8
.net ×3
c#-4.0 ×2
streamwriter ×2
file ×1
file-io ×1
memorystream ×1
newline ×1
readline ×1
string ×1
text ×1
text-files ×1
tfilestream ×1
vb.net ×1