我想用函数写多个文件 [英] I want to write multiple file with the function
本文介绍了我想用函数写多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件..
我想从z拆分该文件。
i有编写代码,但我想优化代码..
i我多次写同一个代码..
之前的文件有6个分割但现在文件是60个分割..
请帮忙我...
我尝试过:
I have a file ..
Which i want to split that file from "z".
i have written code but i want to optimise the code ..
i am writing same code multiple time ..
the file earlier was of 6 splits but now the file is of 60 splits ..
please help me .....
What I have tried:
Dim s As String = IO.File.ReadAllText("C:\Users\mks\Desktop\Newfolder\P6.A3")
Dim wFile, wFile1 As System.IO.FileStream
Dim byteData(), byteData1(), byteDatayp(), byteDatayn(), byteDatabp(), byteDatabn(), byteDatarp1(), byteDatarn1(), byteDatayp1(), byteDatayn1(), byteDatabp1(), byteDatabn1() As Byte
Dim rn, rp, yp, yn, bp, bn As String
Dim r As String = Regex.Replace(s.ToString, "[^1-9Z-]", "")
' Console.WriteLine(r)
r = Regex.Replace(r.ToString, "[-]", "0")
Dim elements() As String = Regex.Split(r, "Z")
For Each element In elements
Next
rp = elements(0)
rn = elements(1)
yp = elements(2)
yn = elements(3)
bp = elements(4)
bn = elements(5)
byteData = Encoding.ASCII.GetBytes(rp)
byteData1 = Encoding.ASCII.GetBytes(rn)
byteDatayp = Encoding.ASCII.GetBytes(yp)
byteDatayn = Encoding.ASCII.GetBytes(yn)
byteDatabp = Encoding.ASCII.GetBytes(yp)
byteDatabn = Encoding.ASCII.GetBytes(yn)
System.IO.File.WriteAllText("C:\Users\mks\Desktop\Newfolder\ro.mod", "")
wFile = New FileStream("C:\Users\mks\Desktop\Newfolder\ro.mod", FileMode.Append)
wFile.Write(byteData, 0, byteData.Length)
wFile.Close()
System.IO.File.WriteAllText("C:\Users\mks\Desktop\Newfolder\rs.mod", "")
wFile1 = New FileStream("C:\Users\mks\Desktop\Newfolder\rs.mod", FileMode.Append)
wFile1.Write(byteData1, 0, byteData1.Length)
wFile1.Close()
System.IO.File.WriteAllText("C:\Users\mks\Desktop\Newfolder\yo.mod", "")
wFile = New FileStream("C:\Users\mks\Desktop\Newfolder\yo.mod", FileMode.Append)
wFile.Write(byteDatayp, 0, byteDatayp.Length)
wFile.Close()
System.IO.File.WriteAllText("C:\Users\mks\Desktop\Newfolder\ys.mod", "")
wFile1 = New FileStream("C:\Users\mks\Desktop\Newfolder\ys.mod", FileMode.Append)
wFile1.Write(byteDatayn, 0, byteDatayn.Length)
wFile1.Close()
System.IO.File.WriteAllText("C:\Users\mks\Desktop\Newfolder\bo.mod", "")
wFile = New FileStream("C:\Users\mks\Desktop\Newfolder\bo.mod", FileMode.Append)
wFile.Write(byteDatabp, 0, byteDatabp.Length)
wFile.Close()
System.IO.File.WriteAllText("C:\Users\mks\Desktop\Newfolder\bs.mod", "")
wFile1 = New FileStream("C:\Users\mks\Desktop\Newfolder\bs.mod", FileMode.Append)
wFile1.Write(byteDatabn, 0, byteDatabn.Length)
wFile1.Close()
File.Delete("C:\Users\mks\Desktop\Newfolder\ro1.mod")
File.Delete("C:\Users\mks\Desktop\Newfolder\yo1.mod")
File.Delete("C:\Users\mks\Desktop\Newfolder\bo1.mod")
File.Delete("C:\Users\mks\Desktop\Newfolder\rs1.mod")
File.Delete("C:\Users\mks\Desktop\Newfolder\ys1.mod")
File.Delete("C:\Users\mks\Desktop\Newfolder\bs1.mod")
For i As Integer = 0 To s.Length - 1 Step 1
If i Mod 1 = 0 Then
End If
Try
Dim rp1 As String = i / 10 & " " & rp.Substring(i, 1) & Environment.NewLine
Dim rn1 As String = i / 10 & " -" & rn.Substring(i, 1) & Environment.NewLine
Dim yp1 As String = i / 10 & " " & yp.Substring(i, 1) & Environment.NewLine
Dim yn1 As String = i / 10 & " -" & yn.Substring(i, 1) & Environment.NewLine
Dim bp1 As String = i / 10 & " " & bp.Substring(i, 1) & Environment.NewLine
Dim bn1 As String = i / 10 & " -" & bn.Substring(i, 1) & Environment.NewLine
byteDatarp1 = Encoding.ASCII.GetBytes(rp1)
byteDatarn1 = Encoding.ASCII.GetBytes(rn1)
byteDatayp1 = Encoding.ASCII.GetBytes(yp1)
byteDatayn1 = Encoding.ASCII.GetBytes(yn1)
byteDatabp1 = Encoding.ASCII.GetBytes(bp1)
byteDatabn1 = Encoding.ASCII.GetBytes(bn1)
wFile = New FileStream("C:\Users\mks\Desktop\Newfolder\ro1.mod", FileMode.Append)
wFile.Write(byteDatarp1, 0, byteDatarp1.Length)
wFile.Close()
wFile1 = New FileStream("C:\Users\mks\Desktop\Newfolder\rs1.mod", FileMode.Append)
wFile1.Write(byteDatarn1, 0, byteDatarn1.Length)
wFile1.Close()
wFile = New FileStream("C:\Users\mks\Desktop\Newfolder\yo1.mod", FileMode.Append)
wFile.Write(byteDatayp1, 0, byteDatayp1.Length)
wFile.Close()
wFile1 = New FileStream("C:\Users\mks\Desktop\Newfolder\ys1.mod", FileMode.Append)
wFile1.Write(byteDatayn1, 0, byteDatayn1.Length)
wFile1.Close()
wFile = New FileStream("C:\Users\mks\Desktop\Newfolder\bo1.mod", FileMode.Append)
wFile.Write(byteDatabp1, 0, byteDatabp1.Length)
wFile.Close()
wFile1 = New FileStream("C:\Users\mks\Desktop\Newfolder\bs1.mod", FileMode.Append)
wFile1.Write(byteDatabn1, 0, byteDatabn1.Length)
wFile1.Close()
Catch ex As ArgumentOutOfRangeException
Exit For
Finally
End Try
Next
MsgBox("FIlE HAS BEEN PROCEESED")
推荐答案
如果您需要多次执行相同的任务,那么您可以使用唯一参数将该代码放入其自己的方法(Sub或Function)中成为 传入。
If you have something that needs to perform the same task multiple times, then you put that code into its own method (Sub or Function) with the unique parameters to be passed in.
Sub WriteBytesToFile(filename as string, data as string)
' code goes here
End Sub
现在,您可以根据需要调用方法,传递参数以执行任务。
Now you call the method as required passing the parameters to perform the task.
Dim Path as string = "C:\Users\mks\Desktop\Newfolder\"
Dim Filenames As String() = New String() _
{"ro1.mod", "yo1.mod", "bo1.mod", "rs1.mod", "ys1.mod", "bs1.mod"}
If Elements.Length <> Filenames.Length Then
' ERROR!
End If
For i = 0 To Elements.Length - 1
WriteBytesToFile(System.IO.Path.Combine(Path, Filenames(i), Elements(i))
Next
Dim s As String = IO.File.ReadAllText(C:\ Users \mks \Desktop\Newfolder \ p17839.sp1)
Dim counter As Integer = 0
Dim wFile As System.IO.FileStream
Dim byteData(), byteDatarp1()As Byte
Dim rp As String
Dim r As String = Regex.Replace(s.ToString,[^ 1-9Z - ], )
'Console.WriteLine(r)
r = Regex.Replace(r.ToString,[ - ],0)
Dim elements()As String = Regex.Split(r,Z)
元素中的每个元素
Console.WriteLine(元素(计数器))
rp =元素(计数器)
byteData =编码.ASCII.GetBytes(rp)
Dim abc As String =C:\ Users\mks\Desktop\Newfolder\test\r+ counter.ToString +。mod
Dim abc1 As String =C:\ Users\mks\Desktop\Newfolder\test\\\+ counter.ToString +。mod
System.IO.File.WriteAllText(abc,)
wFile =新FileStream(abc,FileMode.Append)
wFile.Write(byteData,0 ,byteData.Length)
wFile.Close()
File.Delete(abc1)
counter + = 1
For i As Integer = 0 to s.Length - 1 Step 1
如果我Mod 1 = 0那么
结束如果
试试
如果计数器Mod 2> 0然后
Dim rp1 As String = i / 10& & rp.Substring(i,1)& Environment.NewLine
byteDatarp1 = Encoding.ASCII.GetBytes(rp1)
wFile =新FileStream(abc1,FileMode.Append)
wFile。写(byteDatarp1,0,byteDatarp1.Length)
wFile.Close()
否则
Dim rp1 As String = i / 10& - & rp.Substring(i,1)& Environment.NewLine
byteDatarp1 = Encoding.ASCII.GetBytes(rp1)
wFile =新FileStream(abc1,FileMode.Append)
wFile。写(byteDatarp1,0,byteDatarp1.Length)
wFile.Close()
结束如果
Catch ex As ArgumentOutOfRangeException
退出
最后
结束尝试
下一页
下一页
MsgBox( 我已经过了)
先生,我这样做了多少最优
Dim s As String = IO.File.ReadAllText("C:\Users\mks\Desktop\Newfolder\p17839.sp1")
Dim counter As Integer = 0
Dim wFile As System.IO.FileStream
Dim byteData(), byteDatarp1() As Byte
Dim rp As String
Dim r As String = Regex.Replace(s.ToString, "[^1-9Z-]", "")
' Console.WriteLine(r)
r = Regex.Replace(r.ToString, "[-]", "0")
Dim elements() As String = Regex.Split(r, "Z")
For Each element In elements
Console.WriteLine(elements(counter))
rp = elements(counter)
byteData = Encoding.ASCII.GetBytes(rp)
Dim abc As String = "C:\Users\mks\Desktop\Newfolder\test\r" + counter.ToString + ".mod"
Dim abc1 As String = "C:\Users\mks\Desktop\Newfolder\test\ra" + counter.ToString + ".mod"
System.IO.File.WriteAllText(abc, "")
wFile = New FileStream(abc, FileMode.Append)
wFile.Write(byteData, 0, byteData.Length)
wFile.Close()
File.Delete(abc1)
counter += 1
For i As Integer = 0 To s.Length - 1 Step 1
If i Mod 1 = 0 Then
End If
Try
If counter Mod 2 > 0 Then
Dim rp1 As String = i / 10 & " " & rp.Substring(i, 1) & Environment.NewLine
byteDatarp1 = Encoding.ASCII.GetBytes(rp1)
wFile = New FileStream(abc1, FileMode.Append)
wFile.Write(byteDatarp1, 0, byteDatarp1.Length)
wFile.Close()
Else
Dim rp1 As String = i / 10 & " -" & rp.Substring(i, 1) & Environment.NewLine
byteDatarp1 = Encoding.ASCII.GetBytes(rp1)
wFile = New FileStream(abc1, FileMode.Append)
wFile.Write(byteDatarp1, 0, byteDatarp1.Length)
wFile.Close()
End If
Catch ex As ArgumentOutOfRangeException
Exit For
Finally
End Try
Next
Next
MsgBox("FIlE HAS BEEN PROCEESED")
sir i have done like this how much optimal it is
这篇关于我想用函数写多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文