Sunday, 11 June 2017

Java Tutorial : Java IO (Java RandomAccessFile - V1) ~ foundjava


Click here to watch in Youtube :

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:

CLICK HERE

No comments:

Post a Comment