- 題名: ランダムファイルアクセスやっております。
- 日時: 2014/04/13 19:50:33
- ID: 32257
- この記事の返信元:
- (なし)
- この記事への返信:
- [32258] Re[1]: ランダムファイルアクセスやっております。2014/04/13 21:03:44
- ツリーを表示
2014/04/16(Wed) 14:06:07 編集(投稿者) ■No32261に返信(ak47さんの記事) エラーチェックなしですがサンプルです。 Button1=>新規保存 Button2=>既存上書き Button3=>読込 RecLengthがレコード長なので適宜変更して下さい。 Imports System.IO Imports System.Text Public Class Form1 Private Const RecLength As Integer = 100 Private Sub ReadRecord() Dim RecNo = CInt(TextBox1.Text) Using rd As New BinaryReader(File.OpenRead("C:\temp\Address.Dat")) rd.BaseStream.Seek((RecNo - 1) * RecLength, SeekOrigin.Begin) Dim BytRecord() As Byte = rd.ReadBytes(RecLength) Using rd2 As New BinaryReader(New MemoryStream(BytRecord), Encoding.UTF8) TextBox2.Text = rd2.ReadString TextBox3.Text = rd2.ReadString TextBox4.Text = rd2.ReadString TextBox5.Text = rd2.ReadString rd2.Close() End Using rd.Close() End Using End Sub Private Sub WriteNew() Using wt As New BinaryWriter(File.Open("C:\temp\Address.Dat", FileMode.Append)) Dim BytRecord(RecLength - 1) As Byte Using wt2 As New BinaryWriter(New MemoryStream(BytRecord), Encoding.UTF8) wt2.Write(TextBox2.Text) wt2.Write(TextBox3.Text) wt2.Write(TextBox4.Text) wt2.Write(TextBox5.Text) wt2.Close() End Using wt.Write(BytRecord) wt.Close() End Using End Sub Private Sub UpdateRecord() Dim RecNo = CInt(TextBox1.Text) Using wt As New BinaryWriter(File.OpenWrite("C:\temp\Address.Dat")) wt.Seek((RecNo - 1) * RecLength, SeekOrigin.Begin) Dim BytRecord(RecLength-1) As Byte Using wt2 As New BinaryWriter(New MemoryStream(BytRecord), Encoding.UTF8) wt2.Write(TextBox2.Text) wt2.Write(TextBox3.Text) wt2.Write(TextBox4.Text) wt2.Write(TextBox5.Text) wt2.Close() End Using wt.Write(BytRecord) wt.Close() End Using End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click WriteNew() End Sub Private Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click UpdateRecord() End Sub Private Sub Button3_Click(sender As Object, e As System.EventArgs) Handles Button3.Click ReadRecord() End Sub End Class
分類:[.NET]