现在定义了一个数组:
myClass[] myArray=new myClass[10];其中myClass是自己写的一个类.现在想把赋好数据的myArray保存到文件中,但是FileWriter/Reader和StreamWriter/Reader好像都是针对字符(串)或字节操作的,怎么才能把myArray保存进文件,而以后又能从文件中直接读取呢?

解决方案 »

  1.   

    可以把类先序列化为byte[],然后把byte[] 写入文本中!
      

  2.   

    你可以把写入一个csv文件.循环一行一行向里写
     Dim fs As FileStream
            Dim sw As StreamWriter
                  
                        fs = New FileStream(路径及名称, FileMode.Create, 
                        strAll = strAll.Substring(1, strAll.Length - 1)
                        sw = New StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"))
                        sw.WriteLine(stAall)                    sw.Close()
      

  3.   

    这是读csv文件,返回dataset
    Public Function GetCSV(ByVal _CSVPath As String) As DataSet        Dim CSVDataSet As DataSet
            CSVDataSet = New DataSet        Dim intClnCount As Integer    'the length of string()
            Dim CSVDataTable As System.Data.DataTable
            CSVDataTable = New System.Data.DataTable("myDataTable")        Dim mydc As DataColumn  'column object
            Dim mydr As DataRow     'row object        Dim CSVPath As String
            CSVPath = _CSVPath      'csv file's path        Dim strLine As String
            strLine = ""        Dim strAryLine() As String  'a string(),read csv file's records into it
            Dim mysr As System.IO.StreamReader
            mysr = New System.IO.StreamReader(CSVPath)        Dim j = 0
            mysr.BaseStream.Seek(0, SeekOrigin.Begin)
            While (mysr.Peek > 0)            Try
                    strLine = mysr.ReadLine()
                    Dim mySplit As Char
                    mySplit = ","
                    strAryLine = strLine.Split(mySplit)
                    Console.WriteLine(strAryLine)                'get the length
                    intClnCount = strAryLine.Length
                    Dim i As Integer
                    'add the column, base on the j's value 
                    If j < intClnCount Then
                        For i = 0 To intClnCount - 1
                            mydc = New DataColumn
                            CSVDataTable.Columns.Add(mydc)
                            j = j + 1
                        Next
                    End If                'create a new row,and get the every column's value
                    mydr = CSVDataTable.NewRow()
                    For i = 0 To intClnCount - 1
                        mydr(i) = strAryLine(i)
                    Next
                    'add the row into the table
                    CSVDataTable.Rows.Add(mydr)
                    j = j + 1
                Catch ex As Exception
                    MsgBox(ex.Message())
                End Try        End While
            'add the table into the returned dataset
            CSVDataSet.Tables.Add(CSVDataTable)
            Return CSVDataSet    End Function
      

  4.   

    to zengkang:
    我以前不是学c#的,请问那个函数叫什么呢? 还有就是,怎么把读出来的byte[]还原成原来的数据结构呢? 直接赋值?to bhwhy:
    感谢贴了这么多代码,不过觉得好像有点复杂. 请问有简单一点的方法吗?
      

  5.   

    Dim fs As FileStream
            Dim sw As StreamWriter
                  
                        fs = New FileStream(路径及名称, FileMode.Create, 
                        strAll = strAll.Substring(1, strAll.Length - 1)
                        sw = New StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"))
                        sw.WriteLine(stAall)                    sw.Close()有个疑问,我要保存的是一个以对象为元素的数组,哪能调用substring方法呢? 这段代码好像不对?
      

  6.   

    strAll = strAll.Substring(1, strAll.Length - 1)
    这是我取字符串的第一到倒数第二的值。你根据需要变换一下