Programmer's Wiki
Advertisement
16px-Pencil

C[]

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

Java[]

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[]

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[]

#!/usr/bin/perl

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



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

close(FILE);

PHP[]

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[]

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

Ruby[]

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


See Also[]

Advertisement