all: replace io/ioutil with io and os package

For golang/go#45557

Change-Id: Ibf78d87cc94baa0d1696e5abc2edad3d2723e42b
GitHub-Last-Rev: ad314caaff6e51edb51a63d183c0ff906cf6b2ad
GitHub-Pull-Request: golang/text#34
Reviewed-on: https://go-review.googlesource.com/c/text/+/430815
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Meng Zhuo <mzh@golangcn.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: Meng Zhuo <mzh@golangcn.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
diff --git a/cases/gen.go b/cases/gen.go
index a041fed..2e1d95c 100644
--- a/cases/gen.go
+++ b/cases/gen.go
@@ -16,7 +16,6 @@
 	"bytes"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"log"
 	"reflect"
 	"strconv"
@@ -635,9 +634,9 @@
 
 	// We discard the output as we know we have perfect functions. We run them
 	// just to verify the properties are correct.
-	n := printProperties(ioutil.Discard, "DerivedCoreProperties.txt", "Cased", verifyCased)
-	n += printProperties(ioutil.Discard, "DerivedCoreProperties.txt", "Lowercase", verifyLower)
-	n += printProperties(ioutil.Discard, "DerivedCoreProperties.txt", "Uppercase", verifyUpper)
+	n := printProperties(io.Discard, "DerivedCoreProperties.txt", "Cased", verifyCased)
+	n += printProperties(io.Discard, "DerivedCoreProperties.txt", "Lowercase", verifyLower)
+	n += printProperties(io.Discard, "DerivedCoreProperties.txt", "Uppercase", verifyUpper)
 	if n > 0 {
 		log.Fatalf("One of the discarded properties does not have a perfect filter.")
 	}
diff --git a/cmd/gotext/main.go b/cmd/gotext/main.go
index c8dc799..aad1d4a 100644
--- a/cmd/gotext/main.go
+++ b/cmd/gotext/main.go
@@ -16,7 +16,6 @@
 	"go/build"
 	"go/format"
 	"io"
-	"io/ioutil"
 	"log"
 	"os"
 	"strings"
@@ -310,7 +309,7 @@
 			if err != nil {
 				logf("Could not format generated docs: %v\n", err)
 			}
-			if err := ioutil.WriteFile("doc.go", b, 0666); err != nil {
+			if err := os.WriteFile("doc.go", b, 0666); err != nil {
 				logf("Could not create file alldocs.go: %v\n", err)
 			}
 		} else {
diff --git a/collate/maketables.go b/collate/maketables.go
index 59afeb9..4175143 100644
--- a/collate/maketables.go
+++ b/collate/maketables.go
@@ -17,7 +17,6 @@
 	"flag"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"log"
 	"os"
 	"regexp"
@@ -187,7 +186,7 @@
 
 func openArchive() *zip.Reader {
 	f := gen.OpenCLDRCoreZip()
-	buffer, err := ioutil.ReadAll(f)
+	buffer, err := io.ReadAll(f)
 	f.Close()
 	failOnError(err)
 	archive, err := zip.NewReader(bytes.NewReader(buffer), int64(len(buffer)))
diff --git a/collate/reg_test.go b/collate/reg_test.go
index 4d35984..f9c192c 100644
--- a/collate/reg_test.go
+++ b/collate/reg_test.go
@@ -10,7 +10,6 @@
 	"bytes"
 	"flag"
 	"io"
-	"io/ioutil"
 	"log"
 	"path"
 	"regexp"
@@ -133,7 +132,7 @@
 
 func loadTestData() []Test {
 	f := gen.OpenUnicodeFile("UCA", "", "CollationTest.zip")
-	buffer, err := ioutil.ReadAll(f)
+	buffer, err := io.ReadAll(f)
 	f.Close()
 	Error(err)
 	archive, err := zip.NewReader(bytes.NewReader(buffer), int64(len(buffer)))
diff --git a/encoding/encoding_test.go b/encoding/encoding_test.go
index 1738147..e459fa2 100644
--- a/encoding/encoding_test.go
+++ b/encoding/encoding_test.go
@@ -5,7 +5,7 @@
 package encoding_test
 
 import (
-	"io/ioutil"
+	"io"
 	"strings"
 	"testing"
 
@@ -62,7 +62,7 @@
 			want = "AB\x00CD\ufffdYZ"
 		}
 		sr := strings.NewReader("AB\x00CD\x80YZ")
-		g, err := ioutil.ReadAll(transform.NewReader(sr, enc))
+		g, err := io.ReadAll(transform.NewReader(sr, enc))
 		if err != nil {
 			t.Errorf("%s: ReadAll: %v", direction, err)
 			continue
diff --git a/encoding/internal/enctest/enctest.go b/encoding/internal/enctest/enctest.go
index 0cccae0..cf5da26 100644
--- a/encoding/internal/enctest/enctest.go
+++ b/encoding/internal/enctest/enctest.go
@@ -8,7 +8,7 @@
 	"bytes"
 	"fmt"
 	"io"
-	"io/ioutil"
+	"os"
 	"strings"
 	"testing"
 
@@ -105,7 +105,7 @@
 			b.ResetTimer()
 			for i := 0; i < b.N; i++ {
 				r := transform.NewReader(bytes.NewReader(src), transformer)
-				io.Copy(ioutil.Discard, r)
+				io.Copy(io.Discard, r)
 			}
 		})
 	}
@@ -157,15 +157,15 @@
 		dstFile, srcFile = srcFile, dstFile
 		coder = enc.NewDecoder()
 	}
