Sunday, 11 June 2017

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


Click here to watch in Youtube :

Click the below Image to Enlarge
Java Tutorial : Java IO (Java RandomAccessFile - V2) 
Java Tutorial : Java IO (Java RandomAccessFile - V2) 
Java Tutorial : Java IO (Java RandomAccessFile - V2) 
Java Tutorial : Java IO (Java RandomAccessFile - V2) 
myfile.txt
John visits to india and Japan
 
RandomAccessFileReadDemo.java
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileReadDemo
{
    public static void main(String[] args) throws IOException
    {
        RandomAccessFileReadDemo randomAccessFileReadDemo = new RandomAccessFileReadDemo();
        randomAccessFileReadDemo.readData("myfile.txt", "r", 5);
    }

    private void readData(String fileName, String mode, int position)
            throws FileNotFoundException, IOException
    {
        RandomAccessFile randomAccessFile = null;
        try
        {

            randomAccessFile = new RandomAccessFile(fileName, mode);

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

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

}
Output
visits to india and Japan 

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

public class RandomAccessFileWriteDemo
{
    public static void main(String[] args) throws IOException
    {
        RandomAccessFileWriteDemo randomAccessFileWriteDemo = new RandomAccessFileWriteDemo();
        randomAccessFileWriteDemo.writeData("myfile.txt", "rw", 20);
    }

    private void writeData(String fileName, String mode, int position)
            throws FileNotFoundException, IOException
    {
        RandomAccessFile randomAccessFile = null;
        try
        {
            randomAccessFile = new RandomAccessFile(fileName, mode);

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

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment