标签: streamreader

C# 从 JsonReader 读取 JObject 时出错。路径 '',第 0 行,位置 0

我正在尝试使用下面的代码解析我的 json。我收到错误:

从 JsonReader 读取 JObject 时出错。路径 '',第 0 行,位置 0。

我想这可能是因为我的 JSON 格式错误,所以我输出了它,看起来没问题:

{ 
    "serviceDeskId": "4", 
    "requestTypeId": "223", 
    "requestFieldValues": { 
        "summary": "test" 
    } 
} 
Run Code Online (Sandbox Code Playgroud)

但现在我完全陷入困境了。谁能看到我哪里出错了?这真让我抓狂!!

正是在这一行触发了错误:

var jsonresponse = JObject.Parse(response);
Run Code Online (Sandbox Code Playgroud)

完整代码片段:

req.ContentType = "application/json";

                var json = JObject.Parse(
                        "{\"serviceDeskId\": \"4\",\"requestTypeId\": \"223\",\"requestFieldValues\": {\"summary\": \"" +
                        summary.Value + "\"}}");

                jsonCheck = json.ToString();

                using (var streamWriter = new StreamWriter(req.GetRequestStream()))
                    {

                        streamWriter.Write(json);
                    }

                    HttpWebResponse resp = req.GetResponse() as HttpWebResponse;


                    // Obtain a 'Stream' object associated with the response object.
                    Stream ReceiveStream = resp.GetResponseStream(); …
Run Code Online (Sandbox Code Playgroud)

c# json stream streamreader jsonreader

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

反向流读取器

我有一个应用程序,我已经负责清理后.应用程序本身相对简单 - 它运行SQL查询,使用Web服务,并将结果吐出到日志文件中.我的工作是在应用程序完成后将文件存档到我们的NAS.它将文件独占锁定,直到完成它们,因此它增加了一点点复杂性.我也不被允许触摸应用程序,只有日志.无论如何,我的申请很简单:

  1. 检查文件是否可以打开(捕获IOException)并在bool []中将其标记为可访问,如果没有抛出异常.
  2. 浏览标记为true的文件数组,使用ReadLine方法将文件的每一行读入StreamReader.因为应用程序偶尔打嗝并且没有完成,我不能简单地使用IOException来判断文件是否已完成 - 我必须实际解析文本.
  3. 如果找到指示完成的文本,请压缩文件,将存档文件加载到NAS,然后删除原始文件.

我的代码工作,它只是非常耗时(日志文件大约500 MB).我对改进的想法涉及从文件底部而不是从顶部开始我的搜索,但StreamReader不支持这样的方法.我不能使用ReadToEnd方法,然后反向读取,因为这只会引发内存不足异常.有什么想法,我可以加快解析日志文件?

c# parsing streamreader

5
推荐指数
1
解决办法
2943
查看次数

C#阅读网页内容Streamreader

我需要在streamreader中读取网页的内容

www.example.com

<test>
<sample></sample>
</test>
Run Code Online (Sandbox Code Playgroud)

我懂了:

System.IO.StreamReader StreamReader1 =
new System.IO.StreamReader("www.example.com");
string test = StreamReader1.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)

但我然后我得到这个错误代码

尝试访问该方法失败:System.IO.StreamReader..ctor(System.String)

c# url streamreader web

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

C#向文本文件添加行号

我正在尝试在 C# 中读取文本文件并将行号添加到行中。

这是我的输入文件:

    This is line one
    this is line two
    this is line three
Run Code Online (Sandbox Code Playgroud)

这应该是输出:

    1 This is line one
    2 this is line two
    3 this is line three
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我的代码:

