mirror of
https://onedev.site.tesses.net/crosslang/crosslang-website
synced 2026-02-09 09:35:46 +00:00
45 lines
1.0 KiB
Markdown
45 lines
1.0 KiB
Markdown
+++
|
|
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();
|
|
```
|