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

seek method RandomAccessFile

I have an assignment where I need to display 200 random characters and then ask the use what letter they would like to replace and then replace all those letters. I have the random characters generated, but I am having trouble with replacing the…
3
votes
1 answer

How can we create a folder and file using RandomAccessFile in a single statement?

When we write RandomAccessFile r =new RandomAccessFile("file.txt","rw"); how can I create this file in a folder named home on the desktop.. Again it should be a single statement!
sedflix
  • 226
  • 4
  • 13
3
votes
2 answers

Getting byte offset of line in a text file?

I have a text file like one two three four five I need to get the offset of each line in the file. How do I do this in Java? I have searched through some of the I/O libraries(like BufferedReader and RandomAccessFile) but I'm unable to find a…
user3044327
  • 193
  • 1
  • 3
  • 11
3
votes
1 answer

C Overwrite existing record in file

I'm writing a program that has a struct for bank clients (BankAccount) having 3 elements - account number, name of client, and bank balance. I have a file with records already in it, and I have a function to modify the balance. However, I am unable…
mmahmad
  • 33
  • 6
3
votes
4 answers

Java Random File Access: Get byte offset of line start

I need to randomly access specific records in a text (ASCII) file and then read from there until a specific "stop sequence" (record delimiter) is found. The file contains multi-line records and each record is separated by the delimiter. Each record…
beginner_
  • 7,230
  • 18
  • 70
  • 127
3
votes
3 answers

data in txt file overwritten

the program is supposed to create a number of blank records to a .txt file, which can be edited later to add infos like name and balance..etc. my problem is whenever i put another record, the balance from the first account disappears (but the name…
3
votes
3 answers

File backed Java map

Is there a simple way of having a file backed Map? The contents of the map are updated regularly, with some being deleted, as well as some being added. To keep the data that is in the map safe, persistence is needed. I understand a database would be…
Lawrence Andrews
  • 440
  • 4
  • 12
3
votes
3 answers

Reading a log file which gets rolled over

I am trying to use a simple program to read from a log file. The code used is as follows: RandomAccessFile in = new RandomAccessFile("/home/hduser/Documents/Sample.txt", "r"); String line; while(true) { if((line = in.readLine()) != null)…
user2384376
  • 35
  • 1
  • 5
3
votes
1 answer

Android: Accessing File from Internal Storage Using RandomAccessFile

I am creating an app that needs to read data from a file. I was initially reading it from the assets folder using a BufferedReader and an InputStreamReader but I was running into memory issues (see Android: File Reading - OutOfMemory Issue). One…
lord_sneed
  • 794
  • 3
  • 12
  • 25
2
votes
0 answers

Cannot delete temporary file after writing to it via a MappedByteBuffer

When I run this JUnit test, it fails because file.delete() returns false: @Test public void testDeleteTempFile() throws Exception { File file = Files.createTempFile("_file_del_test", ".txt").toFile(); try (RandomAccessFile f = new…
Michael
  • 4,722
  • 6
  • 37
  • 58
2
votes
2 answers

Write big files using RandomAccessFile class

I need to copy big files (GBs) into another file (the container), and I was wondering about performance and ram use. Reading the entire source file like the following: RandomAccessFile f = new RandomAccessFile(origin, "r"); originalBytes = new…
navy1978
  • 1,411
  • 1
  • 15
  • 35
2
votes
1 answer

read until specific index in a file with RandomAccessFile java

I was trying to read from a file between two specific indices using the RandomAccessFile. I know I can jump to an index with the seek() function, but i couldn't find the answer how to read then text out of the file until a specific index. For…
2
votes
1 answer

Why does RandomAccessFile read  as firt character in my UTF-8 text file?

A question on reading text files in Java. I have a text file saved with UTF-8 encoding with only the content: Hello. World. Now I am using a RandomAccessFile to read this class. But for some reason, there seems to be an "invisible" character at the…
DanielBK
  • 892
  • 8
  • 23
2
votes
2 answers

RandomAccessFile writeInt(int i) vs write(byte[] b) - performance

I've faced an interesting thing today regarding RandomAccessFile. I've noticed that using RandomAccessFile's writeInt(int i) method is much more slower than using RandomAccessFile's write(byte[] b) where I first convert int value to byte[4] array.…
Kristoff
  • 326
  • 2
  • 13
2
votes
0 answers

How to perform seek in a Dataoutput Stream?

I am using a dataoutput/datainputStream to make a binary Tree over a file,reading a 50000 lines .csv file to build it. Initially i thought to achieve this with the RandomAccesFile class, but it has serious permormance issues (it's very slow on…
1 2
3
23 24