class Program
{
    public static void Main()
    {
        string path = Directory.GetCurrentDirectory() + @"\MyText.txt";

        StreamReader sr1 = File.OpenText(path);

        string s = "";

        while ((s = sr1.ReadLine()) != null)           
        {
            for (int i = 1; i < 4; i++)
                Console.WriteLine(i + " " + s);
            }

            sr1.Close();
            Console.WriteLine(); …
Run Code Online (Sandbox Code Playgroud)

c# streamwriter streamreader

5
推荐指数
1
解决办法
6711
查看次数

使用正则表达式读入文件?

这与我早先的一个问题相关.

从本质上讲,该问题的解决方案效果很好,但现在我需要使其适应更大的分析应用程序.简单地使用StreamReader.ReadToEnd()是不可接受的,因为我将要阅读的一些文件非常非常大.如果出现了错误并且有人忘记清理,理论上它们可能是千兆字节.显然,我不能只读到最后.

不幸的是,正常的读取行也是不可接受的,因为我读入的一些数据行包含堆栈跟踪 - 它们显然/r/n在它们的格式化中使用.理想情况下,我想告诉程序向前读取,直到匹配正则表达式,然后返回.在.net中有任何功能吗?如果没有,我可以就如何编写它获得一些建议吗?

编辑:为了更容易理解我的问题,这里贴了一些改编代码的重要部分:

foreach (var fileString in logpath.Select(log => new StreamReader(log)).Select(fileStream => fileStream.ReadToEnd()))
{
    const string junkPattern = @"\[(?<junk>[0-9]*)\] \((?<userid>.{0,32})\)";
    const string severityPattern = @"INFO|ERROR|FATAL";
    const string datePattern = "^(?=[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})";
    var records = Regex.Split(fileString, datePattern, RegexOptions.Multiline);
    foreach (var record in records.Where(x => string.IsNullOrEmpty(x) == false))
    ......
Run Code Online (Sandbox Code Playgroud)

问题在于Foreach. .Select(fileStream => fileStream.ReadToEnd())会严重炸掉记忆,我才知道.

.net c# regex streamreader

5
推荐指数
1
解决办法
2034
查看次数

带StreamReader.ReadLine的GZipStream只读取第一行

我有一个包含需要清理的txt文件的gzip文件.我想逐行读取GZipped文件,然后将清理后的内容一次性写入输出GZIP文件,如下所示:

    void ExtractAndFix(string inputPath, string outputPath) {
        StringBuilder sbLine = new StringBuilder();

        using (GZipStream gzInput = new GZipStream(new FileStream(inputPath, FileMode.Open), System.IO.Compression.CompressionMode.Decompress)) {
            using (StreamReader reader = new StreamReader(gzInput, Encoding.UTF8)) {
                using (GZipOutputStream gzipWriter = new GZipOutputStream(new FileStream(outputPath, FileMode.Create))) {
                    string line = null;
                    while ((line = reader.ReadLine()) != null) {
                        sbLine.Clear();
                        sbLine.Append(line.Replace("\t", " "));
                        sbLine.Append("\r\n");
                        byte[] bytes = Encoding.UTF8.GetBytes(sbLine.ToString());
                        gzipWriter.Write(bytes, 0, bytes.Length);
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,在while循环中调用line = reader.ReadLine()只读取一次然后返回null(reader EOS = true).我已经尝试使用本机C#压缩库和ICSharpCode包,我得到了相同的行为.我意识到我总是可以提取完整的文件,然后清理它,然后重新压缩它,但我不想浪费资源,硬盘空间等.注意:这些是大文件(压缩到几GB)所以任何事情使用MemoryStream不是一个好的解决方案.以前有没有人遇到过这样奇怪的事?谢谢.

c# compression gzip streamreader gzipstream

5
推荐指数
1
解决办法
2222
查看次数

当任一应用程序没有控制台或控制台输入已从文件重定向时,无法读取密钥。试试 Console.Read

无法让程序读取文件,不确定我在这里做错了什么,欢迎提供任何建议。尝试为地址簿做一个项目,该文件包含所有人员的姓名、地址和电话号码。

 public Form1()
        {
            string filename = "Addresses.txt";
            ReadFile(filename);
            ReadTokens(filename);
        }//end of main

        static void ReadFile(string filename)
        {
            StreamReader readFile;
            readFile = File.OpenText(filename);
            while (!readFile.EndOfStream)
            {
                Console.WriteLine(readFile.ReadLine());
            }//end of while
            {
                readFile.Close();
                Console.ReadKey();
            }//End of read file
        }
        static void ReadTokens(string filename)
        {
            StreamReader readFile;
            string line;
            char[] delim = { ',' };
            readFile = File.OpenText(filename);
            while (!readFile.EndOfStream)
            {
                line = readFile.ReadLine(); // reads one line at a time
                string[] tokens = line.Split(delim);
                foreach (string str in tokens)
                {
                    Console.WriteLine(str …
Run Code Online (Sandbox Code Playgroud)

c# streamreader

5
推荐指数
0
解决办法
5802
查看次数

Powershell 标准输出缓冲区对于外部命令来说太小

提前一些有用的信息。我试图做的是使用 powershell start-process 和 System.diagnostics.ProcessStartInfo 读取外部命令(特别是 steamcmd)的输出。

我遇到的是 RedirectStandardOutput 缓冲区限制为 4096 字节。我从 steamcmd 获得的输出超出了该缓冲区,因此我只获得了我需要的一部分。除了调用 steamcmd 之外,我没有其他方法来获取此数据。

如果您有 steamcmd(免费)并运行它,您也可以看到输出。

steamcmd +login anonymous +app_info_update 1 +app_info_print 443030 +quit
Run Code Online (Sandbox Code Playgroud)

这将下载有关该 appid 的所有清单信息。

我尝试重定向到一个文件和一个变量,两者都按预期工作,只是它被缓冲区缩短了。System.Diagnostics.Process 中似乎也没有 powershell 方法来等待 OutputDataReceived 事件。

使用的代码(从另一个 STackOverflow 问题中窃取)

$psi = New-object System.Diagnostics.ProcessStartInfo 
$psi.CreateNoWindow = $true 
$psi.UseShellExecute = $false 
$psi.RedirectStandardOutput = $true 
$psi.RedirectStandardError = $true 
$psi.FileName = "C:\Path\to\SteamCMD.exe"
$psi.Arguments = "+login anonymous +app_info_update 1 +app_info_print 443030 +quit"
$process = New-Object System.Diagnostics.Process 
$process.StartInfo = $psi 
[void]$process.Start()
$output = $process.StandardOutput.ReadToEnd()
$process.WaitForExit() 
$output
Run Code Online (Sandbox Code Playgroud)

我认为实际的问题是 …

powershell stdout streamreader redirectstandardoutput

5
推荐指数
1
解决办法
1898
查看次数

为什么会出现空字节?即使在“消毒”流之后

我一直在想为什么某些字符串中会出现空字节。下面举例。

{"gender":"fema\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000le"}
Run Code Online (Sandbox Code Playgroud)

我基本上从 HTTP 请求中包装了一个 io.Reader 并解码为一个结构体。见下文

func bodyToStruct(res *http.Request, v gojay.UnmarshalerJSONObject) error {
    var reader io.ReadCloser
    var err error
    switch res.Header.Get("Content-Encoding") {
    case "gzip":
        reader, err = pool.Gzip.GetReader(res.Body)
        if err != nil {
            return err
        }
        defer pool.Gzip.PutReader(reader)
    case "deflate":
        reader = flate.NewReader(res.Body)
        defer reader.Close()
    default:
        reader = res.Body
    }

    decoder := gojay.BorrowDecoder(streams.NewNullByteRemoverStream(reader)) //wrapped in NewNullByteRemoverStream
    defer decoder.Release()

    return decoder.DecodeObject(v)
}
Run Code Online (Sandbox Code Playgroud)

我尝试了多种方法来尝试删除空字节,我假设它们是来自 Android 客户端的请求。

从早期堆栈线程的帮助中,我能够将以下实现部署到生产中,以尝试删除空字节。

package streams

import (
    "io"
)

// NullByte is a stream wrapper that should …
Run Code Online (Sandbox Code Playgroud)

null streamreader go

5
推荐指数
1
解决办法
230
查看次数

在流上使用 SeekOrigin.Begin 时不支持指定的方法

StreamReader稍后将使用它来反序列化我的请求,如下所示 -

sStream对象 -

using (var reader = new StreamReader(s))
{
    using (var jsonReader = new JsonTextReader(reader))
    {
        var ser = new JsonSerializer();
        return ser.Deserialize<T>(jsonReader);
    }
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但现在我试图在控制台上打印实际的请求正文,所以我尝试这样做,但它给了我一个错误 -

using (var reader = new StreamReader(s))
{
    string body = reader.ReadToEnd();
    // print body on the console which is what I am trying to do
    // reset to start of stream
    // this line gives me error
    s.Seek(0, SeekOrigin.Begin);
    using (var jsonReader = new JsonTextReader(reader)) …
Run Code Online (Sandbox Code Playgroud)

c# streamreader

5
推荐指数
1
解决办法
7603
查看次数