Questions tagged [randomaccessfile]

Creates a random access file stream to read from, and optionally to write to, a file with the specified name.

A new FileDescriptor object is created to represent the connection to the file. The mode argument specifies the access mode with which the file is to be opened. The permitted values and their meanings are as specified for the RandomAccessFile(File,String) constructor.

If there is a security manager, its checkRead method is called with the name argument as its argument to see if read access to the file is allowed. If the mode allows writing, the security manager's checkWrite method is also called with the name argument as its argument to see if write access to the file is allowed.

Parameters

  • name - the system-dependent filename
  • mode - the access mode

Throws

  • IllegalArgumentException - if the mode argument is not equal to one of "r", "rw", "rws", or "rwd"
  • FileNotFoundException - if the mode is "r" but the given string does not denote an existing regular file, or if the mode begins with "rw" but the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
  • SecurityException - if a security manager exists and its checkRead method denies read access to the file or the mode is "rw" and the security manager's checkWrite method denies write access to the file
359 questions
4
votes
1 answer

Why does Java FileChannel.truncate fail predictably on Windows only?

I have a web application running in Wildfly, and part of the application uses a temporary file via: File.createTempFile(...). That temporary file is then used as the destination file for a read-write java.io.RandomAccessFile stream. The…
DDoomUs
  • 183
  • 9
4
votes
1 answer

How to overwrite some bytes in a file?

I've created a file using java.io.File, FileInputStream & FileOutputStream. Suppose that I want to change the value of some bytes in the file (for instance from the byte 15 to 35) without changing the size of the file. I've tried creating a…
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
4
votes
1 answer

Java - get line from Random access file based on offsets

I have a very large (11GB) .json file (yeah, whoever thought that a great idea?) that I need to sample (read k random lines). I'm not very savvy in Java file IO but I have, of course, found this post: How to get a random line of a text file in…
User1291
  • 7,664
  • 8
  • 51
  • 108
4
votes
1 answer

Can multiple RandomAccessFile objects write data to same file?

public class WriteThread extends Thread{ @Override public void run() { RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "rwd"); randomAccessFile.seek(threadPosition); byte[]…
Jack
  • 43
  • 5
4
votes
5 answers

Who is better in performance filechannel or RandomAccessFile for reading and writing?

I recently came across FileChannel, I am a big fan of RandomAccessFile. But I am wondering why would I pick FileChannel over RandomAccessFile for reading from a file and writing that content to another. Is there any specific performance reason? I…
fscore
  • 2,567
  • 7
  • 40
  • 74
4
votes
1 answer

How do I fetch specific bytes from a file knowing the offset and length?

I have a file, and the first 4 bytes of the file are the magic such as LOL . How would I be able to get this data? I imagined it would be like: byte[] magic = new byte[4]; RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.read(magic, 0,…
user3530525
  • 691
  • 3
  • 8
  • 20
4
votes
2 answers

Search for a String (as an byte[]) in a binary stream

Hi Team, I am trying to find a String "Henry" in a binary file and change the String to a different string. FYI the file is the output of serialisation of an object. Original Question here I am new to searching bytes and imagined this code would…
4
votes
1 answer

Read Random Access File in C#

Does anyone know if it is possible to read random access files in C#? I am trying to replicate the following function (from an old VB6 application) in C# - Open File For Random Shared As #100 Len = Len(Record) Get #100, DM, Record Close…
user1948635
  • 1,357
  • 4
  • 15
  • 22
4
votes
2 answers

Writing out using a Bytebuffer returns differently from simple write out

I'm trying to write byte data to a file and part of optimizing it, I want to reduce the number of times I write out to the file. Currently, I'm using: try (RandomAccessFile out = new RandomAccessFile(file, "rw")) { for(int i = 0; i < totalPkts;…
Aboutblank
  • 697
  • 3
  • 14
  • 31
3
votes
2 answers

Upgrading VB6 method to read Random-access file to VB.net, Unable to read beyond the end of the stream Exception for last record

I am recreating an older VB6 program that reads random-access files created using another VB6 program in VB.net. Backwards compatibility is essential for the new VB.net program. There are thousands of files that have been written that need to be…
VeryColdAir
  • 107
  • 2
  • 8
3
votes
2 answers

How mappingsize or limit affects MappedByteBuffer performance?

I am working with large files and I am using MappedByteBuffer to read and write operations. I have a little lack of knowledge so I am wondering somethings about it. MappedByteBuffer buf = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, offset,…
cgrgcn
  • 361
  • 2
  • 6
  • 24
3
votes
1 answer

Efficient way to read specific lines in a file other than Random Access File - Java

I want to efficiently read specific lines of a file in Java. As of now, I have been using the method public int read(byte[] b) from the class Random access file. Its fast, but is there any other technique which is much faster. The file has around…
faiz fz
  • 41
  • 6
3
votes
1 answer

JAVA using RandomAccessFile after seek is very slow. What is the reason?

This is my test code long fileSize = 1024 * 1024 * 512L; byte[] bts = new byte[8]; RandomAccessFile randomAccessFile = new RandomAccessFile("f:/test.data", "rw"); randomAccessFile.setLength(fileSize); randomAccessFile.seek(0); long time =…
liwei2633
  • 61
  • 7
3
votes
2 answers

What is stdin and how is it being used with fscanf?

I dont understand the connection between stdin and fscanf struct musteri{ int no; char name[40]; char surname[25]; double arrear; }; int main() { struct musteri hesapBilgi={0,"","",0.0}; FILE *ptr; …
Emrah
  • 67
  • 1
  • 1
  • 6
3
votes
2 answers

BTrees and Disk Persistance

For some time i am working on creating index for very large data sets (around 190 million). I have a BTree which can insert data sets (typically an object)/search for key and while i searched how to persist the data into files in disk, i came across…
Anandan
  • 353
  • 3
  • 17
1
2
3
23 24