Skip to content

Commit c16dbf3

Browse files
authored
Update README.md
1 parent 7ee381e commit c16dbf3

File tree

1 file changed

+252
-10
lines changed

1 file changed

+252
-10
lines changed

README.md

Lines changed: 252 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# File
2+
23
![Official](https://img.shields.io/badge/project-official-green.svg?colorA=303033&colorB=226af6&label=Pelagornis)
34
![SPM](https://img.shields.io/badge/SPM-compatible-brightgreen.svg)
45
![Swift](https://img.shields.io/badge/Swift-5.7-orange.svg)
5-
[![License](https://img.shields.io/github/license/pelagornis/plfile)](https://github.com/pelagornis/plfile/blob/main/LICENSE)
6+
[![License](https://img.shields.io/github/license/pelagornis/swift-file)](https://github.com/pelagornis/swift-file/blob/main/LICENSE)
67
![Platform](https://img.shields.io/badge/platforms-iOS%2013.0%7C%20tvOS%2013.0%7C%20macOS%2010.15%7C%20watchOS%206.0-red.svg)
78

8-
📁 Pelagornis File Management Library
9+
📁 **File** is a powerful and intuitive file management library for Swift. It simplifies file and folder operations, providing a consistent API across different platforms. File is designed to make working with the file system a breeze, whether you're reading, writing, creating, or deleting files and folders.
910

1011
## Installation
1112
File was deployed as Swift Package Manager. Package to install in a project. Add as a dependent item within the swift manifest.
@@ -46,20 +47,261 @@ Path.home
4647
Path.documents
4748
```
4849

49-
Create, Write file and Folder!
50+
#### Writing String Data
51+
52+
This example demonstrates how to create a file and write a string to it.
53+
5054
```swift
51-
let path = Path.home
52-
let folder = try? Folder(path: path)
53-
let file = try? folder.createFile(at: "test.swift")
54-
try? file.write("print(1)")
55+
import File
56+
57+
let path = Path.temporary
58+
let folder = try Folder(path: path)
59+
let file = try folder.createFile(at: "example.txt")
60+
try file.write("Hello, File!")
5561
```
5662

57-
And you can delete files and folders if you want.
63+
#### Writing Binary Data
64+
65+
This example shows how to write binary data to a file.
5866

5967
```swift
60-
try? file.delete()
61-
try? folder.delete()
68+
import Foundation
69+
import File
70+
71+
let path = Path.temporary
72+
let folder = try Folder(path: path)
73+
let file = try folder.createFile(at: "binary.data")
74+
let data = Data([0x00, 0x01, 0x02, 0x03])
75+
try file.write(data)
76+
```
77+
78+
#### Appending String Data
79+
80+
This example demonstrates how to append a string to an existing file.
81+
82+
```swift
83+
import File
84+
85+
let path = Path.temporary
86+
let folder = try Folder(path: path)
87+
let file = try folder.createFile(at: "example.txt")
88+
try file.append(" This is appended content.")
89+
```
90+
91+
#### Appending Binary Data
92+
93+
This example shows how to append binary data to an existing file.
94+
95+
```swift
96+
import Foundation
97+
import File
98+
99+
let path = Path.temporary
100+
let folder = try Folder(path: path)
101+
let file = try folder.createFile(at: "binary.data")
102+
let data = Data([0x04, 0x05, 0x06, 0x07])
103+
try file.append(data)
104+
```
105+
106+
#### Reading Data
107+
108+
This example demonstrates how to read data from a file.
109+
110+
```swift
111+
import File
112+
113+
let path = Path.temporary
114+
let folder = try Folder(path: path)
115+
let file = try folder.createFile(at: "example.txt")
116+
try file.write("Hello, File!")
117+
let readData = try file.read()
118+
if let readString = String(data: readData, encoding: .utf8) {
119+
print(readString) // Output: Hello, File!
120+
}
62121
```
63122

123+
#### Opening a File (AppKit)
124+
125+
If `AppKit` is available, this example shows how to open a file using `AppKit`.
126+
127+
```swift
128+
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
129+
import File
130+
131+
let path = Path.temporary
132+
let folder = try Folder(path: path)
133+
let file = try folder.createFile(at: "example.txt")
134+
file.open()
135+
#endif
136+
```
137+
138+
#### Handling FileError
139+
140+
This example shows how to catch and handle `FileError`.
141+
142+
```swift
143+
import File
144+
145+
let path = Path.root
146+
let filePath = Path("/path/that/do/not/exist/example.txt")
147+
148+
do {
149+
let folder = try Folder(path: path)
150+
let file = try folder.createFile(at: filePath)
151+
try file.write("this will fail")
152+
} catch let error as FileError {
153+
print("FileError: \(error.message)")
154+
if let underlyingError = error.error {
155+
print("Underlying error: \(underlyingError)")
156+
}
157+
} catch {
158+
print("Unexpected error: \(error)")
159+
}
160+
```
161+
162+
### Folder Examples
163+
164+
#### Creating Subfolders and Files
165+
166+
This example demonstrates how to create subfolders and files within a folder.
167+
168+
```swift
169+
import File
170+
171+
let path = Path.temporary
172+
let folder = try Folder(path: path)
173+
let subfolder = try folder.createSubfolder(at: "subfolder")
174+
let newFile = try subfolder.createFile(at: "newFile.txt")
175+
try newFile.write("Content of the new file")
176+
```
177+
178+
#### Retrieving Files and Subfolders
179+
180+
This example shows how to retrieve files and subfolders from a folder.
181+
182+
```swift
183+
import File
184+
185+
let path = Path.temporary
186+
let folder = try Folder(path: path)
187+
_ = try folder.createSubfolder(at: "subfolder")
188+
_ = try folder.createFile(at: "example.txt")
189+
let files = folder.files
190+
let subfolders = folder.subfolders
191+
print(files) // Output: Contains "example.txt"
192+
print(subfolders) // Output: Contains "subfolder"
193+
```
194+
195+
#### Moving and Copying Contents
196+
197+
This example shows how to move and copy the contents of a folder to another folder.
198+
199+
```swift
200+
import File
201+
202+
let path = Path.temporary
203+
let folder = try Folder(path: path)
204+
let destinationFolder = try Folder(path: Path(path.rawValue + "destination"))
205+
_ = try folder.createFile(at: "example.txt")
206+
_ = try folder.createSubfolder(at: "subfolder")
207+
try folder.moveContents(to: destinationFolder)
208+
let files = destinationFolder.files
209+
let subfolders = destinationFolder.subfolders
210+
print(files) // Output: Contains "example.txt"
211+
print(subfolders) // Output: Contains "subfolder"
212+
213+
let copyFolder = try Folder(path: Path(path.rawValue + "copy"))
214+
try destinationFolder.copy(to: copyFolder)
215+
216+
```
217+
218+
#### Emptying the Folder
219+
220+
This example shows how to empty a folder (delete all its contents).
221+
222+
```swift
223+
import File
224+
225+
let path = Path.temporary
226+
let folder = try Folder(path: path)
227+
_ = try folder.createFile(at: "example.txt")
228+
_ = try folder.createSubfolder(at: "subfolder")
229+
try folder.empty()
230+
```
231+
232+
#### Deleting Content
233+
234+
This example show how to delete subfolders and file
235+
236+
```swift
237+
import File
238+
239+
let path = Path.temporary
240+
let folder = try Folder(path: path)
241+
let file = try folder.createFile(at: "example.txt")
242+
let subfolder = try folder.createSubfolder(at: "subfolder")
243+
try file.delete()
244+
try subfolder.delete()
245+
```
246+
247+
### FileSystem Examples
248+
249+
#### Renaming
250+
251+
This example demonstrates how to rename a file or a folder.
252+
253+
```swift
254+
import File
255+
256+
let path = Path.temporary
257+
let folder = try Folder(path: path)
258+
let file = try folder.createFile(at: "oldName.txt")
259+
try file.rename(to: "newName")
260+
print(file.name) // Output: newName.txt
261+
```
262+
263+
#### Moving
264+
265+
This example shows how to move a file or a folder to a new location.
266+
267+
```swift
268+
import File
269+
270+
let path = Path.temporary
271+
let folder = try Folder(path: path)
272+
let destinationFolder = try Folder(path: Path(path.rawValue + "destination"))
273+
let file = try folder.createFile(at: "example.txt")
274+
try file.move(to: destinationFolder)
275+
```
276+
277+
#### Copying
278+
279+
This example shows how to copy a file or a folder to a new location.
280+
281+
```swift
282+
import File
283+
284+
let path = Path.temporary
285+
let folder = try Folder(path: path)
286+
let destinationFolder = try Folder(path: Path(path.rawValue + "destination"))
287+
let file = try folder.createFile(at: "example.txt")
288+
try file.copy(to: destinationFolder)
289+
```
290+
291+
#### Deleting
292+
293+
This example shows how to delete a file or a folder.
294+
295+
```swift
296+
import File
297+
298+
let path = Path.temporary
299+
let folder = try Folder(path: path)
300+
let file = try folder.createFile(at: "example.txt")
301+
try file.delete()
302+
```
303+
304+
64305
## License
65306
**swift-file** is under MIT license. See the [LICENSE](LICENSE) file for more info.
307+

0 commit comments

Comments
 (0)