Vb.net 如何将文本文件的内容随机化?

Vb.net 如何将文本文件的内容随机化?,vb.net,random,text-files,Vb.net,Random,Text Files,我需要随机化文本文件中的所有行,然后通过替换相同的文本文件来保存未排序的行 我如何才能做到这一切?以下是我的看法: Dim filepath as String = "text_path" Dim arr() As String = File.ReadAlllines(filepath) Dim a As Random Dim b(str.Length) As Integer Dim result=1, c As Integer File.Delete(filepath) Dim f

我需要随机化文本文件中的所有行,然后通过替换相同的文本文件来保存未排序的行

我如何才能做到这一切?

以下是我的看法:

Dim filepath as String = "text_path"
Dim arr() As String = File.ReadAlllines(filepath)
Dim a As Random
Dim b(str.Length) As Integer
Dim result=1, c As Integer

    File.Delete(filepath)
Dim f As StreamWriter = File.AppendText(filepath)

    For i = 0 To str.Length
    while(result)
        result = 0
        c = a.Next(0, str.Length)
        For j = 0 To b.Length
            If b(j) = c Then result = 1
        Next
    end while
        f.WriteLine(arr(c))
    Next
    f.Close()
Dim linesList As New List(Of String)(IO.File.ReadAllLines("filepath"))
Dim newLinesList As New List(Of String)
Randomize()
While linesList.Count > 0
  Dim randomIndex As Integer = Math.Floor(Rnd() * linesList.Count)
  newLinesList.Add(linesList(randomIndex))
  linesList.RemoveAt(randomIndex)
End While
IO.File.WriteAllLines("filepath", newLinesList.ToArray)
Imports System.IO

Module Module1

    Sub CreateFile(destFile As String)
        Using sw = New StreamWriter(destFile)
            For i = 1 To 200
                sw.WriteLine("Line " & i.ToString)
            Next
        End Using
    End Sub

    Function RandomList(nNumbers As Integer) As List(Of Integer)
        ' generate a List of numbers from 0..nNumbers-1 in a random order.
        Dim ns As New List(Of Integer)
        Dim rnd As New Random
        For i = 0 To nNumbers - 1
            ns.Insert(rnd.Next(0, i + 1), i)
        Next
        Return ns
    End Function

    Sub RandomiseFile(srcFile As String)
        Dim lines = File.ReadAllLines(srcFile)
        Dim nLines = lines.Count
        Dim randomNumbers = RandomList(nLines)
        ' use a temporary file in case something goes wrong so that
        ' the original file is still there.
        Dim tmpFile = Path.GetTempFileName()
        ' output the lines in a random order.
        Using sw = New StreamWriter(tmpFile)
            For i = 0 To nLines - 1
                sw.WriteLine(lines(randomNumbers(i)))
            Next
        End Using
        File.Delete(srcFile)
        File.Move(tmpFile, srcFile)
    End Sub

    Sub Main()
        Dim fileToUse As String = "C:\temp\makerandom.txt"
        CreateFile(fileToUse)
        RandomiseFile(fileToUse)
    End Sub

End Module
以下是我对它的看法:

Dim linesList As New List(Of String)(IO.File.ReadAllLines("filepath"))
Dim newLinesList As New List(Of String)
Randomize()
While linesList.Count > 0
  Dim randomIndex As Integer = Math.Floor(Rnd() * linesList.Count)
  newLinesList.Add(linesList(randomIndex))
  linesList.RemoveAt(randomIndex)
End While
IO.File.WriteAllLines("filepath", newLinesList.ToArray)
Imports System.IO

Module Module1

    Sub CreateFile(destFile As String)
        Using sw = New StreamWriter(destFile)
            For i = 1 To 200
                sw.WriteLine("Line " & i.ToString)
            Next
        End Using
    End Sub

    Function RandomList(nNumbers As Integer) As List(Of Integer)
        ' generate a List of numbers from 0..nNumbers-1 in a random order.
        Dim ns As New List(Of Integer)
        Dim rnd As New Random
        For i = 0 To nNumbers - 1
            ns.Insert(rnd.Next(0, i + 1), i)
        Next
        Return ns
    End Function

    Sub RandomiseFile(srcFile As String)
        Dim lines = File.ReadAllLines(srcFile)
        Dim nLines = lines.Count
        Dim randomNumbers = RandomList(nLines)
        ' use a temporary file in case something goes wrong so that
        ' the original file is still there.
        Dim tmpFile = Path.GetTempFileName()
        ' output the lines in a random order.
        Using sw = New StreamWriter(tmpFile)
            For i = 0 To nLines - 1
                sw.WriteLine(lines(randomNumbers(i)))
            Next
        End Using
        File.Delete(srcFile)
        File.Move(tmpFile, srcFile)
    End Sub

    Sub Main()
        Dim fileToUse As String = "C:\temp\makerandom.txt"
        CreateFile(fileToUse)
        RandomiseFile(fileToUse)
    End Sub

End Module
另一种看法是:

Dim linesList As New List(Of String)(IO.File.ReadAllLines("filepath"))
Dim newLinesList As New List(Of String)
Randomize()
While linesList.Count > 0
  Dim randomIndex As Integer = Math.Floor(Rnd() * linesList.Count)
  newLinesList.Add(linesList(randomIndex))
  linesList.RemoveAt(randomIndex)
End While
IO.File.WriteAllLines("filepath", newLinesList.ToArray)
Imports System.IO

