簡體   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