修改文本文件中的数据

时间:2012-06-01 17:50:29

标签: java android file-io text-files edit

创建了一个存储记录的文件,其中数据以“,”结尾,以“;”结尾。

到目前为止,我已经能够附加到该文件,但现在我需要更新记录中的特定元素。每条记录都以唯一标识符开头。

下面是一个如何打开文件并将每个唯一ID读入数组的示例:

DataInputStream fin = new DataInputStream(openFileInput("Updates.txt"));
try {
    for (;;) {
    String record = fin.readUTF();
    Log.d(DEBUG_TAG, "Record "+record);
    elements = record.split(",");
    String trackingNo = elements[0];
    UpdateRecord item = new UpdateRecord(trackingNo);
    }
}
catch (EOFException e) {
    Log.i("Data Input Sample", "End of file reached");
}
fin.close();

1 个答案:

答案 0 :(得分:1)

Sub example1()
Dim strFinal As String
Dim strline As String

Open "D:\textfile.txt" For Input As #1
    While EOF(1) = False
        Line Input #1, strline
        If Len(strline) > 24 Then
            strFinal = strFinal + ModifyColumn(strline)
            Else
            strFinal = strFinal + strline + vbCrLf
        End If
    Wend
    strFinal = strFinal
Close #1

Open "D:\textfile.txt" For Output As #1
    Print #1, strFinal
Close #1
End Sub


Function ModifyColumn(ByVal strInput As String) As String
    Dim arrString() As String
    Dim strOutput As String
    'split the columns
    arrString = Split(strInput, vbTab)
    'concatenate the first 2 column as they are
    strOutput = arrString(0) + vbTab + arrString(1) + vbTab + arrString(2)
    'add 100$ to column3
    requirevalue = Left(arrString(3), InStr(1, arrString(3), "|") - 1)
    last3Digit = Right(requirevalue, 3)
    If Left(requirevalue, 3) = "max" Then
        Newvalue = vbTab + "OTPxxxxxx" & last3Digit & "|" & Right(arrString(3), Len(arrString(3)) - InStr(1, arrString(3), "|")) + vbCrLf
    Else
        Newvalue = vbTab + arrString(3) + vbCrLf
    End If
    strOutput = strOutput & Newvalue
    'strOutput = strOutput + Strings.Trim(Str(CDbl(Left(arrString(3), Len(arrString(2)) - 1)) + 100)) + "$" + vbCrLf
    ModifyColumn = strOutput
End Function`enter code here`