读取和写入二进制文件

时间:2014-02-03 08:22:54

标签: android vb.net

我想阅读并编写一个Lowrance .usr文件。这是一个具有特定数据结构的二进制文件,我已经设法解码并编写了一个Visual Basic程序,可以进行必要的读写操作。我正在将此软件迁移到我的Android手机。

我在VB中定义的结构是:

   Public Structure WAYPOINT
    Public wWaypointNum As Integer
    Public dwLatitudeMM As Long
    Public dwLongitudeMM As Long
    Public dwAltitudeFT As Long
    Public dwWaypointNmLength As Long
    Public abWaypointName As Char()
    Public wWaypointDescLength As Long
    Public abWaypointDescription As Char()
    Public dwWaypointTime As Long
    Public dwWaypointSymbol As Long
    Public wWaypointStatus As Integer
End Structure

然后使用以下代码读取文件:

   Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
    Dim i As Integer
    Dim objBR As BinaryReader
    Dim objFS As FileStream
    Dim objWP As New WAYPOINT
    Dim objwVer As Integer
    Dim objwSVer As Integer
    Dim objNoWaypoints As Integer
    Dim objNoRoutes As Integer
    Dim TempLat As Double
    Dim TempLng As Double
    Dim LatString As String
    Dim LngString As String
    objFS = New FileStream("c:\Binary\Binary File\Binary File\waverider.usr", FileMode.Open)
    objBR = New BinaryReader(objFS)
    objBR.BaseStream.Seek(0, SeekOrigin.Begin)
    objwVer = objBR.ReadInt16
    objwSVer = objBR.ReadInt16
    objNoWaypoints = objBR.ReadInt16
    For i = 1 To objNoWaypoints
        objWP = Nothing
        With objWP
            .wWaypointNum = objBR.ReadInt16
            .dwLatitudeMM = objBR.ReadInt32
            .dwLongitudeMM = objBR.ReadInt32
            .dwAltitudeFT = objBR.ReadInt32
            .dwWaypointNmLength = objBR.ReadInt32
            .abWaypointName = objBR.ReadChars(.dwWaypointNmLength)
            .wWaypointDescLength = objBR.ReadInt32
            .abWaypointDescription = objBR.ReadChars(.wWaypointDescLength)
            .dwWaypointTime = objBR.ReadInt32
            .dwWaypointSymbol = objBR.ReadInt32
            .wWaypointStatus = objBR.ReadInt16
        End With
        TempLat = objWP.dwLatitudeMM / EarthRad
        TempLat = Math.Exp(TempLat)
        TempLat = (2 * Math.Atan(TempLat)) - (Math.PI / 2)
        TempLat = TempLat * RadToDeg
        LatString = DegToDecMin(TempLat, "Lat")
        TempLng = objWP.dwLongitudeMM / EarthRad
        TempLng = Math.Exp(TempLng)
        TempLng = (2 * Math.Atan(TempLng)) - (Math.PI / 2)
        TempLng = TempLng * RadToDeg
        LngString = DegToDecMin(TempLng, "Lng")
        With lvWaypoints
            Dim lv As ListViewItem = .Items.Add(objWP.abWaypointName)
            With lv
                .SubItems.Add(objWP.abWaypointDescription)
                .SubItems.Add(LatString)
                .SubItems.Add(LngString)
            End With
        End With
    Next i
    '\\ Read Routes
    'objNoRoutes = objBR.ReadInt16 'Number of Routes
End Sub

然后用以下方法解码数据:

   Public Function DegToDecMin(ByVal DecDeg As Double, ByVal LatLng As String) As String
    Dim ReturnValue As String
    Dim Pos As String
    If LatLng = "Lat" Then
        If DecDeg < 0 Then Pos = "S" Else Pos = "N"
    Else
        If DecDeg < 0 Then Pos = "W" Else Pos = "E"
    End If
    Dim mDeg As Double = Math.Abs(DecDeg)
    Dim mWholeDeg As Double = Math.Truncate(mDeg)
    Dim mRemDegS As String = String.Format("{0:##.000}", (mDeg - mWholeDeg) * 60)
    If InStr(mRemDegS, ".") = 2 Then mRemDegS = "0" & mRemDegS
    If InStr(mRemDegS, ".") = 1 Then mRemDegS = "00" & mRemDegS
    ReturnValue = mWholeDeg.ToString & Chr(176) & mRemDegS & "'" & Pos
    Return ReturnValue
End Function

Java中是否存在可以实现相同目标的类似功能

1 个答案:

答案 0 :(得分:0)

对于Java,只有语法是根据Java语法进行更改,

首先,你需要创建一个如下所示的bean文件,

public class WayPoint
{
    public int wWaypointNum;
    public int dwLatitudeMM;
    public dwLongitudeMM;
    ...
}

其次,您需要File API来读写文件。为此,您可以使用FileFileInputStreamFileOutputStreamDataInputStreamDataOutputStream