"함수 readAllText()"의 두 판 사이의 차이

 
(사용자 3명의 중간 판 43개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[category: File]]
;함수 ReadAllText()
{{lowercase title}}
{{다른뜻|PHP file_get_contents()}}
;file_get_contents
;read
;ReadAllText


==Bash==
==Bash==
[[category: Bash]]
[[category: Bash]]
<source lang='bash'>
<syntaxhighlight lang='bash'>
str=`cat test.txt`
str=`cat test.txt`
# John Smith
# John Smith
</source>
</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]]
<source lang='bash'>
<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%
</source>
</syntaxhighlight>


==C#==
==C#==
[[category: Csharp]]
[[category: Csharp]]
<source lang='csharp'>
<syntaxhighlight lang='csharp'>
string str = System.IO.File.ReadAllText(@"test.txt");
string str = System.IO.File.ReadAllText(@"test.txt");
// John Smith
// John Smith
</source>
</syntaxhighlight>
<source lang='csharp'>
<syntaxhighlight lang='csharp'>
public static string file_get_contents(string filepath)
public static string file_get_contents(string filepath)
{
{
47번째 줄: 59번째 줄:
   }
   }
}
}
</source>
</syntaxhighlight>


==JavaScript==
==Go==
[[category: JavaScript]]
[[분류: Go]]
<source lang='JavaScript'>
{{참고|Go ReadFile()}}
var xhr = new XMLHttpRequest();
{{참고|Go readAllText()}}
xhr.open("GET",'http://zetawiki.com/ex/txt/utf8test.txt',false);
<syntaxhighlight lang='go' run>
xhr.send();
package main
console.log( xhr.responseText );
</source>


==Objective-C==
import (
[[category: Objective-C]]
"fmt"
<source lang='objc'>
"os"
// ViewController.h
)
#import <UIKit/UIKit.h>
 
@interface ViewController : UIViewController <NSURLConnectionDelegate> {
func main() {
    NSURLConnection* urlconn;
fileBytes, err := os.ReadFile("/etc/hosts")
if err != nil {
panic(err)
}
fmt.Println(string(fileBytes))
}
}
@end
</syntaxhighlight>
</source>
<source lang='objc'>
//  ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end


@implementation ViewController
==Java==
- (void)viewDidLoad {
{{참고|자바 Files.readAllBytes()}}
    [super viewDidLoad];
<syntaxhighlight lang='java'>
    urlconn = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://zetawiki.com/ex/txt/utf8test.txt"]] delegate:self];
String content = new String(Files.readAllBytes(Paths.get("test.txt")));
}
System.out.println(content);
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
</syntaxhighlight>
    NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
<syntaxhighlight lang='java'>
}
String content = new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir"),"test.txt")));
@end
System.out.println(content);
</source>
</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()}}
{{참고|PHP file_get_contents()}}
<source lang='php'>
<syntaxhighlight lang='php'>
$str = file_get_contents('test.txt');
$str = file_get_contents('test.txt');
// John Smith
// John Smith
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<source lang='Python'>
<syntaxhighlight lang='Python'>
with open('name.txt', 'w') as w: w.write('His name is\nJohn Smith')
 
f = open('name.txt', 'r')
print( f.read() )
f.close()
# His name is
# John Smith
</source>
<source lang='Python'>
f = open('name.txt')
f = open('name.txt')
print( f.read() )
print( f.read() )
f.close()
f.close()
</source>
</syntaxhighlight>
<source lang='Python'>
<syntaxhighlight lang='Python'>
with open('name.txt') as f:
with open('name.txt') as f:
     print( f.read() )
     content = f.read()
</source>
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]]
<source lang='Ruby'>
<syntaxhighlight lang='Ruby'>
str = open('test.txt').read
str = open('test.txt').read
# John Smith
# John Smith
</source>
</syntaxhighlight>
<source lang='Ruby'>
require 'open-uri'
h = open('http://zetawiki.com/ex/txt/utf8test.txt');
contents = h.read
print contents
</source>
 
==UnityScript==
[[category: UnityScript]]
<source lang='javascript'>
var www:WWW = new WWW("http://zetawiki.com/ex/txt/utf8test.txt");
yield www;
Debug.Log(www.text);
</source>


==같이 보기==
==같이 보기==
*[[readlines]]
* [[readlines]]
*[[readline]]
* [[readline]]
*[[함수 file_put_contents()]]
* [[함수 WriteAllText()]]
*[[함수 get_http_code()]]
* [[함수 getTextFromURL()]]
*[[bz2_get_contents]]
* [[함수 bz2_get_contents()]]
*[[wget]]
* [[함수 exec()]]
*[[File get contents() Permission denied]]
* [[File get contents() Permission denied]]

2023년 4월 16일 (일) 02:59 기준 최신판

함수 ReadAllText()

1 Bash[ | ]

str=`cat test.txt`
# John Smith

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

3 Cmd[ | ]

for /f "delims=" %a in ('type test.txt') do @set str=%a
echo %str%

4 C#[ | ]

string str = System.IO.File.ReadAllText(@"test.txt");
// John Smith
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[ | ]

package main

import (
	"fmt"
	"os"
)

func main() {
	fileBytes, err := os.ReadFile("/etc/hosts")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(fileBytes))
}

6 Java[ | ]

String content = new String(Files.readAllBytes(Paths.get("test.txt")));
System.out.println(content);
String content = new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir"),"test.txt")));
System.out.println(content);
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[ | ]

f = io.open('test.txt','rb')
content = f:read('*all')
f:close()
print( content )

8 PHP[ | ]

$str = file_get_contents('test.txt');
// John Smith

9 Python[ | ]

f = open('name.txt')
print( f.read() )
f.close()
with open('name.txt') as f:
    content = f.read()
print( content )

10 Perl[ | ]

open my $in, '<', 'name.txt';
local $/;
my $contents = <$in>;
close($in);
print $contents;

11 Ruby[ | ]

str = open('test.txt').read
# John Smith

12 같이 보기[ | ]

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