标签: streamreader

HTTPWebResponse + StreamReader非常慢

我正在尝试使用HttpWebResponse.GetResponse()和Streamreader.ReadToEnd()在C#中实现有限的Web爬虫(仅限几百个站点),也尝试使用StreamReader.Read()和循环来构建我的HTML字符串.

我只下载大约5-10K的页面.

这一切都很慢!例如,平均GetResponse()时间约为半秒,而平均StreamREader.ReadToEnd()时间约为5秒!

所有站点都应该非常快,因为它们非常靠近我的位置,并且具有快速的服务器.(在资源管理器中几乎没有任何东西到D/L)并且我没有使用任何代理.

我的Crawler有大约20个线程同时从同一个站点读取.这会导致问题吗?

如何减少StreamReader.ReadToEnd DRASTICALLY?

c# performance web-crawler httpwebresponse streamreader

20
推荐指数
2
解决办法
2万
查看次数

如何在Java/Scala中跳过流中的无效字符?

例如,我有以下代码

Source.fromFile(new File( path), "UTF-8").getLines()
Run Code Online (Sandbox Code Playgroud)

它抛出异常

Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
    at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:319)
Run Code Online (Sandbox Code Playgroud)

我不在乎是否读取了一些行,但是如何跳过无效的字符并继续读取行?

java scala inputstream streamreader

19
推荐指数
2
解决办法
1万
查看次数

如何将StreamReader转换为字符串?

我修改了我的代码,因此我可以将文件打开为只读.现在我无法使用,File.WriteAllText因为我的FileStreamStreamReader没有转换为字符串.

这是我的代码:

static void Main(string[] args)
{
    string inputPath = @"C:\Documents and Settings\All Users\Application Data\"
                     + @"Microsoft\Windows NT\MSFax\ActivityLog\OutboxLOG.txt";
    string outputPath = @"C:\FAXLOG\OutboxLOG.txt";

    var fs = new FileStream(inputPath, FileMode.Open, FileAccess.Read,
                                      FileShare.ReadWrite | FileShare.Delete);
    string content = new StreamReader(fs, Encoding.Unicode);

    // string content = File.ReadAllText(inputPath, Encoding.Unicode);
    File.WriteAllText(outputPath, content, Encoding.UTF8);
}
Run Code Online (Sandbox Code Playgroud)

c# streamreader

18
推荐指数
2
解决办法
7万
查看次数

C#StreamReader,"ReadLine"用于自定义分隔符

获得方法功能的最佳方法是什么StreamReader.ReadLine(),但使用自定义(字符串)分隔符?

我想做点什么:

String text;
while((text = myStreamReader.ReadUntil("my_delim")) != null)
{
   Console.WriteLine(text);
}
Run Code Online (Sandbox Code Playgroud)

我尝试我自己的使用,使Peek()StringBuilder,但它的效率太低.我正在寻找建议或可能是一个开源解决方案.

谢谢.

编辑

我之前应该澄清一下......我已经看到了这个答案,但是,我不想将整个文件读入内存.

c# file-io parsing streamreader delimiter

18
推荐指数
1
解决办法
1万
查看次数

从C#Resources读取文本文件

我需要从我的资源中读取一个文件并将其添加到列表中.我的代码:

private void Form1_Load(object sender, EventArgs e)
{
    using (StreamReader r = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("myProg.myText.txt")))
    {
        //The Only Options Here Are BaseStream & CurrentEncoding
    }
}
Run Code Online (Sandbox Code Playgroud)

我搜索了这个,只得到了答案,"Assembly.GetExecutingAssembly...."但我的程序没有大会选项.

c# file-io resources streamreader

18
推荐指数
2
解决办法
5万
查看次数

使用c#搜索文本文件并显示行号和包含搜索关键字的完整行

我需要帮助才能使用c#搜索文本文件(日志文件)并显示行号和包含搜索关键字的完整行.

c# command-line text line-numbers streamreader

16
推荐指数
2
解决办法
5万
查看次数

如何检测.NET StreamReader是否在基础流上找到了UTF8 BOM?

我得到一个FileStream(filename,FileMode.Open,FileAccess.Read,FileShare.ReadWrite)然后一个StreamReader(stream,true).

有没有办法可以检查流是否以UTF8 BOM开头?我注意到没有BOM的文件被StreamReader读取为UTF8.

我怎么能分开呢?

c# byte-order-mark utf-8 filestream streamreader

16
推荐指数
3
解决办法
9075
查看次数

使用'使用'而不是关闭WebResponse和StreamReader是否安全

目前

我为HttpWebRequest被调用实现了一个简单的帮助方法GetResponse(url).目前,我手动关闭WebResponseStreamReader读取结果之后.然后我就这样返回结果:

// construct the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";

// get the result
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string result = reader.ReadToEnd();

// clean up and return the result
reader.Close();
response.Close();
return result;
Run Code Online (Sandbox Code Playgroud)

建议

在陈述中包含回报而不是关闭它们是否安全using ; 这会和.Close()es 有同样的效果吗?

// construct the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";

// get the result
using (WebResponse response = request.GetResponse())
{
    using …
Run Code Online (Sandbox Code Playgroud)

c# httpwebrequest streamreader

16
推荐指数
1
解决办法
5230
查看次数

在C#中同时读写文件

我有一个文件包含我想要监视更改的数据,以及添加我自己的更改.想像"Tail -f foo.txt".

基于这个线程,看起来我应该创建一个文件流,并将它传递给一个编写器和读者.但是,当读者到达原始文件的末尾时,它无法看到我自己写的更新.

我知道这似乎是一个奇怪的情况......它更多的是一个实验,看看它是否可以完成.

这是我尝试的示例案例:


foo.txt:
a
b
c
d
e
f


        string test = "foo.txt";
        System.IO.FileStream fs = new System.IO.FileStream(test, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);

        var sw = new System.IO.StreamWriter(fs);
        var sr = new System.IO.StreamReader(fs);

        var res = sr.ReadLine();
        res = sr.ReadLine();
        sw.WriteLine("g");
        sw.Flush();
        res = sr.ReadLine();
        res = sr.ReadLine();
        sw.WriteLine("h");
        sw.Flush();
        sw.WriteLine("i");
        sw.Flush();
        sw.WriteLine("j");
        sw.Flush();
        sw.WriteLine("k");
        sw.Flush();
        res = sr.ReadLine();
        res = sr.ReadLine();
        res = sr.ReadLine();
        res = sr.ReadLine();
        res = sr.ReadLine();
        res = sr.ReadLine();
Run Code Online (Sandbox Code Playgroud)

过了"f"后,读者返回null.

c# filestream streamwriter streamreader

14
推荐指数
2
解决办法
4万
查看次数

C#Stream.Read超时

我有这个streamreader:

            Boolean read = false;
            while (wline!="exit")
            {

                while (!read || streamReader.Peek() >= 0)
                {
                    read = true;
                    Console.Write((char)streamReader.Read());
                }
                wline = Console.ReadLine();
                streamWriter.Write(wline+"\r\n");
                streamWriter.Flush();

            }
Run Code Online (Sandbox Code Playgroud)

如何设置Read()方法的超时?谢谢

c# sockets timeout streamreader

14
推荐指数
2
解决办法
2万
查看次数