[英]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中的文本文件有限制嗎?
我不確定問題是什么,但我可以分享我一直使用的片段,如果對你有用,請告訴我。
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.