함수 readAllText()

Jmnote (토론 | 기여)님의 2017년 7월 12일 (수) 20:07 판 (→‎Java)

  다른 뜻에 대해서는 PHP file_get_contents() 문서를 참조하십시오.
file_get_contents
read
ReadAllText

1 Bash

str=`cat test.txt`
# John Smith

2 Cmd

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

3 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 "";
   }
}

4 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().getResource("json/test.json").toURI();
	content = new String(Files.readAllBytes(Paths.get(uri)));
} catch (URISyntaxException | IOException e) { e.printStackTrace(); }
System.out.println( content );
URL url = new URL("http://example.zetawiki.com/txt/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("http://example.zetawiki.com/txt/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("http://example.zetawiki.com/txt/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);

5 JavaScript

var xhr = new XMLHttpRequest();
xhr.open("GET",'http://example.zetawiki.com/txt/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('http://example.zetawiki.com/txt/utf8test.txt') );

6 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:@"http://example.zetawiki.com/txt/utf8test.txt"]] delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
@end

7 PHP

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

8 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() )

9 Ruby

str = open('test.txt').read
# John Smith
require 'open-uri'
h = open('http://example.zetawiki.com/txt/utf8test.txt');
contents = h.read
print contents

10 UnityScript

var www:WWW = new WWW("http://example.zetawiki.com/txt/utf8test.txt");
yield www;
Debug.Log(www.text);

11 같이 보기