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

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
8번째 줄: 8번째 줄:
==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==
==C==
[[category: C]]
[[category: C]]
<source lang='c'>
<syntaxhighlight lang='c'>
char* buffer = 0;
char* buffer = 0;
long length;
long length;
28번째 줄: 28번째 줄:


printf("%s", buffer);
printf("%s", buffer);
</source>
</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)
{
{
64번째 줄: 64번째 줄:
   }
   }
}
}
</source>
</syntaxhighlight>


==Java==
==Java==
{{참고|자바 Files.readAllBytes()}}
{{참고|자바 Files.readAllBytes()}}
<source lang='java'>
<syntaxhighlight lang='java'>
String content = new String(Files.readAllBytes(Paths.get("test.txt")));
String content = new String(Files.readAllBytes(Paths.get("test.txt")));
System.out.println(content);
System.out.println(content);
</source>
</syntaxhighlight>
<source lang='java'>
<syntaxhighlight lang='java'>
String content = new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir"),"test.txt")));
String content = new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir"),"test.txt")));
System.out.println(content);
System.out.println(content);
</source>
</syntaxhighlight>
<source lang='java'>
<syntaxhighlight lang='java'>
String content = "";
String content = "";
try {
try {
URI uri = getClass().getClassLoader().getResource("test.txt").toURI();
URI uri = getClass().getClassLoader().getResyntaxhighlight("test.txt").toURI();
content = new String(Files.readAllBytes(Paths.get(uri)));
content = new String(Files.readAllBytes(Paths.get(uri)));
} catch (URISyntaxException | IOException e) { e.printStackTrace(); }
} catch (URISyntaxException | IOException e) { e.printStackTrace(); }
System.out.println( content );
System.out.println( content );
</source>
</syntaxhighlight>
<source lang='java'>
<syntaxhighlight lang='java'>
URL url = new URL("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
URL url = new URL("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
Scanner scanner = new Scanner(url.openStream(), "UTF-8");
Scanner scanner = new Scanner(url.openStream(), "UTF-8");
91번째 줄: 91번째 줄:
scanner.close();
scanner.close();
System.out.println(content);
System.out.println(content);
</source>
</syntaxhighlight>
<source lang='java'>
<syntaxhighlight lang='java'>
URL url = new URL("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
URL url = new URL("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
99번째 줄: 99번째 줄:
while ((line = in.readLine()) != null) content += line + System.getProperty("line.separator");
while ((line = in.readLine()) != null) content += line + System.getProperty("line.separator");
System.out.println(content);
System.out.println(content);
</source>
</syntaxhighlight>
<source lang='java'>
<syntaxhighlight lang='java'>
URL url = new URL("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
URL url = new URL("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
108번째 줄: 108번째 줄:
String content = String.join(System.getProperty("line.separator"), lines);
String content = String.join(System.getProperty("line.separator"), lines);
System.out.println(content);
System.out.println(content);
</source>
</syntaxhighlight>


==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
<source lang='JavaScript'>
<syntaxhighlight lang='JavaScript'>
var xhr = new XMLHttpRequest();
var xhr = new XMLHttpRequest();
xhr.open("GET",'https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt',false);
xhr.open("GET",'https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt',false);
xhr.send();
xhr.send();
console.log( xhr.responseText );
console.log( xhr.responseText );
</source>
</syntaxhighlight>
<source lang='JavaScript'>
<syntaxhighlight lang='JavaScript'>
function file_get_contents(url) {
function file_get_contents(url) {
var xhr = new XMLHttpRequest();
var xhr = new XMLHttpRequest();
126번째 줄: 126번째 줄:
}
}
console.log( file_get_contents('https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt') );
console.log( file_get_contents('https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt') );
</source>
</syntaxhighlight>


==Lua==
==Lua==
[[분류: Lua]]
[[분류: Lua]]
<source lang='lua'>
<syntaxhighlight lang='lua'>
f = io.open('test.txt','rb')
f = io.open('test.txt','rb')
content = f:read('*all')
content = f:read('*all')
f:close()
f:close()
print( content )
print( content )
</source>
</syntaxhighlight>


==Objective-C==
==Objective-C==
[[category: Objective-C]]
[[category: Objective-C]]
<source lang='objc'>
<syntaxhighlight lang='objc'>
//  ViewController.h
//  ViewController.h
#import <UIKit/UIKit.h>
#import <UIKit/UIKit.h>
146번째 줄: 146번째 줄:
}
}
@end
@end
</source>
</syntaxhighlight>
<source lang='objc'>
<syntaxhighlight lang='objc'>
//  ViewController.m
//  ViewController.m
#import "ViewController.h"
#import "ViewController.h"
162번째 줄: 162번째 줄:
}
}
@end
@end
</source>
</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')
with open('name.txt', 'w') as w: w.write('His name is\nJohn Smith')


182번째 줄: 182번째 줄:
# His name is
# His name is
# John Smith
# John Smith
</source>
</syntaxhighlight>
<source lang='Python'>
<syntaxhighlight 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() )
     print( f.read() )
</source>
</syntaxhighlight>


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang='perl'>
<syntaxhighlight lang='perl'>
open my $in, '<', 'name.txt';
open my $in, '<', 'name.txt';
local $/;
local $/;
201번째 줄: 201번째 줄:
close($in);
close($in);
print $contents;
print $contents;
</source>
</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'>
<syntaxhighlight lang='Ruby'>
require 'open-uri'
require 'open-uri'
h = open('https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt);
h = open('https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt);
contents = h.read
contents = h.read
print contents
print contents
</source>
</syntaxhighlight>


==UnityScript==
==UnityScript==
[[category: UnityScript]]
[[category: UnityScript]]
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
var www:WWW = new WWW("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
var www:WWW = new WWW("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
yield www;
yield www;
Debug.Log(www.text);
Debug.Log(www.text);
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:32 판

  다른 뜻에 대해서는 PHP file_get_contents() 문서를 참조하십시오.
file_get_contents
read
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 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 );
URL url = new URL("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
Scanner scanner = new Scanner(url.openStream(), "UTF-8");
scanner.useDelimiter("\\A");
String content = scanner.next();
scanner.close();
System.out.println(content);
URL url = new URL("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
String content = "";
while ((line = in.readLine()) != null) content += line + System.getProperty("line.separator");
System.out.println(content);
URL url = new URL("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
List<String> lines = new ArrayList<String>();
while ((line = in.readLine()) != null) lines.add(line);
String content = String.join(System.getProperty("line.separator"), lines);
System.out.println(content);

6 JavaScript

var xhr = new XMLHttpRequest();
xhr.open("GET",'https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt',false);
xhr.send();
console.log( xhr.responseText );
function file_get_contents(url) {
	var xhr = new XMLHttpRequest();
	xhr.open("GET", url, false);
	xhr.send();
	return xhr.responseText;
}
console.log( file_get_contents('https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt') );

7 Lua

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

8 Objective-C

//  ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <NSURLConnectionDelegate> {
    NSURLConnection* urlconn;
}
@end
//  ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    urlconn = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt"]] delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
@end

9 PHP

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

10 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
f = open('name.txt')
print( f.read() )
f.close()
with open('name.txt') as f:
    print( f.read() )

11 Perl

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

12 Ruby

str = open('test.txt').read
# John Smith
require 'open-uri'
h = open('https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt);
contents = h.read
print contents

13 UnityScript

var www:WWW = new WWW("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt");
yield www;
Debug.Log(www.text);

14 같이 보기

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