Hazem Abdullah
Hazem Abdullah

Reputation: 1893

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 ?

Upvotes: 2

Views: 2550

Answers (2)

Yasser Fatehy
Yasser Fatehy

Reputation: 1

"Use This Code"

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

file.AutoFlush = true;

Upvotes: 0

Pawel Cioch
Pawel Cioch

Reputation: 3184

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

Upvotes: 1

Related Questions