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
2
votes
1 answer

Fastest way to read a line in file

I am using RandomAccessFile to read some informations from a large file. RandomAccessFile has a method seek that points the cursor to a specific part of the file that I want to read the whole line. To read this line I use readLine() method. I read…
Marcelo Machado
  • 1,179
  • 2
  • 13
  • 33
2
votes
3 answers

Reading a single UTF-8 character with RandomAccessFile

I've set up a sequential scanner, where a RandomAccessFile pointing to my file is able to read a single character, via the below method: public char nextChar() { try { seekPointer++; int i = source.read(); return i > -1 ?…
user5549921
2
votes
0 answers

RandomAccessFile - Unwritten bytes

I use java.io.RandomAccessFile to open a file and write few entries in the same at random locations. When I read the file back, is it guaranteed that unwritten bytes in that file (that may be in between two locations which were written earlier) are…
LPD
  • 2,833
  • 2
  • 29
  • 48
2
votes
1 answer

fread into buffer is blank despite non-empty file

I'm trying to read in 256 bytes into a buffer from a 65536 byte file, treating it as a random-access file by using fopen, fread, fwrite, and fseek. I'm not getting any errors, but the buffer is still empty after the read, even though the file is…
Praxder
  • 2,315
  • 4
  • 32
  • 51
2
votes
1 answer

Java RandomAccessFile how to Read nth byte as a character

I was learning java fileoperations, and I was trying to read nth byte as character in a text file. I used RandomAccessFile (I am not sure if this is the right module to use here), and my code is import java.io.RandomAccessFile; public class…
scott
  • 1,557
  • 3
  • 15
  • 31
2
votes
3 answers

How to make a variable equal to a random line in a text file in Java?

This is a little hard to explain, but here goes nothing. I am creating a program that creates excuses for example: "I am sorry, but my dog ate my homework!" The way I'll make it random is by placing the intros "I am sorry but" in a text file on…
lokilindo
  • 682
  • 5
  • 17
2
votes
2 answers

How to read a text file from X line in Java?

How can I read a text file starting at X number line? I can start reading the file and do nothing until X line is reached but I'm wondering if theres a better way to do it.
2
votes
1 answer

RandomAccessFile Endianness issue

I have to read data from a binary file in byte, int and long data sizes. I read it with RandomAccessFile's methods readInt, readLong, readByte. The problem is that the endiannes of the system (Windows 8.1) and the endiannes of the file are…
user3719857
  • 1,083
  • 4
  • 16
  • 45
2
votes
2 answers

Write to Random Access File in Java without overwriting

In a Random Access File, at a particular byte position, the new text to be inserted is overwritten. How do I modify the write method so that the new text is inserted between the existing values without overwriting? I know it is possible to shift but…
Java Enthusiast
  • 1,161
  • 2
  • 15
  • 30
2
votes
3 answers

Reading From RandomAccessFile

I am reading 10 lines from a simple RandomAccessFile and printing each line. The exact lines in the text file are as follows one after the other: blue green bubble cheese bagel zipper candy whale monkey chart When printing them in order as I read…
Branbron
  • 101
  • 4
  • 12
2
votes
1 answer

Unexpected output with RandomAccessFile

I'm trying to learn about RandomAccessFile but after creating a test program I'm getting some bizarre output. import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; public class RandomAccessFileTest { public static…
2
votes
1 answer

Declaring a RandomAccessFile object inside a try block gives resource leak warning but declaring it outside does not?

This is my code: try { RandomAccessFile srcFile = new RandomAccessFile("src.txt", "rw"); } catch(FileNotFoundException e) { e.printStackTrace(); } This code gives me a warning that the RandomAccessFile object srcFile is never closed. But if…
Akshay Damle
  • 1,220
  • 3
  • 18
  • 31
2
votes
1 answer

Java RandomAccessFile extending file by writing with an offset

So I know you can extend the size of a RandomAccessFile by writing past its current size but I can't seem to find any info for a specific case. If I have a 10 byte file and I write 5 bytes with an offset of 15 the file gets extended to 20 bytes, but…
Cody James
  • 33
  • 4
2
votes
1 answer

Playing a changing file using android media player

is there anyway to play a file that is being dynamically added to, using android media player ? I tried using RandomAccessFile : public static RandomAccessFile tempMp3; tempMp3 = new RandomAccessFile(Environment.getExternalStorageDirectory()+"/ggg",…
Dexter
  • 1,710
  • 2
  • 17
  • 34
2
votes
2 answers

Reading specific bytes from RandomAccessFile and testing to see if they equal 0

first time poster here. Thank you in advance for viewing my question. I'm having a ton of trouble with a homework problem in which I have to read a specific range of bytes from a RandomAccessFile, then check that range of bytes to see if they all…
alex_crow
  • 27
  • 4