함수 WriteAllText()

(File put contents에서 넘어옴)
함수 WriteAllText()
함수 file_put_contents()

1 Bash[ | ]

echo "John Smith" > test.txt
cat test.txt
# John Smith

2 CMD[ | ]

echo John Smith > test.txt
type test.txt
REM John Smith

3 C#[ | ]

System.IO.File.WriteAllText(@"test.txt", "John Smith");
StreamWriter sw = new StreamWriter("test.txt");
sw.Write("John Smith");
sw.Close();

4 Go[ | ]

package main

import (
	"fmt"
	"os"
)

func WriteAllText(filePath string, text string) (err error) {
	f, err := os.Create(filePath)
	if err != nil {
		return fmt.Errorf("error on Create: %w", err)
	}
	defer f.Close()
	_, err = f.WriteString(text)
	if err != nil {
		return fmt.Errorf("error on WriteString: %w", err)
	}
	return nil
}

func main() {
	err := WriteAllText("hello.txt", "Hello, 世界")
	if err != nil {
		panic(err)
	}
	fmt.Println("ok")
}

5 Java[ | ]

BufferedWriter bw = new BufferedWriter( new FileWriter( "test.txt" ) );
bw.write( "John Smith" );
bw.close();
PrintWriter pw = new PrintWriter( "test.txt" );
pw.println( "John Smith" );
pw.close();
PrintWriter pw = new PrintWriter( new FileWriter( "test.txt" ) );
pw.println( "John Smith" );
pw.close();

6 Lua[ | ]

s = 'His name is\nJohn Smith'
f = io.open('test.txt','w')
f:write(s)
f:close()

7 PHP[ | ]

file_put_contents("test.txt", "John Smith");

8 Python[ | ]

s = 'His name is\nJohn Smith'
f = open('name.txt', 'w')
f.write(s)
f.close()

with open('name.txt') as f2: print( f2.read() )
# His name is
# John Smith
s = 'His name is\nJohn Smith'
with open('name.txt', 'w') as f:
    f.write(s)
 
with open('name.txt') as f2: print( f2.read() )

9 Perl[ | ]

my $s = "His name is\nJohn Smith";
open my $fh, ">", 'name.txt';
print $fh $s;
close $fh;

10 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}