繁体   English   中英

使用带有长字符串 Json 的 System.IO.StreamWriter 的 C# 文件限制

[英]C# file limit using System.IO.StreamWriter with long string Json

我将 Json 用于一个大数组,而 Json 的结果在"sz"(string).

当我想将 Json 结果( string sz )保存在文件中时,它不会保存整个字符串,为什么?

请注意,字符串值大约为 184985 个字符。

这是我迄今为止尝试过的代码:

 string sz = JsonConvert.SerializeObject(tempArray);

 using (StreamWriter file = File.AppendText("json.txt"))
 {
      file.Write(sz);
 }

我试着用

File.AppendAllText(@"json.txt", sz);

System.IO.StreamWriter file = new System.IO.StreamWriter("json.txt", true);

文件大小仅为 200K。

windows中的文本文件有限制吗?

  • “windows 中的文本文件有限制吗?”
  • 否(在 FAT32 系统中为 2GB)

我不确定问题是什么,但我可以分享我一直使用的片段,如果对你有用,请告诉我。

public class JsonFile 
{
    public JsonFile()
    {
        formatting = Formatting.Indented;
    }

    public JsonFile(Formatting formatting)
    {
        this.formatting = formatting;
    }

    private Formatting formatting;

    public T Load<T>(string filename)
    {
        string json = File.ReadAllText(filename);
        return JsonConvert.DeserializeObject<T>(json);
    }

    public void Save(string filename, object data)
    {
        File.WriteAllText(filename, JsonConvert.SerializeObject(data, formatting), Encoding.UTF8);
    }
}

“使用此代码”

System.IO.StreamWriter file = new System.IO.StreamWriter(@"myFile.txt");

file.AutoFlush = true;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM