RandomAccessFile Nedir?
Basitçe anlatmak gerekirse, '
RandomAccessFile', bir çesit hızlı dosyalama yöntemidir. Mantığı ise; verilen numaradaki kayda ulaşmaktır, bu sayede bir cümle öncede dediğim gibi hızlı bir dosyalama(okuma ve yazma) işlemi gerçekleşir. Tabi göz önünde bulundurulması gereken bir dejavantajı bulunuyor, bu hızlı dosyalama olanağı dosyadaki verilerin cihaz belleğinde(RAM) daha fazla bulundurulmasını gerektiriyor, yani bellekte daha fazla yer kaplıyor, Ayrıca anlaşılacağı üzere normal '
File'dan farklı bir kullanım şekli bulunuyor.
RandomAccessFile Constructor ve Method'lar
Required Imports
import java.io.RandomAccessFile;
import java.io.FileNotFoundException;
Constructor
RandomAccessFile(String path, String mode) throws FileNotFoundException
'path' kısmına dosya yolunu, 'mode' kısmına ise erişim modunu yazıyoruz.
Access(Erişim) Modes
- r - Sadece okuma izni var.
- rw - Okuma ve yazma izni var, Dosya bulunmuyorsa oluşturmayı dener.
- rws - Okuma ve yazma izni var, dosya içeriğinde veya meta verisinde gerçekleşen güncellemeleri eş zamanlı olarak kaydeder.
- rwd - Okuma ve yazma izni var, dosya içeriğindeki gerçekleşen tüm güncellemeleri eş zamanlı olarak kaydeder.
Seek(Atla) Method
void Seek(Long pos) throws IOException
İsaretleyiciyi belli bir kayda taşır.
Write(Yaz) Methods
void write(Byte[] b, int off, int len) throws IOException
void write(Byte[] b) throws IOException
void write(int b) throws IOException
void writeBoolean(Boolean v) throws IOException
void writeByte(int v) throws IOException
void writeBytes(String s) throws IOException
void writeChar(int v) throws IOException
void writeChars(String s) throws IOException
void writeDouble(double v) throws IOException
void writeFloat(float v) throws IOException
void writeLong(long v) throws IOException
void writeShort(int v) throws IOException
void writeUTF(String str) throws IOException
void writeInt(int v) thows IOException
Read(Oku) Methods
int read(Byte[] b) throws IOException
int read() throws IOException
int read(Byte[] b, int off, int len) throws IOException
boolean readBoolean() throws IOException
Byte readByte() throws IOException
char readChar() throws IOException
double readDouble() throws IOException
float readFloat() throws IOException
void readFully(Byte[] b) throws IOException
void readFully(Byte[] b, int off, int len) throws IOException
String readLine() throws IOException
long readLong() throws IOException
short readShort() throws IOException
int readUnsignedByte() throws IOException
int readUnsignedShort() throws IOException
String readUTF() throws IOException
int readInt() throws IOException
Açıklamalı Kullanım Örneği
Yazma
İlk olarak RandomAccessFile değişkenimizi, main method içinde oluşturup tanımlayalım.
RandomAccessFile raf = new RandomAccessFile("/sdcard/test.txt", "rw");
Şimdi ise basit olarak bir kaç veriyi kaydedecez.
raf.seek(0); // Varsayılan olarak zaten sıfırda oluyor 😅
raf.writeUTF("naifcanbasci.blogspot.com");
raf.seek(1);
raf.writeInt(1001);
Şuan iki farklı türde veriyi bir dosyada kaydettik.
Okuma
İlk olarak RandomAccessFile değişkenimizi, main method içinde oluşturup tanımlayalım.
RandomAccessFile raf = new RandomAccessFile("/sdcard/test.txt", "rw");
Şimdide daha önce kaydettiğimiz verileri okuyacağız.
raf.seek(0);
System.out.printf('Data at 0 %s \n', raf.readUTF());
raf.seek(1);
System.out.println('Data at 1 %s \n', raf.readInt());
Bu kadar🎉 okuduğunuz için teşekkürler, görüşlerinizi yorumlarda belirtmeyi unutmayın😀
Yorumlar
Yorum Gönder