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

RandomAccessFile.setLength much slower on Java 10 (Centos)

The following code public class Main { public static void main(String[] args) throws IOException { File tmp = File.createTempFile("deleteme", "dat"); tmp.deleteOnExit(); RandomAccessFile raf = new RandomAccessFile(tmp,…
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
19
votes
5 answers

RandomAccessFile with support beyond Long?

I'm currently using an instance of RandomAccessFile to manage some in-memory data, but the size of my RandomAccessFile instance is beyond 2^64 bytes, so I cannot used methods such as seek() and write() because they use Long and cannot manage an…
Ahmad
  • 12,886
  • 30
  • 93
  • 146
19
votes
2 answers

Concurrency of RandomAccessFile in Java

I am creating a RandomAccessFile object to write to a file (on SSD) by multiple threads. Each thread tries to write a direct byte buffer at a specific position within the file and I ensure that the position at which a thread writes won't overlap…
user1715122
  • 947
  • 1
  • 11
  • 26
16
votes
5 answers

Java - how to efficiently write a sequential file with occassional holes in it

I have a requirement to write records to a file where the data is written at a file location (i.e, seek position) depending on the value of a numeric key. For example, if the key is 100, I might write at position 400. The records consist of the…
rghome
  • 8,529
  • 8
  • 43
  • 62
12
votes
2 answers

How can I write to a specific line number in a txt file in Java

I'm currently writing my project for school in which requires me to read and write to txt files. I can read them correctly but I can only write to them at the end from an appended FileWriter. I would like to be able to overwrite things in my txt…
Elliot Lewis
  • 189
  • 1
  • 2
  • 14
11
votes
1 answer

How to get random access to a file on SD-Card by means of API presented for Lollipop?

My application uses Java class RandomAccessFile to read/write bytes to a file on SD card randomly by means of realization of SeekableByteChannel interface. Now I need rewrite it for Android 5.0 with new Lollipop API. I have found the only way to…
10
votes
2 answers

Does RandomAccessFile in java read entire file in memory?

I need to read last n lines from a large file (say 2GB). The file is UTF-8 encoded. Would like to know the most efficient way of doing it. Read about RandomAccessFile in java, but does the seek() method , read the entire file in memory. It uses…
Vinod Jayachandran
  • 3,726
  • 8
  • 51
  • 88
9
votes
4 answers

How to properly close MappedByteBuffer?

This is the code I'm running: import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class Main { public static void main(String[] args) throws Exception { String filePath =…
ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
7
votes
2 answers

Reading data from RandomAccessFile producing incorrect results - Java

I have a text file with 42 lines. Each line has more than 22,000 numbers separated by comma. I wanted to extract certain numbers from each line, and I have an int array with a length of 1000 containing a 1,000 numbers that I need from each of…
6
votes
1 answer

Java RandomAccessFile truncate from start

I know how to truncate a RandomAccess file so that bytes at the end are removed. raf.getChannel().truncate(file.length() - 4); or raf.setLength(file.length() - 4); But how to truncate a RandomAccessFile in such a way that bytes at the start is…
retromuz
  • 809
  • 9
  • 26
5
votes
2 answers

How does RandomAccessFile.seek() work?

As per the API, these are the facts: The seek(long bytePosition) method simply put, moves the pointer to the position specified with the bytePosition parameter. When the bytePosition is greater than the file length, the file length does not…
SirVirgin
  • 153
  • 1
  • 3
  • 10
5
votes
1 answer

Read scattered data from multiple files in java

I'm working on a reader/writer for DNG/TIFF files. As there are several options to work with files in general (FileInputStream, FileChannel, RandomAccessFile), I'm wondering which strategy would fit my needs. A DNG/TIFF file is a composition…
5
votes
4 answers

DocumentFile RandomAccessFile

Is there any way get a RandomAccessFile from a given DocumentFile? I know it is possible to get an InputStream via getUri InputStream inputStream = getContentResolver().openInputStream(DocumentFile.getUri()); But I need a RandomAccessFile Thanks…
JensSommer
  • 51
  • 2
5
votes
1 answer

IndexOutofBounds when using Java's read bytes

I am using a RandomAccessFile in Java 6 but having some strange behavior when reading bytes. With the following code, where offset and data are appropriately initialized: int offset; byte data[]; randFile.readFully(data, offset, data.length); I get…
jaynp
  • 3,275
  • 4
  • 30
  • 43
4
votes
0 answers

Is there a way to get a RandomAccessFile from a Storage Access Framework document?

I am trying to open a file for writing via SAF (Storage Access Framework). However I need the returned document/file to be written in random-access manner. SAF returns me an URI. I can get an OutputStream from that URI via…
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
1
2 3
23 24