- 함수 ReadAllText()
1 Bash[ | ]
Bash
Copy
str=`cat test.txt`
# John Smith
2 C[ | ]
C
Copy
char* buffer = 0;
long length;
FILE* fp = fopen("/etc/lsb-release", "rb");
fseek( fp, 0, SEEK_END );
length = ftell( fp );
fseek( fp, 0, SEEK_SET );
buffer = malloc( length );
fread( buffer, 1, length, fp );
fclose( fp );
printf("%s", buffer);
3 Cmd[ | ]
Bash
Copy
for /f "delims=" %a in ('type test.txt') do @set str=%a
echo %str%
4 C#[ | ]
C#
Copy
string str = System.IO.File.ReadAllText(@"test.txt");
// John Smith
C#
Copy
public static string file_get_contents(string filepath)
{
try
{
if (filepath.ToLower().IndexOf("tp://") > 0)
{
System.Net.WebClient wc = new System.Net.WebClient();
byte[] response = wc.DownloadData(filepath);
return System.Text.Encoding.UTF8.GetString(response);
}
System.IO.StreamReader sr = new System.IO.StreamReader(filepath);
string contents = sr.ReadToEnd();
sr.Close();
return contents;
}
catch
{
return "";
}
}
5 Go[ | ]


Go
Copy
package main
import (
"fmt"
"os"
)
func main() {
fileBytes, err := os.ReadFile("/etc/hosts")
if err != nil {
panic(err)
}
fmt.Println(string(fileBytes))
}
Loading
6 Java[ | ]

Java
Copy
String content = new String(Files.readAllBytes(Paths.get("test.txt")));
System.out.println(content);
Java
Copy
String content = new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir"),"test.txt")));
System.out.println(content);
Java
Copy
String content = "";
try {
URI uri = getClass().getClassLoader().getResyntaxhighlight("test.txt").toURI();
content = new String(Files.readAllBytes(Paths.get(uri)));
} catch (URISyntaxException | IOException e) { e.printStackTrace(); }
System.out.println( content );
7 Lua[ | ]
lua
Copy
f = io.open('test.txt','rb')
content = f:read('*all')
f:close()
print( content )
8 PHP[ | ]

PHP
Copy
$str = file_get_contents('test.txt');
// John Smith
9 Python[ | ]
Python
Copy
f = open('name.txt')
print( f.read() )
f.close()
Python
Copy
with open('name.txt') as f:
content = f.read()
print( content )
10 Perl[ | ]
Perl
Copy
open my $in, '<', 'name.txt';
local $/;
my $contents = <$in>;
close($in);
print $contents;
11 Ruby[ | ]
Ruby
Copy
str = open('test.txt').read
# John Smith