Advertisement
datatheoz

Randomize File Lines

Jul 6th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.14 KB | None | 0 0
  1. Imports System.IO
  2.  
  3. Public Class FileUtility
  4.     Private Shared ReadOnly Rnd As New Random(Now.Millisecond)
  5.  
  6.     Public Shared Sub RandomizeLines(targetFile As String, Optional randomizeRate As Integer = 10)
  7.         If randomizeRate < 2 Then randomizeRate = 10
  8.  
  9.         Dim currPath As String = Path.GetDirectoryName(targetFile)
  10.         Dim currFile As String = Path.Combine(currPath, Path.GetFileNameWithoutExtension(targetFile) & " (Randomized)" & Path.GetExtension(targetFile))
  11.         Dim currWriter As String
  12.  
  13.         Dim writers() As StreamWriter = {}
  14.         Array.Resize(writers, randomizeRate)
  15.         For i As Integer = 0 To randomizeRate - 1
  16.             writers(i) = New StreamWriter(Path.Combine(currPath, "temp" & i.ToString & ".txt")) 'With {.AutoFlush = True}
  17.         Next
  18.  
  19.         Using fs As FileStream = File.OpenRead(targetFile)
  20.             Using sr As New StreamReader(fs)
  21.                 While Not sr.EndOfStream
  22.                     writers(Rnd.Next(randomizeRate)).WriteLine(sr.ReadLine())
  23.                 End While
  24.             End Using
  25.         End Using
  26.  
  27.         Using sw As New StreamWriter(currFile) 'With {.AutoFlush = True}
  28.             For i As Integer = 0 To randomizeRate - 1
  29.                 writers(i).Dispose()
  30.                 currWriter = Path.Combine(currPath, "temp" & i.ToString & ".txt")
  31.                 Using reader As New StreamReader(currWriter)
  32.                     While Not reader.EndOfStream
  33.                         sw.WriteLine(reader.ReadLine)
  34.                     End While
  35.                 End Using
  36.                 File.Delete(currWriter)
  37.             Next
  38.         End Using
  39.     End Sub
  40.  
  41.     Public Shared Sub RandomizeLinesHaze(targetFile As String, Optional randomizeRate As Integer = 10)
  42.         If randomizeRate < 2 Then randomizeRate = 10
  43.  
  44.         Dim currPath As String = Path.GetDirectoryName(targetFile)
  45.         Dim currFile As String = Path.Combine(currPath, Path.GetFileNameWithoutExtension(targetFile) & " (Randomized)" & Path.GetExtension(targetFile))
  46.         Dim currWriter As String
  47.  
  48.         Dim lstWriters As New List(Of StreamWriter)
  49.         For i As Integer = 0 To randomizeRate - 1
  50.             lstWriters.Add(New StreamWriter(Path.Combine(currPath, "temp" & i.ToString & ".txt")) 'With {.AutoFlush = True})
  51.         Next
  52.  
  53.         Using fs As FileStream = File.OpenRead(targetFile)
  54.             Using sr As New StreamReader(fs)
  55.                 While Not sr.EndOfStream
  56.                     lstWriters(Rnd.Next(randomizeRate)).WriteLine(sr.ReadLine())
  57.                 End While
  58.             End Using
  59.         End Using
  60.  
  61.         Using sw As New StreamWriter(currFile) 'With {.AutoFlush = True}
  62.             For i As Integer = 0 To randomizeRate - 1
  63.                 lstWriters(i).Dispose()
  64.                 currWriter = Path.Combine(currPath, "temp" & i.ToString & ".txt")
  65.                 Using reader As New StreamReader(currWriter)
  66.                     While Not reader.EndOfStream
  67.                         sw.WriteLine(reader.ReadLine)
  68.                     End While
  69.                 End Using
  70.                 File.Delete(currWriter)
  71.             Next
  72.         End Using
  73.     End Sub
  74. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement