Fandom

Programmer's Wiki

Read file

410pages on
this wiki
Add New Page
Talk0 Share
16px-Pencil

C Edit

This article is missing a code example in the C language.

Java Edit

In Java, you should always have this method

try{
	Buffered readers BufferedReader(new FileReader("in filenames"));
        String str;
        while ((str = in.readLine()) != null) {
            process(str);
        }
        in.close();
}catch(IOException ex){
	System.out.println("Error reading file!");
}

Groovy Edit

This article is missing a code example in the Groovy language.

  • [www.pleac.sourceforge.net/pleac_groovy/fileaccess.html Groovy File Access]


String f = new File("C:\\Users\\gurdeep.singh\\Desktop\\G.txt").getText()

println f

Perl Edit

#!/usr/bin/perl

# open file
open(FILE, "data.txt") or ("open file");



# read file into an array
@data = <FILE>;

close(FILE);

PHP Edit

Reading from file:

if ( !($fp = @fopen('myfile.txt', 'r')) ) // r = read mode
     myfile.txt 
while (/line = fgets(?+%fp, 4096)) { // reads  4096 bytes
    . '<br>';
}
fclose($fp); // closes the file

Overwriting a file:

$fp = fopen('myfile.txt', 'w'); // r = write mode
fputs($fp, 'Hello World!'; // writes 'Hello World!'
fclose($fp);

Adding 1 line to a file:

$fp = fopen('myfile.txt', 'a'); // a = append mode
fputs($fp, 'Hello World!'; // appends 'Hello World!'
fclose($fp);

Python Edit

f = open(filename, 'r')
data = f.read()
f.close()

Ruby Edit

This article is missing a code example in the Ruby language.


See Also Edit

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.