Four ways to write file operations
Package Main
Import (
"Bufio"//Cache IO
"FMT"
"Io/ioutil"//io Toolkit
"IO"
"OS"
)
Func Check (e error) {
If e!= Nil {
Panic (e)
}
}
/**
* To determine if a file exists returns true does not exist returns false
*/
Func checkfileisexist (filename string) (bool) {
var exist = true;
If _, Err: = OS. Stat (filename); Os. Isnotexist (Err) {
exist = false;
}
return exist;
}
Func Main () {
var wiretestring = "Test n"
var filename = "./output1.txt";
var f *os. File
var err1 error;
/*** the first way: using IO. WriteString Write to File **/
If checkfileisexist (filename) {//If file exists
F, err1 = OS. OpenFile (filename, os. O_append, 0666)//Open File
Fmt. Println ("file exists");
}else {
F, err1 = OS. Create (filename)//File creation
Fmt. PRINTLN ("File does not exist");
}
Check (ERR1)
N, err1: = Io. WriteString (f, wiretestring)//write file (string)
Check (ERR1)
Fmt. Printf ("write%d bytes n", n);
/** the second way: using Ioutil. WriteFile Write to File **/
var D1 = []byte (wiretestring);
ERR2: = Ioutil. WriteFile ("./output2.txt", D1, 0666)//write file (byte array)
Check (ERR2)
/** A third way: Write files using file (write,writestring) **/
F, ERR3: = OS. Create ("./output3.txt")//Creating files
Check (ERR3)
Defer F.close ()
N2, Err3: = F.write (D1)//write file (byte array)
Check (ERR3)
Fmt. Printf ("write%d bytes n", N2)
N3, Err3: = f.writestring ("WRITESN")//write file (byte array)
Fmt. Printf ("write%d bytes n", N3)
F.sync ()
/** Fourth Way: Using Bufio. Newwriter Write to File **/
W: = Bufio. Newwriter (f)//Create a new Writer object
N4, Err3: = w.writestring ("Bufferedn")
Fmt. Printf ("write%d bytes n", N4)
W.flush ()
F.close ()
}
1. Read the document
Package Main
Import (
"Bufio"
"FMT"
"IO"
"Io/ioutil"
"OS"
)
Reading a file requires frequent error checking, which can streamline the following error checking procedures.
Func Check (e error) {
If e!= Nil {
Panic (e)
}
}
Func Main () {
Perhaps most basic file-reading tasks are to read the contents of a file into memory.
DAT, err: = Ioutil. ReadFile ("/tmp/dat")
Check (ERR)
Fmt. Print (string (DAT))
You often think of more control over how a file is read and which part is read. For this task, from using the OS. Open opens a file to get an OS. The File value begins.
F, err: = OS. Open ("/tmp/dat")
Check (ERR)
Reads some bytes from the beginning of the file. This reads up to 5 bytes, and this is the number of bytes that we actually read.
B1: = Make ([]byte, 5)
N1, Err: = F.read (B1)
Check (ERR)
Fmt. Printf ("%d bytes:%s\n", N1, String (B1))
You can also seek a known location in a file and start reading from that location.
O2, Err: = F.seek (6, 0)
Check (ERR)
B2: = Make ([]byte, 2)
N2, Err: = F.read (B2)
Check (ERR)
Fmt. Printf ("%d bytes @%d:%s\n", N2, O2, String (B2))
The IO package provides a number of functions that can help us read files. For example, the above reading can use Readatleast to get a more robust implementation.
O3, Err: = F.seek (6, 0)
Check (ERR)
B3: = Make ([]byte, 2)
N3, err: = Io. Readatleast (F, B3, 2)
Check (ERR)
Fmt. Printf ("%d bytes @%d:%s\n", N3, O3, String (B3))
There is no built-in slewing support, but it is implemented using seek (0, 0).
_, Err = F.seek (0, 0)
Check (ERR)
The Bufio package implements a buffered read, which not only improves performance for many small read operations, but also provides many additional read functions.
R4: = Bufio. Newreader (f)
B4, err: = R4. Peek (5)
Check (ERR)
Fmt. Printf ("5 bytes:%s\n", String (B4))
Close the file after the task is finished (usually this should be done immediately after the open operation with defer).
F.close ()
}
/*
$ echo "Hello" >/tmp/dat
$ echo "Go" >>/tmp/dat
$ go Run reading-files.go
Hello
Go
5 Bytes:hello
2 bytes @ 6:go
2 bytes @ 6:go
5 Bytes:hello
*/
2. Write a document
Package Main
Import (
"Bufio"
"FMT"
"Io/ioutil"
"OS"
)
Func Check (e error) {
If e!= Nil {
Panic (e)
}
}
Func Main () {
To begin, here is a presentation such as writing a string (or just some bytes) to a file.
D1: = []byte ("hello\ngo\n")
ERR: = Ioutil. WriteFile ("/tmp/dat1", D1, 0644)
Check (ERR)
For finer-grained writes, open a file first.
F, err: = OS. Create ("/tmp/dat2")
Check (ERR)
After you open the file, it is customary to call the close operation of the file immediately using defer.
Defer F.close ()
You can write the byte slice you want to write
D2: = []byte{115, 111, 109, 101, 10}
N2, Err: = F.write (D2)
Check (ERR)
Fmt. Printf ("wrote%d bytes\n", N2)
WriteString is also available.
N3, Err: = F.writestring ("writes\n")
Fmt. Printf ("wrote%d bytes\n", N3)
Call Sync to write information about the buffer to disk.
F.sync ()
Bufio provides a buffered writer like the buffer reader we saw earlier.
W: = Bufio. Newwriter (f)
N4, err: = W.writestring ("buffered\n")
Fmt. Printf ("wrote%d bytes\n", N4)
Use Flush to ensure that all cached operations are written to the underlying writer.
W.flush ()
}
/*
To run this end file to write code.
$ go Run writing-files.go
wrote 5 bytes
wrote 7 bytes
wrote 9 bytes
Then check the contents of the write file.
$ cat/tmp/dat1
Hello
Go
$ cat/tmp/dat2
Some
writes
buffered
*/