Module Module1

    Sub CreateFile(destFile As String)
        Using sw = New StreamWriter(destFile)
            For i = 1 To 200
                sw.WriteLine("Line " & i.ToString)
            Next
        End Using
    End Sub

    Function RandomList(nNumbers As Integer) As List(Of Integer)
        ' generate a List of numbers from 0..nNumbers-1 in a random order.
        Dim ns As New List(Of Integer)
        Dim rnd As New Random
        For i = 0 To nNumbers - 1
            ns.Insert(rnd.Next(0, i + 1), i)
        Next
        Return ns
    End Function

    Sub RandomiseFile(srcFile As String)
        Dim lines = File.ReadAllLines(srcFile)
        Dim nLines = lines.Count
        Dim randomNumbers = RandomList(nLines)
        ' use a temporary file in case something goes wrong so that
        ' the original file is still there.
        Dim tmpFile = Path.GetTempFileName()
        ' output the lines in a random order.
        Using sw = New StreamWriter(tmpFile)
            For i = 0 To nLines - 1
                sw.WriteLine(lines(randomNumbers(i)))
            Next
        End Using
        File.Delete(srcFile)
        File.Move(tmpFile, srcFile)
    End Sub

    Sub Main()
        Dim fileToUse As String = "C:\temp\makerandom.txt"
        CreateFile(fileToUse)
        RandomiseFile(fileToUse)
    End Sub

End Module
另一种看法是:

Dim linesList As New List(Of String)(IO.File.ReadAllLines("filepath"))
Dim newLinesList As New List(Of String)
Randomize()
While linesList.Count > 0
  Dim randomIndex As Integer = Math.Floor(Rnd() * linesList.Count)
  newLinesList.Add(linesList(randomIndex))
  linesList.RemoveAt(randomIndex)
End While
IO.File.WriteAllLines("filepath", newLinesList.ToArray)
Imports System.IO

Module Module1

    Sub CreateFile(destFile As String)
        Using sw = New StreamWriter(destFile)
            For i = 1 To 200
                sw.WriteLine("Line " & i.ToString)
            Next
        End Using
    End Sub

    Function RandomList(nNumbers As Integer) As List(Of Integer)
        ' generate a List of numbers from 0..nNumbers-1 in a random order.
        Dim ns As New List(Of Integer)
        Dim rnd As New Random
        For i = 0 To nNumbers - 1
            ns.Insert(rnd.Next(0, i + 1), i)
        Next
        Return ns
    End Function

    Sub RandomiseFile(srcFile As String)
        Dim lines = File.ReadAllLines(srcFile)
        Dim nLines = lines.Count
        Dim randomNumbers = RandomList(nLines)
        ' use a temporary file in case something goes wrong so that
        ' the original file is still there.
        Dim tmpFile = Path.GetTempFileName()
        ' output the lines in a random order.
        Using sw = New StreamWriter(tmpFile)
            For i = 0 To nLines - 1
                sw.WriteLine(lines(randomNumbers(i)))
            Next
        End Using
        File.Delete(srcFile)
        File.Move(tmpFile, srcFile)
    End Sub

    Sub Main()
        Dim fileToUse As String = "C:\temp\makerandom.txt"
        CreateFile(fileToUse)
        RandomiseFile(fileToUse)
    End Sub

End Module


你需要一个代码样本还是一个伪代码?我是初学者,所以我需要一个完整的样本,但如果你能给我一个伪代码,我可以自己试试。。。谢谢你的评论你需要一个代码样本还是一个伪代码?我是Beginer,所以我需要一个完整的样本,但如果你能给我一个伪代码,我可以自己试试。。。感谢您的评论还有一个文件.ReadAllLines()。但是如果您使用ReadAllLines,则不必将其拆分为数组,它会为您返回数组。很抱歉,调试器会说:“未将参数'Number'指定为参数'Number'(公共函数Str(Number As Object)As String”。“在:”Dim b(Str.Length)As Integer“,如果我更改“str.lenght”加上“8”,代码会删除文本文件的所有内容,为什么我可以这样做?还有一个文件.ReadAllLines()。但是如果使用ReadAllLines,则不必将其拆分为数组,它会为您返回数组。很抱歉,调试器会说:“参数未为公共函数str(Number As Object)的参数'Number'指定”“Dim b(Str.Length)为整数”,如果我将“Str.lenght”改为“8”,代码将删除文本文件的所有内容,为什么我可以这样做?谢谢,看起来很简单,但它说:“属性访问必须分配给属性或使用其值。”在“randomize()“,我如何解决它?@ElektroHacker:显示我没有编译错误。您的环境版本是什么?感谢您的回复,我使用的是VS2012,它是winform,windows7@ElektroHacker:你能发布你的项目吗。谢谢,看起来很简单,但上面说:“属性访问必须分配给属性或使用其值。”在“randomize()”行,我如何解决它?@ElektroHacker:没有显示编译错误。您的环境版本是什么?感谢您的回复,我使用的是VS2012,它是winform,windows7@ElektroHacker:你能发布你的项目吗。谢谢,我已经创建了这个模块,但是你现在能告诉我调用这个模块的命令吗?(对不起,我很抱歉)@ElektroHacker这是一个完整的程序,如果您创建一个新的控制台应用程序项目,它将正常工作。您只需要RandomiseFile和RandomList方法,它们不需要在模块中。您只需使用
RandomiseFile(“yourfilenamegoesher”)
。这清楚吗?我很惊讶,然后你给了我一个2in1工具,我也想编译控制台工具,再次感谢你,我已经创建了模块,但是你现在能告诉我调用该模块的命令吗?(对不起,我很抱歉)@ElektroHacker这是一个完整的程序,如果您创建一个新的控制台应用程序项目,它将正常工作。您只需要RandomiseFile和RandomList方法,它们不需要在模块中。您只需使用
RandomiseFile(“yourfilenamegoesher”)
。这清楚了吗?我印象深刻,然后你给了我一个2in1工具,我也想编译控制台工具,再次感谢