.net对文件操作

''' -----------------------------------------------------------------------------
''' Project  : SHYC.PublicMethods
''' Class  : PublicMethods.FileOperateMethods
'''
''' -----------------------------------------------------------------------------
''' <summary>
'''     文件的相关操作方法
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
'''  [陈立功] 2006-3-25 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class FileOperateMethods
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    '''     读取指定文件数据到Byte数组中
    ''' </summary>
    ''' <param name="fileName">指定文件名</param>
    ''' <returns>Byte数组</returns>
    ''' <remarks>
    '''     <para>不对文件的正确性进行判断,调用者自行捕获错误</para>
    ''' </remarks>
    ''' <history>
    '''  [] 2006-3-25 Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function GetFileDataToByte(ByVal fileName As String) As Byte()
        Dim fs As System.IO.FileStream
        Dim w As System.IO.BinaryWriter
        Dim fr As System.IO.BinaryReader
        Dim iLen As Long
 
        Try
            fs = New System.IO.FileStream(fileName, System.IO.FileMode.Open)
            w = New System.IO.BinaryWriter(fs)
 
            iLen = fs.Length()
 
            Dim b(iLen) As Byte
 
            fr = New System.IO.BinaryReader(fs)
            b = fr.ReadBytes(iLen)
 
            w.Close()
            fr.Close()
 
            Return b
        Catch ex As Exception
            ErrorOperate.Writelog(fileName & ":" & ex.Message)
 
            If Not IsNothing(w) Then w.Close()
            If Not IsNothing(fs) Then fs.Close()
 
            Throw ex
        End Try
    End Function
 
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    '''     将二进制数据写入到指定文件中
    ''' </summary>
    ''' <param name="fileName">指定文件名</param>
    ''' <param name="fileData">二进制数据</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    '''  [] 2006-3-25 Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Sub WriteByteToFile(ByVal fileName As String, ByVal fileData() As Byte)
 
        Dim tempFileName As String
        tempFileName = fileName.Trim & ".temp"
 
        Dim fs As System.IO.FileStream
        Dim w As System.IO.BinaryWriter
 
        Try
            '若文件已存在,先备份文件
            If System.IO.File.Exists(fileName) Then
                If System.IO.File.Exists(tempFileName) Then
                    System.IO.File.Delete(tempFileName)
                End If
                System.IO.File.Copy(fileName, tempFileName)
                System.IO.File.Delete(fileName)
            End If
 
            fs = New System.IO.FileStream(fileName, System.IO.FileMode.CreateNew)
            w = New System.IO.BinaryWriter(fs)
            w.Write(fileData)
 
            w.Close()
            fs.Close()
 
            '删除备份文件
            If System.IO.File.Exists(tempFileName) Then System.IO.File.Delete(tempFileName)
        Catch ex As Exception
            ErrorOperate.Writelog(fileName & ":" & ex.Message)
 
            If Not IsNothing(w) Then w.Close()
            If Not IsNothing(fs) Then fs.Close()
 
            '有错误时从备份文件恢复原始文件
            If System.IO.File.Exists(tempFileName) Then
                System.IO.File.Copy(tempFileName, fileName)
                System.IO.File.Delete(tempFileName)
            End If
 
            Throw ex
        End Try
    End Sub
End Class

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值