1

誰かが私に少し助けてくれることを願っていましたか?テキストファイルの行を変更する次のコーディングがあります。基本的に、テキストボックスから数値を読み取り、テキストボックスの数値と一致する行数で別のテキストファイルを作成します。コーディングはうまくいきます。

Dim lines() As String = IO.File.ReadAllLines("C:\test1.txt")
IO.File.WriteAllLines("C:\test1.txt", lines.Skip(CInt(TextBox1.Text)).toarray)
IO.File.WriteAllLines("C:\test2.txt", lines.Take(CInt(TextBox1.Text)).toarray)

今、私はいくつかの変更を加えたいと思っていました.私が望むのは、テキストボックスから数字を読み込んで、5つの別々のテキストファイルを作成することです. たとえば、テキストファイル 1 に 60 行があり、テキストボックス 1 に番号 50 がある場合、コーディングを実行すると、次のテキストファイルが作成されます。

textfile 1 - 10 rows remaining
textfile 2 - 10 rows
textfile 3 - 10 rows
textfile 4 - 10 rows
textfile 5 - 10 rows
textfile 6 - 10 rows

textfile1 に1行しかない場合は、次のようにします

textfile 1 - 0 rows remaining
textfile 2 - 1 rows
textfile 3 - 0 rows
textfile 4 - 0 rows
textfile 5 - 0 rows
textfile 6 - 0 rows

textfile1 が 5 の場合は、次のようになります。

textfile 1 - 0 rows remaining
textfile 2 - 1 rows
textfile 3 - 1 rows
textfile 4 - 1 rows
textfile 5 - 1 rows
textfile 6 - 1 rows

textfile1 が 4 の場合、次のようになります。

textfile 1 - 0 rows remaining
textfile 2 - 1 rows
textfile 3 - 1 rows
textfile 4 - 1 rows
textfile 5 - 1 rows
textfile 6 - 0 rows

など これは可能ですか?どうもありがとう

4

2 に答える 2

1

わかりました、試してみますが、あなたの要件はSOにはローカライズされすぎており、他の人にとって役立つことはまずありません. あなたのコメントは質問への回答を可能にします。

1 つのアプローチは、LINQ を使用して行をIndex Mod FileCount次のようにグループ化することです。

Dim lineCount = Decimal.ToInt32(NumericUpDown1.Value)
Dim fileCount = Decimal.ToInt32(NumericUpDown2.Value)
Dim file1Lines = IO.File.ReadAllLines("C:\test1.txt")
Dim newFile1Lines = file1Lines.Skip(lineCount)
Dim lineGroups = (
    file1Lines.Take(lineCount).
               Select(Function(l, i) New With {.Line = l, .Index = i}).
               GroupBy(Function(x) x.Index Mod fileCount).
               Select(Function(grp) New With {
                          .FileIndex = grp.Key,
                          .Lines = grp.Select(Function(x) x.Line)
              })) 

IO.File.WriteAllLines("C:\test1.txt", newFile1Lines)
For i = 0 To fileCount - 1
    Dim path = String.Format("C:\test{0}.txt", i + 2)
    Dim lineGroup = lineGroups.FirstOrDefault(Function(lg) lg.FileIndex = i)
    If lineGroup Is Nothing Then
        IO.File.WriteAllLines(path, {""})
    Else
        IO.File.WriteAllLines(path, lineGroup.Lines)
    End If
Next
于 2012-07-02T20:57:37.423 に答える
1

さらに低いメモリ消費。一度に RAM に格納されるのは 1 行だけです。

Dim files As Integer = 5
Dim lines As Integer = 50

Using rdr As New IO.StreamReader("C:\test1.txt")

    Dim output(files) As StreamWriter
    For i As Integer = 0 To files
       output(i) = New StreamWriter(String.Format("C:\test{0}.txt",i+1))
    Next i

    Try

        Dim currentStream As Integer = 1
        Dim line As String
        While (line = rdr.ReadLine()) <> Nothing
           If lines > 0 Then
               output(currentStream).WriteLine(line)
               currentStream += 1
               If currentStream > files Then currentStream = 1
               lines -= 1
           Else
               output(0).WriteLine(line)
           End If

        End While

    Finally
        For Each writer As StreamWriter In output
            writer.Close()
        Next writer
    End Try
End Using
于 2012-07-02T21:30:56.890 に答える