简体   繁体   中英

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

I am using Json for a big array, and the result of Json is on "sz"(string).

When I want to save the Json result ( string sz ) on a file it will not save the whole string, why?

Note the string value is about 184985 characters.

Here is the code what i have tried so far :

 string sz = JsonConvert.SerializeObject(tempArray);

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

I tried to use

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

And

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

The file size is just 200K.

Is there a limit in text file in windows ?

  • "Is there a limit in text file in windows ?"
  • No (well 2GB at FAT32 system)

I'm not sure what the problem is, but I can share my snippet I use all the time, let me know if works for you.

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);
    }
}

"Use This Code"

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

file.AutoFlush = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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