Some method to save data from an Arraylist

Asked

Viewed 3,231 times

2

First of all, some information:

= .NET Framework 4.0
= Visual C#

I’m simulating a database using a class specifically for this, using a List<string[]>, however, I would like to be able to store and save this data even after closing the program, I thought of a solution like generating a file .txt and when the application starts, read that file and write the data that is in it, in the List<string[]>.

If there is no more practical solution (besides using a database, in fact), I would like to know how to generate this file .txt and how it could make the application read it, if there is a more practical solution, what it would be and how it could put it into practice.

  • Serialize the class and save to a file.

2 answers

2


There are several ways to implement this, for example, or text (*. txt).

Json

Install the package Json.NET, with the package manager Nuget: inserir a descrição da imagem aqui

After such installation you can from a structure, an object, etc. generate a json of it and write to a file with the extension .json

Coding:

Serialize

//objeto criado
List<string[]> objListString = new List<string[]>();
objListString.Add(new string[] { "1", "valor 1" });
objListString.Add(new string[] { "2", "valor 2" });
objListString.Add(new string[] { "3", "valor 3" });

//serializando objeto no formato Json
var data = JsonConvert.SerializeObject(objListString);

//gravando informação em um arquivo na pasta raiz do executavel
System.IO.StreamWriter writerJson = System.IO.File.CreateText(".\\base.json");
writerJson.Write(data);
writerJson.Flush();
writerJson.Dispose();

After running this code, you will have a file inside the folder \bin with the base.json name in this format:

inserir a descrição da imagem aqui

Deserialize

To do the reverse process just read the file again and use JsonConvert.DeserializeObject thus:

String dataJson = System.IO.File.ReadAllText(".\\base.json", Encoding.UTF8);
List<string[]> retorno = JsonConvert.DeserializeObject<List<string[]>>(dataJson);

Upshot

inserir a descrição da imagem aqui


Text

Coding

To generate the same example in a file format .txt it’s simple, note code:

System.IO.StreamWriter writerTxt = System.IO.File.CreateText(".\\base.txt");
foreach(string[] item in objListString.ToArray())
{
     writerTxt.WriteLine(String.Join(";", item));
}
writerTxt.Flush();
writerTxt.Dispose();

inserir a descrição da imagem aqui

To generate the inverse txt process for an object in your program do:

String[] dataTxt = System.IO.File.ReadAllLines(".\\base.txt", Encoding.UTF8);
foreach (String linha in dataTxt)
{
      String[] itens = linha.Split(';');
      if (itens.Count() > 0)
      {
         objListString.Add(itens);
      }
}

inserir a descrição da imagem aqui

The two ways are viable alternatives, but if it were to choose would put in .

  • Is there any way to create only one file .json for several List<string[]> serialized?

  • And just to clarify, the List<string[]> retorno could be replaced by objListString?

  • Yes, there will depend a lot on programming, on the second question return and objListString are of the same type List<string[]>, so can replace in programming smoothly. The proposed response is an example that resembles your reality, but of course you should put into practice in your model.

  • Just to make it even clearer, what is the best place to place the deserialization process? In the main class builder, perhaps?

  • This could be it

2

You can serialize the object to JSON, save to a file, then read the file and deserialize the string using Json.net.

string json = JsonConvert.SerializeObject(objeto);
List<string[]> objeto = JsonConvert.DeserializeObject<List<string[]>>(json);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.