我知道之前在Stackoverflow上已经问过这个问题,但是找不到解释.
当我尝试从压缩字节数组中读取一个字符串时,我在第一次尝试时得到一个空字符串,在第二次尝试时我得到了字符串.
代码示例:
public static string Decompress(byte[] gzBuffer)
{
if (gzBuffer == null)
return null;
using (var ms = new MemoryStream(gzBuffer))
{
using (var decompress = new GZipStream(ms, CompressionMode.Decompress))
{
using (var sr = new StreamReader(decompress, Encoding.UTF8))
{
string ret = sr.ReadToEnd();
// this is the extra check that is needed !?
if (ret == "")
ret = sr.ReadToEnd();
return ret;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
所有建议表示赞赏. - Victor Cassel
大家好我需要做的是跟踪我从流阅读器中读取的行的位置当我说reader.ReadLine()我需要知道文件中该行的位置时我还希望能够从中读取文件我之前跟踪的位置.
这可能吗?如果是这样请协助.
非常感谢帮助
提前致谢.
在环顾四周的时候,我发现了很多关于如何计算出文件中行数的讨论.
例如这三个:
c#我 如何计算
文本文件中的行数确定文本文件中的行数
如何快速计算行数?
那么,我继续前进并最终使用了我能找到的最有效(至少是内存方式?)方法:
private static int countFileLines(string filePath)
{
using (StreamReader r = new StreamReader(filePath))
{
int i = 0;
while (r.ReadLine() != null)
{
i++;
}
return i;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当文件中的行本身非常长时,这将永远存在.真的没有更快的解决方案吗?
我一直试图使用StreamReader.Read()或者StreamReader.Peek()我不能(或者不知道如何)让它们中的任何一个一旦有'东西'(字符?文本?)就移动到下一行.
有什么想法吗?
结论/结果(根据提供的答案进行一些测试后):
我在两个不同的文件中测试了下面的5种方法,我得到了一致的结果,似乎表明普通的旧StreamReader.ReadLine()方法仍然是最快的方法之一......说实话,我对答案中的所有评论和讨论感到困惑.
文件#1:
大小:3,631 KB
行:56,870
文件#1的结果以秒为单位:
0.02 - > ReadLine方法.
0.04 - >读取方法.
0.29 - > ReadByte方法.
0.25 - > Readlines.Count方法.
0.04 - > ReadWithBufferSize方法.
文件#2:
大小
:14,499 KB 行:213,424
文件#1的结果以秒为单位:
0.08 - > …
public void get10FirstLines()
{
StreamReader sr = new StreamReader(path);
String lines = "";
lines = sr.readLine();
}
Run Code Online (Sandbox Code Playgroud)
如何在字符串中获取文件的前10行?
我有一个本地化的应用程序,可以在整个欧洲使用.
我有一个菜单选项从磁盘加载文件.
此操作在我的开发机器上运行正常但在我用于测试其他操作系统的虚拟机上不起作用_例如法语,西班牙语等.
StreamReader尝试打开文件时生成FileNotFoundException.
它说"'找不到文件C:\ Program Files\MyCompany\MyTool\bin\Files\debug.txt'"
事实是,文件确实存在,位于正确的位置并且文件名正确.
目标(法语)操作系统上的目录名称与开发机器相同.
有任何想法吗?
string ourPath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
try
{
System.IO.StreamReader sr = System.IO.File.OpenText(ourPath + @"\bin\Files\debug.txt");
string input = null;
while ((input = sr.ReadLine()) != null)
{
m_text.Append(input);
}
sr.Close();
}
catch (System.IO.FileNotFoundException)
{
MessageBox.Show("LoadDebugOptions: File Not Found: " + ex.Message);
}
Run Code Online (Sandbox Code Playgroud) 你可以使用streamreader来读取普通文本文件,然后在保存当前位置后再读取流读取器,然后再次打开streamreader并开始读取该poision吗?
如果不是我可以使用什么来完成相同的情况而不锁定文件?
这样的事情:
var fs = File.Open(@"C:\testfile.txt", FileMode.Open, FileAccess.Read);
var sr = new StreamReader(fs);
Debug.WriteLine(sr.ReadLine());//Prints:firstline
var pos = fs.Position;
while (!sr.EndOfStream)
{
Debug.WriteLine(sr.ReadLine());
}
fs.Seek(pos, SeekOrigin.Begin);
Debug.WriteLine(sr.ReadLine());//Prints Nothing, i expect it to print SecondLine.
Run Code Online (Sandbox Code Playgroud)
@lasseespeholt
这是我试过的代码
var position = -1;
StreamReaderSE sr = new StreamReaderSE(@"c:\testfile.txt");
Debug.WriteLine(sr.ReadLine());
position = sr.BytesRead;
Debug.WriteLine(sr.ReadLine());
Debug.WriteLine(sr.ReadLine());
Debug.WriteLine(sr.ReadLine());
Debug.WriteLine(sr.ReadLine());
Debug.WriteLine("Wait");
sr.BaseStream.Seek(position, SeekOrigin.Begin);
Debug.WriteLine(sr.ReadLine());
Run Code Online (Sandbox Code Playgroud) 简单地说,我一直在努力实现BufferedStreamReaderJava中的功能.我打开了一个套接字流,只想逐行地逐行读取它.
我有以下服务器代码.
while (continueProcess)
{
try
{
StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8);
string command = reader.ReadLine();
if (command == null)
break;
OnClientExecute(command);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
以下客户端代码:
TcpClient tcpClient = new TcpClient();
try
{
tcpClient.Connect("localhost", serverPort);
StreamWriter writer = new StreamWriter(tcpClient.GetStream(), Encoding.UTF8);
writer.AutoFlush = true;
writer.WriteLine("login>user,pass");
writer.WriteLine("print>param1,param2,param3");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
tcpClient.Close();
}
Run Code Online (Sandbox Code Playgroud)
服务器只读取第一行(login>user,pass)然后ReadLine返回null!
实现这种面向行的读者的最简单方法是什么,就像在Java中一样BufferedStreamReader?:■
你好我正在做一些事情,我需要能够将文本添加到.txt文件中.虽然我已经完成了这个我有一个小问题.我需要或多或少地在文件中间写字符串.例:
Hello my name is Brandon,
I hope someone can help, //I want the string under this line.
Thank you.
Run Code Online (Sandbox Code Playgroud)
希望有人可以帮助解决方案.
编辑好吧谢谢你们,我会试着想出来,可能只是重写整个文件.好吧,我正在制作的程序与hosts文件有关,并不是每个人都有相同的hosts文件,所以我想知道是否有办法读取他们的hosts文件,并复制所有文件,同时将字符串添加到它?
我在控制台应用程序中有这个代码,它在一个循环中运行
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search);
request.Headers.Add("Accept-Language", "de-DE");
request.Method = "GET";
request.Accept = "text/html";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(),
Encoding.ASCII))
{
string html = reader.ReadToEnd();
FindForMatch(html, url);
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)
经过几次循环就给出了
无法从传输连接读取数据:连接已关闭
错误.知道为什么会这样吗?感谢名单..
我正在使用Portable Class Libraries编写ConfigManager类.PCL支持StreamReader和StreamWriter我想要使用的类,但这些类的PCL版本不支持在构造期间传入字符串.PCL也不支持reader.Close()和writer.Close().最后它不支持该FileStream课程.
所以我正在寻找以下任何一个问题的答案:
StreamReader和StreamWriter类在PCL中工作?stream使用PCL 创建新的?c# filestream streamwriter streamreader portable-class-library
c# ×10
streamreader ×10
.net ×2
readline ×2
streamwriter ×2
filestream ×1
lines ×1
localization ×1
peek ×1