【发布时间】:2018-07-03 14:49:07
【问题描述】:
大家好,有人可以向我解释为什么这不起作用。
我基本上必须拥有名为 Books 和 NewBooks 的文本文件...
文本文件从网络请求中填充,然后信息被解析到文本文件中......当我启动程序时,书籍和新书籍是相同的,并且几乎是彼此的副本。
完成了更多网络请求以更新 NewBooks 文本文件,当我比较它们时,如果 NewBooks 中有一行不在 Books 中,它会将该行添加到名为 myNewBooks 的第三个文本文件中。现在我将在这里展示的初始代码可以按预期工作
Dim InitialBooks = File.ReadAllLines("Books.json")
Dim TW As System.IO.TextWriter
'Create a Text file and load it into the TextWriter
TW = System.IO.File.CreateText("myNewBooks.JSON")
Dim NewBooks = String.Empty
Using reader = New StreamReader("NewBooks.json")
Do Until reader.EndOfStream
Dim current = reader.ReadLine
If Not InitialBooks.Contains(current) Then
NewBooks = current & Environment.NewLine
TW.WriteLine(NewBooks)
TW.Flush()
'Close the File
End If
Loop
End Using
TW.Close() : TW.Dispose()
但是因为我的文本文件行中的部分字符串包含一个 url,有时我会发现同一本书具有不同的 url...我得到了重复的书籍条目,因为 url 是唯一的区别。所以我想我会在 url 之前拆分字符串,以便我只比较标题和描述以及区域...仅供参考,我的文本文件中的一行看起来类似于:
{ "Title": "My Title Here", "Description": "My Description Here", "Region": "My Region Here", "Url": "My Url Here", "Image": "My图片在这里”};
所以今天有个小伙伴帮我弄清楚了如何拆分我的线路,让它看起来更像这样:
{ "Title": "My Title Here", "Description": "My Description Here", "Region": "My Region Here", "Url"
这很好,但是现在当我比较它时,没有看到第一行包含分割线,我不明白为什么......这是修改后的代码。
Dim InitialBooks = File.ReadAllLines("Books.json")
Dim TW As System.IO.TextWriter
'Create a Text file and load it into the TextWriter
TW = System.IO.File.CreateText("myNewBooks.JSON")
Dim NewBooks = String.Empty
Using reader = New StreamReader("NewBooks.json")
Do Until reader.EndOfStream
Dim current = reader.ReadLine
Dim splitAt As String = """Url"""
Dim index As Integer = current.IndexOf(splitAt)
Dim output As String = current.Substring(0, index + splitAt.Length)
If Not InitialBooks.Contains(output) Then
NewBooks = current & Environment.NewLine
TW.WriteLine(NewBooks)
TW.Flush()
'Close the File
End If
Loop
End Using
TW.Close() : TW.Dispose()
您的智慧将不胜感激!
【问题讨论】:
标签: vb.net file-handling