-	dst, err := ioutil.ReadFile(dstFile)
+	dst, err := os.ReadFile(dstFile)
 	if err != nil {
-		if dst, err = ioutil.ReadFile("../" + dstFile); err != nil {
+		if dst, err = os.ReadFile("../" + dstFile); err != nil {
 			return nil, nil, nil, err
 		}
 	}
-	src, err := ioutil.ReadFile(srcFile)
+	src, err := os.ReadFile(srcFile)
 	if err != nil {
-		if src, err = ioutil.ReadFile("../" + srcFile); err != nil {
+		if src, err = os.ReadFile("../" + srcFile); err != nil {
 			return nil, nil, nil, err
 		}
 	}
diff --git a/encoding/traditionalchinese/all_test.go b/encoding/traditionalchinese/all_test.go
index 3825c76..f804e83 100644
--- a/encoding/traditionalchinese/all_test.go
+++ b/encoding/traditionalchinese/all_test.go
@@ -6,7 +6,7 @@
 
 import (
 	"fmt"
-	"io/ioutil"
+	"io"
 	"strings"
 	"testing"
 
@@ -103,7 +103,7 @@
 		"\x88\xa2\x88\xa3\x88\xa4\x88\xa5\x88\xa6"
 	want := "ÓǑÒ\u00ca\u0304Ế\u00ca\u030cỀÊ " +
 		"ü\u00ea\u0304ế\u00ea\u030cề"
-	dst, err := ioutil.ReadAll(transform.NewReader(
+	dst, err := io.ReadAll(transform.NewReader(
 		strings.NewReader(src), Big5.NewDecoder()))
 	if err != nil {
 		t.Fatal(err)
diff --git a/feature/plural/message.go b/feature/plural/message.go
index 6248d01..56d518c 100644
--- a/feature/plural/message.go
+++ b/feature/plural/message.go
@@ -6,7 +6,7 @@
 
 import (
 	"fmt"
-	"io/ioutil"
+	"io"
 	"reflect"
 	"strconv"
 
@@ -56,7 +56,7 @@
 func Selectf(arg int, format string, cases ...interface{}) catalog.Message {
 	var p parser
 	// Intercept the formatting parameters of format by doing a dummy print.
-	fmt.Fprintf(ioutil.Discard, format, &p)
+	fmt.Fprintf(io.Discard, format, &p)
 	m := &message{arg, kindDefault, 0, cases}
 	switch p.verb {
 	case 'g':
diff --git a/gen.go b/gen.go
index fb2fb94..566a303 100644
--- a/gen.go
+++ b/gen.go
@@ -14,7 +14,6 @@
 	"flag"
 	"fmt"
 	"go/format"
-	"io/ioutil"
 	"os"
 	"os/exec"
 	"path"
@@ -256,7 +255,7 @@
 			filepath.Dir(file) != dirSrc {
 			return nil
 		}
-		b, err := ioutil.ReadFile(file)
+		b, err := os.ReadFile(file)
 		if err != nil || bytes.Contains(b, []byte("\n// +build ignore")) {
 			return err
 		}
@@ -275,7 +274,7 @@
 		}
 		file = filepath.Join(dirDst, base)
 		vprintf("=== COPY %s\n", file)
-		return ioutil.WriteFile(file, b, 0666)
+		return os.WriteFile(file, b, 0666)
 	})
 	if err != nil {
 		fmt.Println("Copying exported files failed:", err)
diff --git a/internal/cldrtree/cldrtree_test.go b/internal/cldrtree/cldrtree_test.go
index 06c366a..c42eba3 100644
--- a/internal/cldrtree/cldrtree_test.go
+++ b/internal/cldrtree/cldrtree_test.go
@@ -7,9 +7,9 @@
 import (
 	"bytes"
 	"flag"
-	"io/ioutil"
 	"log"
 	"math/rand"
+	"os"
 	"path/filepath"
 	"reflect"
 	"regexp"
@@ -303,11 +303,11 @@
 
 			file := filepath.Join("testdata", tc, "output.go")
 			if *genOutput {
-				ioutil.WriteFile(file, got, 0700)
+				os.WriteFile(file, got, 0700)
 				t.SkipNow()
 			}
 
-			b, err := ioutil.ReadFile(file)
+			b, err := os.ReadFile(file)
 			if err != nil {
 				t.Fatalf("failed to open file: %v", err)
 			}
diff --git a/internal/gen/bitfield/bitfield_test.go b/internal/gen/bitfield/bitfield_test.go
index 789f86d..a43dda9 100644
--- a/internal/gen/bitfield/bitfield_test.go
+++ b/internal/gen/bitfield/bitfield_test.go
@@ -7,7 +7,7 @@
 import (
 	"bytes"
 	"fmt"
-	"io/ioutil"
+	"os"
 	"testing"
 )
 
@@ -222,7 +222,7 @@
 `
 
 func mustRead(filename string) string {
-	b, err := ioutil.ReadFile(filename)
+	b, err := os.ReadFile(filename)
 	if err != nil {
 		panic(err)
 	}
diff --git a/internal/gen/gen.go b/internal/gen/gen.go
index fa28953..268258d 100644
--- a/internal/gen/gen.go
+++ b/internal/gen/gen.go
@@ -25,7 +25,6 @@
 	"go/build"
 	"go/format"
 	"io"
-	"io/ioutil"
 	"log"
 	"net/http"
 	"os"
@@ -175,7 +174,7 @@
 		if err := os.MkdirAll(dir, permissions); err != nil {
 			log.Fatalf("Could not create directory: %v", err)
 		}
-		ioutil.WriteFile(readme, []byte(readmeTxt), permissions)
+		os.WriteFile(readme, []byte(readmeTxt), permissions)
 	}
 	return dir
 }
@@ -213,15 +212,15 @@
 	}
 	r := get(urlRoot, path)
 	defer r.Close()
-	b, err := ioutil.ReadAll(r)
+	b, err := io.ReadAll(r)
 	if err != nil {
 		log.Fatalf("Could not download file: %v", err)
 	}
 	os.MkdirAll(filepath.Dir(file), permissions)
-	if err := ioutil.WriteFile(file, b, permissions); err != nil {
+	if err := os.WriteFile(file, b, permissions); err != nil {
 		log.Fatalf("Could not create file: %v", err)
 	}
-	return ioutil.NopCloser(bytes.NewReader(b))
+	return io.NopCloser(bytes.NewReader(b))
 }
 
 func get(root, path string) io.ReadCloser {
@@ -280,13 +279,13 @@
 func updateBuildTags(pattern string) {
 	for _, t := range tags {
 		oldFile := fmt.Sprintf(pattern, t.version)
-		b, err := ioutil.ReadFile(oldFile)
+		b, err := os.ReadFile(oldFile)
 		if err != nil {
 			continue
 		}
 		build := fmt.Sprintf("// +build %s", t.buildTags)
 		b = regexp.MustCompile(`// \+build .*`).ReplaceAll(b, []byte(build))
-		err = ioutil.WriteFile(oldFile, b, 0644)
+		err = os.WriteFile(oldFile, b, 0644)
 		if err != nil {
 			log.Fatal(err)
 		}
@@ -334,7 +333,7 @@
 // Repackage rewrites a Go file from belonging to package main to belonging to
 // the given package.
 func Repackage(inFile, outFile, pkg string) {
-	src, err := ioutil.ReadFile(inFile)
+	src, err := os.ReadFile(inFile)
 	if err != nil {
 		log.Fatalf("reading %s: %v", inFile, err)
 	}
diff --git a/internal/language/gen.go b/internal/language/gen.go
index 95455ec..520f159 100644
--- a/internal/language/gen.go
+++ b/internal/language/gen.go
@@ -15,7 +15,6 @@
 	"flag"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"log"
 	"math"
 	"reflect"
@@ -921,7 +920,7 @@
 	r := gen.OpenIANAFile("domains/root/db")
 	defer r.Close()
 
-	buf, err := ioutil.ReadAll(r)
+	buf, err := io.ReadAll(r)
 	failOnError(err)
 	re := regexp.MustCompile(`"/domains/root/db/([a-z]{2}).html"`)
 	for _, m := range re.FindAllSubmatch(buf, -1) {
diff --git a/internal/testtext/codesize.go b/internal/testtext/codesize.go
index 88df487..cdaed81 100644
--- a/internal/testtext/codesize.go
+++ b/internal/testtext/codesize.go
@@ -7,7 +7,6 @@
 import (
 	"bytes"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"os/exec"
 	"path/filepath"
@@ -26,13 +25,13 @@
 // See dict_test.go in the display package for an example.
 func CodeSize(s string) (int, error) {
 	// Write the file.
-	tmpdir, err := ioutil.TempDir(os.TempDir(), "testtext")
+	tmpdir, err := os.MkdirTemp(os.TempDir(), "testtext")
 	if err != nil {
 		return 0, fmt.Errorf("testtext: failed to create tmpdir: %v", err)
 	}
 	defer os.RemoveAll(tmpdir)
 	filename := filepath.Join(tmpdir, "main.go")
-	if err := ioutil.WriteFile(filename, []byte(s), 0644); err != nil {
+	if err := os.WriteFile(filename, []byte(s), 0644); err != nil {
 		return 0, fmt.Errorf("testtext: failed to write main.go: %v", err)
 	}
 
diff --git a/internal/triegen/example_compact_test.go b/internal/triegen/example_compact_test.go
index 7cf604c..95486d1 100644
--- a/internal/triegen/example_compact_test.go
+++ b/internal/triegen/example_compact_test.go
@@ -7,7 +7,6 @@
 import (
 	"fmt"
 	"io"
-	"io/ioutil"
 
 	"golang.org/x/text/internal/triegen"
 )
@@ -17,12 +16,12 @@
 	for r := rune(0); r < 10000; r += 64 {
 		t.Insert(r, 0x9015BADA55^uint64(r))
 	}
-	sz, _ := t.Gen(ioutil.Discard)
+	sz, _ := t.Gen(io.Discard)
 
 	fmt.Printf("Size normal:    %5d\n", sz)
 
 	var c myCompacter
-	sz, _ = t.Gen(ioutil.Discard, triegen.Compact(&c))
+	sz, _ = t.Gen(io.Discard, triegen.Compact(&c))
 
 	fmt.Printf("Size compacted: %5d\n", sz)
 
diff --git a/internal/triegen/example_test.go b/internal/triegen/example_test.go
index 557a152..5823a2c 100644
--- a/internal/triegen/example_test.go
+++ b/internal/triegen/example_test.go
@@ -6,7 +6,7 @@
 
 import (
 	"fmt"
-	"io/ioutil"
+	"io"
 	"math/rand"
 	"unicode"
 
@@ -15,7 +15,7 @@
 
 const seed = 0x12345
 
-var genWriter = ioutil.Discard
+var genWriter = io.Discard
 
 func randomRunes() map[rune]uint8 {
 	rnd := rand.New(rand.NewSource(seed))
diff --git a/message/pipeline/pipeline_test.go b/message/pipeline/pipeline_test.go
index 2adb11f..f4cd42a 100644
--- a/message/pipeline/pipeline_test.go
+++ b/message/pipeline/pipeline_test.go
@@ -11,7 +11,6 @@
 	"flag"
 	"fmt"
 	"go/build"
-	"io/ioutil"
 	"os"
 	"os/exec"
 	"path"
@@ -40,7 +39,7 @@
 		t.Skipf("skipping because 'go' command is unavailable: %v", err)
 	}
 
-	GOPATH, err := ioutil.TempDir("", "pipeline_test")
+	GOPATH, err := os.MkdirTemp("", "pipeline_test")
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -66,7 +65,7 @@
 	wd, _ := os.Getwd()
 	defer os.Chdir(wd)
 
-	dirs, err := ioutil.ReadDir(testdata)
+	dirs, err := os.ReadDir(testdata)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -124,11 +123,11 @@
 			return os.MkdirAll(filepath.Join(dst, rel), 0755)
 		}
 
-		data, err := ioutil.ReadFile(p)
+		data, err := os.ReadFile(p)
 		if err != nil {
 			return err
 		}
-		return ioutil.WriteFile(filepath.Join(dst, rel), data, 0644)
+		return os.WriteFile(filepath.Join(dst, rel), data, 0644)
 	})
 	if err != nil {
 		t.Fatal(err)
@@ -145,17 +144,17 @@
 
 replace golang.org/x/text => %s
 `, xTextDir)
-	if err := ioutil.WriteFile(filepath.Join(dst, "go.mod"), []byte(goMod), 0644); err != nil {
+	if err := os.WriteFile(filepath.Join(dst, "go.mod"), []byte(goMod), 0644); err != nil {
 		t.Fatal(err)
 	}
 
 	// Copy in the checksums from the parent module so that we won't
 	// need to re-fetch them from the checksum database.
-	data, err := ioutil.ReadFile(filepath.Join(xTextDir, "go.sum"))
+	data, err := os.ReadFile(filepath.Join(xTextDir, "go.sum"))
 	if err != nil {
 		t.Fatal(err)
 	}
-	if err := ioutil.WriteFile(filepath.Join(dst, "go.sum"), data, 0644); err != nil {
+	if err := os.WriteFile(filepath.Join(dst, "go.sum"), data, 0644); err != nil {
 		t.Fatal(err)
 	}
 
@@ -194,17 +193,17 @@
 			return nil
 		}
 
-		got, err := ioutil.ReadFile(gotFile)
+		got, err := os.ReadFile(gotFile)
 		if err != nil {
 			t.Errorf("failed to read %q", gotFile)
 			return nil
 		}
 		if *genFiles {
-			if err := ioutil.WriteFile(wantFile, got, 0644); err != nil {
+			if err := os.WriteFile(wantFile, got, 0644); err != nil {
 				t.Fatal(err)
 			}
 		}
-		want, err := ioutil.ReadFile(wantFile)
+		want, err := os.ReadFile(wantFile)
 		if err != nil {
 			t.Errorf("failed to read %q", wantFile)
 		} else {
@@ -242,7 +241,7 @@
 	if err != nil {
 		t.Fatal(err)
 	}
-	if err := ioutil.WriteFile(path, data, 0644); err != nil {
+	if err := os.WriteFile(path, data, 0644); err != nil {
 		t.Fatal(err)
 	}
 }
diff --git a/transform/transform_test.go b/transform/transform_test.go
index 273abfa..62fad2b 100644
--- a/transform/transform_test.go
+++ b/transform/transform_test.go
@@ -8,7 +8,7 @@
 	"bytes"
 	"errors"
 	"fmt"
-	"io/ioutil"
+	"io"
 	"strconv"
 	"strings"
 	"testing"
@@ -648,7 +648,7 @@
 			// exported API. We override them manually.
 			r.dst = make([]byte, tc.dstSize)
 			r.src = make([]byte, tc.srcSize)
-			got, err := ioutil.ReadAll(r)
+			got, err := io.ReadAll(r)
 			str := string(got)
 			if str != tc.wantStr || err != tc.wantErr {
 				t.Errorf("\ngot  %q, %v\nwant %q, %v", str, err, tc.wantStr, tc.wantErr)
diff --git a/unicode/cldr/decode.go b/unicode/cldr/decode.go
index 48f6bd6..1abcb9e 100644
--- a/unicode/cldr/decode.go
+++ b/unicode/cldr/decode.go
@@ -10,7 +10,6 @@
 	"encoding/xml"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"log"
 	"os"
 	"path/filepath"
@@ -160,7 +159,7 @@
 
 // DecodeZip loads CLDR data from the zip archive for which r is the source.
 func (d *Decoder) DecodeZip(r io.Reader) (cldr *CLDR, err error) {
-	buffer, err := ioutil.ReadAll(r)
+	buffer, err := io.ReadAll(r)
 	if err != nil {
 		return nil, err
 	}
diff --git a/unicode/cldr/makexml.go b/unicode/cldr/makexml.go
index 3c1fd26..528f2c4 100644
--- a/unicode/cldr/makexml.go
+++ b/unicode/cldr/makexml.go
@@ -15,7 +15,6 @@
 	"flag"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"log"
 	"os"
 	"regexp"
@@ -30,7 +29,7 @@
 	flag.Parse()
 
 	r := gen.OpenCLDRCoreZip()
-	buffer, err := ioutil.ReadAll(r)
+	buffer, err := io.ReadAll(r)
 	if err != nil {
 		log.Fatal("Could not read zip file")
 	}
diff --git a/unicode/norm/normalize_test.go b/unicode/norm/normalize_test.go
index 8387d4e..678ca40 100644
--- a/unicode/norm/normalize_test.go
+++ b/unicode/norm/normalize_test.go
@@ -9,7 +9,6 @@
 	"flag"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"log"
 	"os"
 	"os/exec"
@@ -927,14 +926,14 @@
 
 func runNM(code string) (string, error) {
 	// Write the file.
-	tmpdir, err := ioutil.TempDir(os.TempDir(), "normalize_test")
+	tmpdir, err := os.MkdirTemp(os.TempDir(), "normalize_test")
 	if err != nil {
 		return "", fmt.Errorf("failed to create tmpdir: %v", err)
 	}
 	defer os.RemoveAll(tmpdir)
 	goTool := filepath.Join(runtime.GOROOT(), "bin", "go")
 	filename := filepath.Join(tmpdir, "main.go")
-	if err := ioutil.WriteFile(filename, []byte(code), 0644); err != nil {
+	if err := os.WriteFile(filename, []byte(code), 0644); err != nil {
 		return "", fmt.Errorf("failed to write main.go: %v", err)
 	}
 	outputFile := filepath.Join(tmpdir, "main")