함수 file_put_contents()

Jmnote (토론 | 기여)님의 2021년 8월 28일 (토) 14:57 판 (→‎Bash)
file_put_contents
WriteAllText

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 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();

5 Lua

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

6 PHP

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

7 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() )

8 Perl

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

9 같이 보기

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