Wednesday, 23 November 2016

Java Tutorial : Java IO (Java RandomAccessFile - V1)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=2xzXz6965RA&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java IO (Java RandomAccessFile - V1) 
Java Tutorial : Java IO (Java RandomAccessFile - V1) 
Java Tutorial : Java IO (Java RandomAccessFile - V1) 
Java Tutorial : Java IO (Java RandomAccessFile - V1) 
Java Tutorial : Java IO (Java RandomAccessFile - V1) 
Java Tutorial : Java IO (Java RandomAccessFile - V1) 
myfile.txt
John visits to india and went to Bangalore,TamilNadu and went to Delhi to visit Prime minister. 

RandomAccessFileReadDemo.java
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileReadDemo
{
    public static void main(String[] args) throws IOException
    {

        RandomAccessFile randomAccessFile = null;
        try
        {
            /*
             * Second input parameter to the constructor:
             * "r". This is the mode you want to open file
             * in. "r" means read mode.
             */
            randomAccessFile = new RandomAccessFile("myfile.txt", "r");

            /*
             * Sets the file-pointer offset, measured from
             * the beginning of this file, at which the next
             * read or write occurs.
             */
            randomAccessFile.seek(57);

            int byteValue;
            while ((byteValue = randomAccessFile.read()) != -1)
            {
                System.out.print((char) byteValue);
            }
        }
        finally
        {
            if (randomAccessFile != null)
            {
                randomAccessFile.close();
            }
        }

    }
}
Output
went to Delhi to visit Prime minister. 

RandomAccessFileWriteDemo.java
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileWriteDemo
{
    public static void main(String[] args) throws IOException
    {

        RandomAccessFile randomAccessFile = null;
        try
        {
            /*
             * Second input parameter to the constructor:
             * "rw". This is the mode you want to open file
             * in. "rw" means read/write mode.
             */
            randomAccessFile = new RandomAccessFile("myfile.txt", "rw");

            /*
             * Sets the file-pointer offset, measured from
             * the beginning of this file, at which the next
             * read or write occurs.
             */
            randomAccessFile.seek(43);
            randomAccessFile.write("Hydrabad ".getBytes());
            System.out.println("Successfully updated the file content..");
        }
        finally
        {
            if (randomAccessFile != null)
            {
                randomAccessFile.close();
            }
        }

    }
}
Output
Successfully updated the file content..

Refer: 
https://docs.oracle.com/javase/8/docs/api/index.html?java/io/RandomAccessFile.html

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_RandomAccessFile_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_RandomAccessFile_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/a3d05269db9922e504bb1f9de8d07031990c9910/BasicJava/JavaIODemo_RandomAccessFile_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment