-1

My project function has return an ArrayList of various type of objects, and I have to save or store it some where for later analyzing. But I have do some research and haven't found a way to store it into database or write it to a file.

If there is any way to Save or Store an ArrayList of objects, I greatly look forward for more efficient ways to do that.

Tri Nguyen Dung
  • 929
  • 2
  • 13
  • 24

6 Answers6

2

Firstly, do you really need to be using non-generic collections? It's almost always better to use List<T> than ArrayList unless you're actually targeting .NET 1.1, which seems unlikely.

The equivalent of using ObjectOutputStream in Java is to use BinaryFormatter in .NET. You can use GZipStream for compression. Note that these are classes, not namespaces - it's worth clearing up the terminology in your mind. FileOutputStream and FileInputStream are both represented by FileStream in .NET.

It's also worth being aware that using the native binary serialization in either .NET or Java is somewhat fragile in terms of versioning. You may want to consider using alternatives such as JSON, Protocol Buffers, Thrift, XML etc.

Personally I would recommend that you learn C# and .NET from scratch, rather than trying to port Java code without really knowing the idioms of the language and platform you're porting to. For example, you definitely want to use using statements rather than using "manual" finally blocks to dispose of resources.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I'm sorry for mistake in classes. Because I got an error in my code and it said "The type or namespace name 'FileOutputStream' could not be found" so I called it namespaces but not class. – Tri Nguyen Dung Mar 08 '13 at 03:26
1

I found some interesting threads on stackoverflow:

How to quickly save/load class instance to file

Saving from List<T> to txt

I think serialization is what you need:

Community
  • 1
  • 1
Alina B.
  • 1,256
  • 8
  • 18
  • Your recommence is helpful to me. I'm doing some research on your recommendation. I will return with a specific solution for this issue. – Tri Nguyen Dung Mar 08 '13 at 04:23
1

The following will compress object and save to file:

using System;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;

public static void WriteFile<T>(T obj, string path) 
{ 
    using (FileStream fs = File.OpenWrite(path))
    using (GZipStream gzip = new GZipStream(fs, CompressionMode.Compress))
    {
        BinaryFormatter bf = new BinaryFormatter(); 
        bf.Serialize(gzip , obj); 
    }
} 

public static T LoadFile<T>(string path) 
{  
    using (FileStream fs = File.OpenRead(path))
    using (GZipStream gzip = new GZipStream(fs, CompressionMode.Decompress))
    {
        BinaryFormatter bf = new BinaryFormatter(); 
        return (T)bf.Deserialize(gzip); 
    }
}

And read and decompress back.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • I have got a lot of error when passing your code into my program. – Tri Nguyen Dung Mar 08 '13 at 03:59
  • I got error at new FileStream(path); fs is not exist in current context. Also path, T, gzip (T is a parameter but used as a variable. – Tri Nguyen Dung Mar 08 '13 at 04:35
  • May I convert element of ArrayList in to a Class, and then use List to store it into a file? – Tri Nguyen Dung Mar 08 '13 at 04:37
  • Thank too much for your code. It really good for me, I think it is incredible. I am also write a code to save an ArrayList but cannot save an object as your code have done. I'm so lucky! Also I have test your code. It run very good and no error has found. Thank you again for your help. – Tri Nguyen Dung Mar 08 '13 at 05:56
0

up until now I haven't got a good solution for my issue. But I have do some research on suggestions of Alina B. and gain some more basic information to do this. I have a code to save a collection of object to file. (I have change my target to use Collection but not ArrayList to make it easier in this context.

//Here I call the method with parameter of Collection of objects
Collection<object> list = new Collection<object>();
list.Add("value1");
list.Add("value2");
list.Add("value3");
SaveListToFile(list);

public void SaveListToFile(Collection<object> list)
{
    var serializer = new XmlSerializer(typeof(Collection<object>));
    using (var writer = new StreamWriter(Server.MapPath("~/Files/test.xml")))
        serializer.Serialize(writer, list);
}

In the method above, I am using XmlSerializer to write the collection of object into an xml file. The method is run very good if every element in Collection is a string. But if I add any other object to the collection like a class, or ArrayList, or some other object type it will generate an error (There was an error generating the XML document.) and I can't save the collection at all. So how can I save a collection of objects like ArrayList, class, integer, or other type but not string? I need some more help if you are here.

Tri Nguyen Dung
  • 929
  • 2
  • 13
  • 24
0

Maybe you can use binary serialization and save a binary file? Then you can use gzip to compress it. Here's a link to an example

0

Do you really need XML? Can you try binary, there is a lot of daata there that you can't just save into an XML?