Site icon WebArtDeveloper

How to Read and Write from the File using RandomAccessFile class in Core Java

In this tutorial how to read and write from the file using RandomAccessFile class in Core Java  is shown.

Code:

RandomAccessFileDemo.java

import java.io.*;

class RandomAccessFileDemo
{
public static void main(String args[]) throws FileNotFoundException
{
File f1 = new File(“Demo.txt”);
int a=123;
long b=435523;
String s=”Here is some text”;

try
{
RandomAccessFile raf = new RandomAccessFile(f1,”rw”);
raf.writeInt(a);
raf.writeLong(b);
raf.writeUTF(s);
raf.seek(0);
System.out.println(raf.read());
raf.close();
}
catch(IOException ex)
{
System.out.println(ex.toString());
}
}
}

https://youtu.be/dT1sp3GYHWk

Exit mobile version