mirror of
https://onedev.site.tesses.net/crosslang/crosslang-website
synced 2026-02-08 17:15:46 +00:00
First commit
This commit is contained in:
44
content/language-features/working-with-files.md
Normal file
44
content/language-features/working-with-files.md
Normal file
@@ -0,0 +1,44 @@
|
||||
+++
|
||||
title = 'Working with files'
|
||||
date = 2025-05-08T22:23:44-05:00
|
||||
+++
|
||||
|
||||
## Write text to file
|
||||
```go
|
||||
FS.WriteAllText(FS.Local, "file.txt", "Some text");
|
||||
```
|
||||
|
||||
## Read text from file
|
||||
```go
|
||||
var text = FS.ReadAllText(FS.Local, "file.txt");
|
||||
```
|
||||
|
||||
## Create a directory
|
||||
```go
|
||||
FS.Local.CreateDirectory("New Folder");
|
||||
FS.Local.CreateDirectory("Folder" / "Another Folder"); // we can create folders even if parents don't exist
|
||||
```
|
||||
|
||||
## Enumerating entries in folder
|
||||
```go
|
||||
each(var file : FS.Local.EnumeratePaths(/))
|
||||
{
|
||||
if(FS.Local.DirectoryExists(file)) Console.Write("DIR ");
|
||||
|
||||
Console.WriteLine(file);
|
||||
}
|
||||
```
|
||||
|
||||
### Exists
|
||||
```go
|
||||
FS.Local.FileExists("/path/to/file"); //use FS.Local.RegularFileExists("/path/to/file"); if you don't want special files to be considered
|
||||
FS.Local.DirectoryExists("/path/to/directory");
|
||||
```
|
||||
|
||||
### Opening files
|
||||
See [Streams](streams.md) for more details on the Stream object
|
||||
```go
|
||||
var strm = FS.Local.OpenFile("/path/to/file.bin", "rb");
|
||||
//use strm.Read(buffer,buffer_offset,length); to read some data, returns number of bytes read
|
||||
strm.Close();
|
||||
```
|
||||
Reference in New Issue
Block a user