- 함수 WriteAllText()
- 함수 file_put_contents()
1 Bash[ | ]

Bash
Copy
echo "John Smith" > test.txt
cat test.txt
# John Smith
2 CMD[ | ]
Bash
Copy
echo John Smith > test.txt
type test.txt
REM John Smith
3 C#[ | ]

C#
Copy
System.IO.File.WriteAllText(@"test.txt", "John Smith");
C#
Copy
StreamWriter sw = new StreamWriter("test.txt");
sw.Write("John Smith");
sw.Close();
4 Go[ | ]

Go
Copy
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")
}
Loading
5 Java[ | ]

Java
Copy
BufferedWriter bw = new BufferedWriter( new FileWriter( "test.txt" ) );
bw.write( "John Smith" );
bw.close();
Java
Copy
PrintWriter pw = new PrintWriter( "test.txt" );
pw.println( "John Smith" );
pw.close();
Java
Copy
PrintWriter pw = new PrintWriter( new FileWriter( "test.txt" ) );
pw.println( "John Smith" );
pw.close();
6 Lua[ | ]
lua
Copy
s = 'His name is\nJohn Smith'
f = io.open('test.txt','w')
f:write(s)
f:close()
7 PHP[ | ]

PHP
Copy
file_put_contents("test.txt", "John Smith");
8 Python[ | ]
Python
Copy
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
Python
Copy
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[ | ]
Perl
Copy
my $s = "His name is\nJohn Smith";
open my $fh, ">", 'name.txt';
print $fh $s;
close $fh;