| 1 | #Region "Microsoft.VisualBasic::dd2a158234e426a88e2e3a25a1b611e3, Microsoft.VisualBasic.Core\Extensions\IO\Extensions\Extensions.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Module Extensions |
| 35 | ' |
| 36 | ' Function: FlushAllLines, OpenWriter |
| 37 | ' |
| 38 | ' |
| 39 | ' /********************************************************************************/ |
| 40 | |
| 41 | #End Region |
| 42 | |
| 43 | Imports System.IO |
| 44 | Imports System.Runtime.CompilerServices |
| 45 | Imports System.Text |
| 46 | Imports Microsoft.VisualBasic.Language |
| 47 | Imports Microsoft.VisualBasic.Text |
| 48 | |
| 49 | Namespace FileIO |
| 50 | |
| 51 | ''' <summary> |
| 52 | ''' |
| 53 | ''' </summary> |
| 54 | ''' <remarks> |
| 55 | ''' <see cref="Encoding"/>会和<see cref="Encodings"/>产生冲突, |
| 56 | ''' 使用这个单独的拓展模块,但是位于不同的命名空间来解决这个问题。 |
| 57 | ''' </remarks> |
| 58 | Public Module Extensions |
| 59 | |
| 60 | ''' <summary> |
| 61 | ''' Write all object into a text file by using its <see cref="Object.ToString"/> method. |
| 62 | ''' </summary> |
| 63 | ''' <typeparam name="T"></typeparam> |
| 64 | ''' <param name="data"></param> |
| 65 | ''' <param name="saveTo"></param> |
| 66 | ''' <param name="encoding"></param> |
| 67 | ''' <returns></returns> |
| 68 | <Extension> Public Function FlushAllLines(Of T)(data As IEnumerable(Of T), saveTo$, Optional encoding As Encoding = Nothing) As Boolean |
| 69 | Dim strings As IEnumerable(Of String) = |
| 70 | data.Select(AddressOf Scripting.ToString) |
| 71 | Dim parent$ = FileSystem.GetParentPath(saveTo) |
| 72 | |
| 73 | Call parent.MkDIR |
| 74 | |
| 75 | If encoding Is Nothing Then |
| 76 | encoding = Encoding.Default |
| 77 | End If |
| 78 | |
| 79 | Try |
| 80 | Using writer As StreamWriter = saveTo.OpenWriter(encoding,) |
| 81 | For Each line As String In strings |
| 82 | Call writer.WriteLine(line) |
| 83 | Next |
| 84 | End Using |
| 85 | Catch ex As Exception |
| 86 | Call App.LogException(New Exception(saveTo, ex)) |
| 87 | Return False |
| 88 | End Try |
| 89 | |
| 90 | Return True |
| 91 | End Function |
| 92 | |
| 93 | ''' <summary> |
| 94 | ''' Open text file writer, this function will auto handle all things. |
| 95 | ''' </summary> |
| 96 | ''' <param name="path">假若路径是指向一个已经存在的文件,则原有的文件数据将会被清空</param> |
| 97 | ''' <param name="encoding"></param> |
| 98 | ''' <returns></returns> |
| 99 | <Extension> |
| 100 | Public Function OpenWriter(path$, |
| 101 | Optional encoding As Encoding = Nothing, |
| 102 | Optional newLine As String = vbLf, |
| 103 | Optional append As Boolean = False) As StreamWriter |
| 104 | Dim file As FileStream |
| 105 | Dim writeNew = Function() |
| 106 | ' 使用最基础的ASCII编码,可能会解决一些莫名其妙的文件头出现的bug |
| 107 | Call "".SaveTo(path, Encoding.ASCII) |
| 108 | Return New FileStream(path, FileMode.OpenOrCreate) |
| 109 | End Function |
| 110 | |
| 111 | If append Then |
| 112 | With path.ParentPath |
| 113 | If Not .DirectoryExists Then |
| 114 | Call .MkDIR |
| 115 | End If |
| 116 | |
| 117 | If path.FileExists Then |
| 118 | file = New FileStream(path, FileMode.Append) |
| 119 | Else |
| 120 | file = writeNew() |
| 121 | End If |
| 122 | End With |
| 123 | Else |
| 124 | file = writeNew() |
| 125 | End If |
| 126 | |
| 127 | Dim writer As New StreamWriter(file, encoding Or UTF8, bufferSize:=App.BufferSize) With { |
| 128 | .NewLine = newLine Or vbLf.AsDefault |
| 129 | } |
| 130 | |
| 131 | Return writer |
| 132 | End Function |
| 133 | End Module |
| 134 | End Namespace |