C

#include<stdio.h>

int main()
{
    FILE *read;
    FILE *write;
    char *filename = "myfile.txt";
    read = fopen( filename, "r" );
    if( read == NULL )
    {
        //File doesn't exist. Have to write something
        write = fopen( filename, "w" );
        fprintf( write, "%s", "writesomethinghere" );
        fclose( write );
    }
    else
    {
        //File exists. Read data from the file
        char somedata[ 20 ];
        fscanf( read, "%s", somedata );
        printf( "%s", somedata );
        fclose( read );
    }
    return 0;
}

C++


#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
    ifstream readfile;
    ofstream writefile;
    string filename = "myfile.txt";
    readfile.open( filename.c_str() );
    if( readfile )
    {
        //I can read something from the file
        readfile.close();
    }
    else
    {
        //The file doesn't exist
        writefile.open( filename.c_str() );
        //write something to the file
        writefile << " write some data " <&lt; endl;
        writefile.close();
    }
    return 0;
}

JAVA


import java.io.*;
import java.lang.*;

public class Solution
{
    public static void main( String[] args )
    {
        File fileName = new File( "myfile.txt" );
        if( !fileName.exists() )
        {
            System.out.println( "this file doesn't exist " );
            try
            {
                fileName.createNewFile();
                FileWriter fileWrite = new FileWriter( fileName );
                BufferedWriter bufferedWriter = new BufferedWriter( fileWrite );
                //bufferedWriter.write( "write something here " );
                bufferedWriter.close();
            } catch ( IOException e )
            {
                //catch exception
            }
        }
        else
        {
            //System.out.println( "this file exists " );
            try
            {
                byte[] buffer = new byte[ 100 ];
                FileInputStream inputStream  = new FileInputStream( fileName );
                int readLines = -1;
                while( ( readLines = inputStream.read( buffer ) ) != -1 )
                {
                    //System.out.println( new String( buffer ) );
                }
                inputStream.close();
            } catch ( IOException e )
            {
                //catch exception
            }
        }
    }
}

CSHARP

using System;
using System.IO;


public class Solution
{
    public static void Main( )
    {
        string fileName = "myfile.txt";
        FileStream file = new FileStream( fileName, FileMode.OpenOrCreate, FileAccess.Read );
        StreamReader sr = new StreamReader( file );
        string s = sr.ReadToEnd();
        if( string.IsNullOrEmpty( s ) )
        {
            file.Close();
            //The file is empty. Write something to it
            //Console.WriteLine( "file doesn't exist " );
            file = new FileStream( fileName, FileMode.OpenOrCreate, FileAccess.Write );
            StreamWriter sw = new StreamWriter( file );
            sw.Write( "Write something here " );
            sw.Close();
        }
        else
        {
            //Console.WriteLine( s );
        }
    }
}

PYTHON


#!/usr/bin/python

filename = "myfile.txt"
with open( filename ) as f:
    # file read can happen here
    # print "file exists"
    print f.readlines()

with open( filename, "w") as f:
    # print "file write happening here"
    f.write("write something here ")

            

RUBY

#!/usr/bin/env ruby

filename = "myfile.txt"
if File.exist?( filename )
    #puts "file read happening"
    f = File.open(filename, 'r')
    data = f.read
    puts data
end

f = File.open(filename, 'a+')
#puts "file write happening"
f.puts "write something here"

PHP

<?php
$filename = "myfile.txt";
if(file_exists($filename)) {
    $data = file_get_contents( $filename );
    print $data;
    $filewrite = fopen( $filename, 'a') or die('Cannot open file:'.$filename);
    fwrite( $filewrite, "write something here");
    fclose( $filewrite );
} else {
    $filewrite = fopen( $filename, 'w') or die('Cannot open file:'.$filename);
    fwrite( $filewrite, "write something here");
    fclose( $filewrite );
}
?>

PERL


#!/usr/bin/env perl

$filename = "myfile.txt";

if( -e $filename )
{
    unless(open FILE, $filename)
    {
        die "Cannot open file";
    }

    while(my $line = <FILE>)
    {
        print $line;
    }

    close FILE;
}
unless(open FILE, '>>'.$filename)
{
    die "Cannot open file";
}
print FILE "Write something here";
close FILE;