import java.io.RandomAccessFile; import java.io.IOException; public class FileReader { public static void main(String[] args) { try { RandomAccessFile file = new RandomAccessFile("example.txt", "r"); byte[] bytes = new byte[10]; file.seek(5); // move file pointer to position 5 file.read(bytes); // read 10 bytes from the file System.out.println(new String(bytes)); file.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.RandomAccessFile; import java.io.IOException; public class FileWriter { public static void main(String[] args) { try { RandomAccessFile file = new RandomAccessFile("example.txt", "rw"); file.seek(file.length()); // move file pointer to end of file String message = "Hello, world!"; file.write(message.getBytes()); // write the message to file file.close(); } catch (IOException e) { e.printStackTrace(); } } }This example appends the message "Hello, world!" to the end of the file "example.txt" by moving the file pointer to the end of the file using the length() method. The message is then written to the file using the write() method. Both examples use the java.io package, which is part of the core Java library.