(→같이 보기) |
(→Go) |
||
(사용자 3명의 중간 판 57개는 보이지 않습니다) | |||
1번째 줄: | 1번째 줄: | ||
;함수 ReadAllText() | |||
;ReadAllText | |||
==Bash== | ==Bash== | ||
[[category: Bash]] | [[category: Bash]] | ||
< | <syntaxhighlight lang='bash'> | ||
str=`cat test.txt` | str=`cat test.txt` | ||
# John Smith | # John Smith | ||
</ | </syntaxhighlight> | ||
==C== | |||
[[category: C]] | |||
<syntaxhighlight lang='c'> | |||
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); | |||
</syntaxhighlight> | |||
==Cmd== | ==Cmd== | ||
[[category: Cmd]] | [[category: Cmd]] | ||
< | <syntaxhighlight lang='bash'> | ||
for /f "delims=" %a in ('type test.txt') do @set str=%a | for /f "delims=" %a in ('type test.txt') do @set str=%a | ||
echo %str% | echo %str% | ||
</ | </syntaxhighlight> | ||
==C#== | ==C#== | ||
[[category: Csharp]] | [[category: Csharp]] | ||
< | <syntaxhighlight lang='csharp'> | ||
string str = System.IO.File.ReadAllText(@"test.txt"); | string str = System.IO.File.ReadAllText(@"test.txt"); | ||
// John Smith | // John Smith | ||
</ | </syntaxhighlight> | ||
< | <syntaxhighlight lang='csharp'> | ||
public static string file_get_contents(string filepath) | public static string file_get_contents(string filepath) | ||
{ | { | ||
46번째 줄: | 59번째 줄: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
==Go== | |||
[[분류: Go]] | |||
{{참고|Go ReadFile()}} | |||
{{참고|Go readAllText()}} | |||
<syntaxhighlight lang='go' run> | |||
package main | |||
import ( | |||
"fmt" | |||
"os" | |||
) | |||
func main() { | |||
fileBytes, err := os.ReadFile("/etc/hosts") | |||
if err != nil { | |||
// | panic(err) | ||
} | |||
fmt.Println(string(fileBytes)) | |||
} | } | ||
</syntaxhighlight> | |||
</ | |||
< | ==Java== | ||
/ | {{참고|자바 Files.readAllBytes()}} | ||
<syntaxhighlight lang='java'> | |||
String content = new String(Files.readAllBytes(Paths.get("test.txt"))); | |||
System.out.println(content); | |||
</syntaxhighlight> | |||
<syntaxhighlight lang='java'> | |||
String content = new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir"),"test.txt"))); | |||
System.out.println(content); | |||
</syntaxhighlight> | |||
<syntaxhighlight lang='java'> | |||
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 ); | |||
</syntaxhighlight> | |||
==Lua== | |||
[[분류: Lua]] | |||
<syntaxhighlight lang='lua'> | |||
f = io.open('test.txt','rb') | |||
content = f:read('*all') | |||
f:close() | |||
print( content ) | |||
</syntaxhighlight> | |||
</ | |||
==PHP== | ==PHP== | ||
[[category: PHP]] | [[category: PHP]] | ||
< | {{참고|PHP file_get_contents()}} | ||
<syntaxhighlight lang='php'> | |||
$str = file_get_contents('test.txt'); | $str = file_get_contents('test.txt'); | ||
// John Smith | // John Smith | ||
</ | </syntaxhighlight> | ||
==Python== | ==Python== | ||
[[category: Python]] | [[category: Python]] | ||
< | <syntaxhighlight lang='Python'> | ||
f = open('name.txt') | |||
print( | print( f.read() ) | ||
f.close() | |||
</ | </syntaxhighlight> | ||
<syntaxhighlight lang='Python'> | |||
with open('name.txt') as f: | |||
content = f.read() | |||
print( content ) | |||
</syntaxhighlight> | |||
==Perl== | |||
[[category: Perl]] | |||
<syntaxhighlight lang='perl'> | |||
open my $in, '<', 'name.txt'; | |||
local $/; | |||
my $contents = <$in>; | |||
close($in); | |||
print $contents; | |||
</syntaxhighlight> | |||
==Ruby== | ==Ruby== | ||
[[category: Ruby]] | [[category: Ruby]] | ||
< | <syntaxhighlight lang='Ruby'> | ||
str = open('test.txt').read | str = open('test.txt').read | ||
# John Smith | # John Smith | ||
</ | </syntaxhighlight> | ||
==같이 보기== | ==같이 보기== | ||
*[[readlines]] | * [[readlines]] | ||
*[[readline]] | * [[readline]] | ||
*[[ | * [[함수 WriteAllText()]] | ||
*[[bz2_get_contents]] | * [[함수 getTextFromURL()]] | ||
*[[ | * [[함수 bz2_get_contents()]] | ||
*[[File get contents() Permission denied]] | * [[함수 exec()]] | ||
* [[File get contents() Permission denied]] |
2023년 4월 16일 (일) 02:59 기준 최신판
- 함수 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 ReadFile() 문서를 참고하십시오.
Go readAllText() 문서를 참고하십시오.
Go
CPU
-1.0s
MEM
-0M
-1.0s
Copy
package main
import (
"fmt"
"os"
)
func main() {
fileBytes, err := os.ReadFile("/etc/hosts")
if err != nil {
panic(err)
}
fmt.Println(string(fileBytes))
}
127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.2 36e6a7469687
6 Java[ | ]
자바 Files.readAllBytes() 문서를 참고하십시오.
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 file_get_contents() 문서를 참고하십시오.
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
12 같이 보기[ | ]
로그인하시면 댓글을 쓸 수 있습니다.
리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― Jmnote리눅스 Python 2.7 컴파일 설치 ― ㅇㅇㅇ미운코딩새끼 ― 승호 도령미운코딩새끼 ― 불탄고등어미운코딩새끼 ― 김레이미운코딩새끼 ― 호박이미운코딩새끼 ― Junhg0211미운코딩새끼 ― 김왼손미운코딩새끼 ― 용딘이미운코딩새끼 ― Pinkcrimson유기농냠냠파이썬 ― 호박유기농냠냠파이썬 ― 이에스유기농냠냠파이썬 ― 이승현파이썬 global ― Jmnote파이썬 global ― John Jeong파이썬 global ― Jmnote파이썬 global ― John Jeong파이썬 global ― John Jeong파이썬 global ― John Jeong파이썬 global ― Jmnote파이썬 global ― John Jeong