RandomAccessFile 1 « API « Java I/O Q&A





1. random access file in java    stackoverflow.com

I have the following fields:

  • Inventory control (16 byte record)
    • Product ID code (int – 4 bytes)
    • Quantity in stock (int – 4 bytes)
    • Price (double – 8 bytes)
How do I create a fixed length ...

2. Java RandomAccessFile    stackoverflow.com

    public void exportUrlsToFile(String file, String urls) throws IOException {
 String[] urlsArray = urls.split("\\s+");// split on one or more white space characters.

 // create a fresh file
 RandomAccessFile ...

3. Clean a RandomAccessFile    stackoverflow.com

I have a code:

for (int i = 1; i <= Math.ceil(n/m); i++){
  RandomAccessFile file_1 = new RandomAccessFile(".\\src\\dataFiles\\fileC_" + i + ".dat", "r");
  RandomAccessFile index_1 = new RandomAccessFile(".\\src\\dataFiles\\indexC_" + i ...

4. Java RandomAccessFile - dealing with different newline styles?    stackoverflow.com

I'm trying to seek through a RandomAccessFile, and as part of an algorithm I have to read a line, and then seek backwards from the end of the line E.g

String line = ...

5. deleting random access file in java    stackoverflow.com

I've created a random access file as follows:

 RandomAccessFile aFile = null;
 aFile = new RandomAccessFile(NetSimView.filename, "rwd");
I want to delete the file "afile". can anyone suggest me how to do it? ...

6. Delete The First Bytes of a Random Access File in Java    stackoverflow.com

I'm writing bytes to a random access file. After completing the operation, I want to delete the first 100 bytes from the file. How can I achieve this? Thanks in advance. ...

7. RandomAccessFile readInt    stackoverflow.com

how to read numbers from a file??? when i use readInt method i get big number, and it not equals number from a file. how to fix it??? Scanner is not good idea, becouse ...

8. java.io.RandomAccessFile Invalid Argument for large files on mac    stackoverflow.com

I'm running a program which needs random access to a very large file (approximately 151 gigabytes). I've found that whenever it runs on one of the MacPro machines in the ...

9. Is it possible to set a speicifc byte in a RandomAccessFile back to null?    stackoverflow.com

Origianlly, when I created the RandomAccessFile, I skipped over some bytes so those stayed as null, but now I need to delete some of the bytes I wrote. Right now, what ...





10. Closing a RandomAccessFile sometimes takes exactly 45 seconds    stackoverflow.com

In my program, closing a java.util.RandomAccessFile sometimes takes exactly 45 seconds (well, almost exactly: between 44.998 and 45.003 seconds). The program creates and closes lots of small files. Usually closing the ...

11. RandomAccessFile over HTTP    stackoverflow.com

It's easy to open a connection to a remote asset using URL.openStream() which returns an InputStream. What about random access?

12. RandomAccessFile read/write start-finish position problem    stackoverflow.com

I've searched this a lot but still can't find... So I hope you can help me to understand :) The question is... I have big file A.dat and I want

  • A) Read it ...

13. How do I delete a record from my random access file?    stackoverflow.com

I was wondering how I could delete a record from my random access file. Here's how I add to my RAF, not sure how to delete it though :X

public void addNewStudent(String name, ...

14. RandomAccessFile cannot be found by compiler after being declared    stackoverflow.com

The code below produces the following error when I try to compile it: cannot find symbol
symbol : variable airplanesFile
The error is produced by the last statement. Why can the RandomAccessFile object not ...

15. Why no readUnsignedInt in RandomAccessFile class?    stackoverflow.com

I just found there is no readUnsignedInt() method in the RandomAccessFile class. Why? Is there any workaround to read an unsigned int out from the file? Edit: I want to read an unsigned ...

16. How to save string with code page 1250 into RandomAccessFile in java    stackoverflow.com

I have text file with string which code page is 1250. I want to save text into RandomAccessFile. When I read bytes from RandomAccessFile I get string with different character. Some ...





17. Question about RandomAccessFile in Java?    stackoverflow.com

I am reading some Java text about RandomAccessFile, in the text, the author says:

RandomAccessFile(File file, String mode)
  • Mode “râ€? to open the file for reading only Mode “rwâ€? to open for ...

18. Are RandomAccessFile writes asynchronous?    stackoverflow.com

Looking at the constructor of RandomAccessFile for the mode it says 'rws' The file is opened for reading and writing. Every change of the file's content or metadata must ...

19. input/output stream on random access file, does close propagate?    stackoverflow.com

I am creating file input streams/file out streams from a random access file via the following API:

RandomAccessFile.getFD();
FileInputStream(FileDescriptor)
FileOutputStream(FileDescriptor)
When I close the file input streams/file out streams, will the random access file also be closed? Bye ...

20. How to choose RandomAccessFile dynamically?    stackoverflow.com

I had written a program using RandomAccessFile class to read binary data. The code is as follows

RandomAccessFile in = new RandomAccessFile('BOT.GRD', "r");
in.read(a);
Now I want to choose file dynamically rather than ...

21. In java is there a readObject() alternative for RandomAccessFile    stackoverflow.com

I'm trying to save a couple of Strings but I'm currently using ObjectInputStream, which apparently doesn't save my data in a permanent manner. Here is the code which I commented in ...

22. Opening and Closing a File in RandomAccessFile    stackoverflow.com

I have a program which has to create an empty file with a specific name, write output to it and then close it (not delete it). If the program is run ...

23. how to Lock the File using RandomAccessFile    coderanch.com

I am using the RandomAccessFile since there are 2 procress . one process will write in a.txt file and 2nd process will read from a.txt simultanously. i found that data get lost so i would like to set the lock on file that is while reading and writing the file.But locking faclilty is in reader and writer class so whether there ...

24. RandomAccessFile    coderanch.com

I don't think this is a correct reason. A URL may not necessarily use HTTP. The reason you cannot have random access to a URL is: There is no concept of a "File Pointer" that you can move around in a URL. All you have is a stream of data. You do not have a "chunk" of data. Furthermore, you can ...

25. RandomAccessFile.seek()    coderanch.com

26. RandomAccessFile(urgent)    coderanch.com

Hi It depends on what exactly you are trying to do! If you simply wants to read contents of a file, you can use RandomAccessFile no need to use any other classes along with this. You can read any data type with this class. It has wide range of read methods. HtH SivaPrasad

27. RandomAccessFile    coderanch.com

28. Random Access File - Overwrite Contents    coderanch.com

hi friends, Thanx friends,let me make my requirement very clear, i am running through the file until the exact book code is found, then i want to overwrite the bookName public void modifyBookDetails(String str){ try{ String bookCode="B0000002"; String bookName= "MasteringJSP"; File f=new File("F:\\javapro\\BookMaster2.txt"); RandomAccessFile raf=new RandomAccessFile(f,"rw"); String line=raf.readLine(); while( line != null){ line=raf.readLine(); if (line != null){ String s=line.substring(0,8); if (s.equals(bookCode)){ ...

29. RandomAccessFile    coderanch.com

Although we can seek N bytes in a RandomAccessFile and read/write at the N+1 bytes, can we also do the same with lines? Can we skip the N lines and read/write directly at the (N+1)th line? I know we can use the readLine() method and keep a count of how many lines we have read, uptill the point count reaches N. ...

30. why use RandomAccessFile    coderanch.com

31. RandomAccessFile - overwrite or insert?    coderanch.com

Hi, I would be grateful if someone who has done this sort of work could clarify something for me. I want to use RandomAccessFile to insert new data values into an existing file. If I use something like raf.writeShort() can you tell me whether it will overwrite the existing data at the current byte position, or will it insert new data ...

32. Restart in a RandomAccessFile    coderanch.com

I've been looking at the posts in the I/O forum and think I've got the basics of an answer, but wanted to walk this thru with someone. I have to take an existing program that has a property file and input file & process the input file run thru some complex logic. As I process the records in the file (fields ...

33. RandomAccessFile not fast enough?! / Wait for a stream    coderanch.com

Hello, i am developing an application which processes a file multiple times. At first I generate a TIFF Image with Java Image I/O API which works perfectly well. Then I am re-reading the file to modify some pointers and stuff and this is where my problem starts: The RandomAccessFile used for writing the file to the HD is still writing down ...

34. java io/reading from Disk file using RandomAccessFile class    coderanch.com

java io/reading from Disk file using RandomAccessFile class (I/O and Streams forum at JavaRanch) A friendly place for programming greenhorns! Register / Login Java Forums Java I/O and Streams java io/reading from Disk file using RandomAccessFile class Post by: C.R.Muthukumar , Ranch Hand on Dec 03, 2004 03:09:00 import java.awt.*; import javax.swing.*; import java.io.*; import java.util.*; import java.awt.event.*; import ...

35. Help In RandomAccessFile    coderanch.com

Hello javaranch members. I have a problem with java.io import java.io.*; class TestRandom { public static void main(String[] args) { String s ="1234567"; String s1="HelloWeee"; String re=null; try{ RandomAccessFile f = new RandomAccessFile("file.dat","rw"); System.out.println("File Pointer "+f.getFilePointer()); re=f.readLine(); while(re!=null) { System.out.println("File Pointer "+f.getFilePointer()); if(re.equals(s)) { System.out.println("After read match lin file pointer" +f.getFilePointer()); f.seek((f.getFilePointer()-re.length()-1)); System.out.println("REpositioned file pointer" +f.getFilePointer()); b = s1.trim().getBytes(); f.write(b); System.out.println("AFterwrite ...

36. RandomAccessFile not erasing contents of old file    coderanch.com

I am creating a "testfile" using RandomAccessFile like this: RandomAccessFile raf = new RandomAccessFile("testfile", "rw"); raf.writeInt(2); raf.writeChar('f'); raf.seek(0); System.out.println(raf.readInt()); System.out.println(raf.readChar()); raf.close(); When I ran first time, it created the file with 2 and f and printed properly. Next time, I commented line no. 3 (i.e no writeChar('f')) and ran the program again and again it printed 2 and f. I looked ...

37. Problem with RandomAccessFile    coderanch.com

Because an ObjectOutputStream writes a header, too. Take a look at the size of the file that's been written - it's bigger than just the 4 bytes that writeInt() would give you. You really don't want to read this with anything other than an ObjectInputStream. Or in this case, if you're not going to use writeObject() and readObject() at some point, ...

38. what's the different between random access file and sequential access file?    coderanch.com

Sequential file access allows data to be read from a file or written to a file from beginning to end. It is not possible to read data starting in the middle of the file, nor is it possible to write data to the file starting in the middle using sequential methods. The input and output streams in Java are sequential. A ...

39. mode for the RandomAccessFile    coderanch.com

40. Does RandomAccessFile need a bufffer class?    coderanch.com

In the case of reading from and writing to a text file, it is helpful to use a buffer class, so that the program stores up the data in a buffer before reading and writing, and does not have to open up the file on the hard disk every time a few characters are added to the file. How about the ...

41. About RandomAccessFile    coderanch.com

Is there some ThreadLocal variables in RandomAccessFile, because following won't do nothing: final RandomAccessFile raf = new RandomAccessFile(f, "rws"); Thread t = new Thread() { public void run() { try { raf.write("test".getBytes("ascii")); } catch(Throwable t) { t.printStackTrace(); } } }; raf.close(); .. but if i do write in Thread, which creates RandomAccessFile instance, then it works.. I don't get any exceptions, ...

42. Random Access File Question    coderanch.com

hi all ,i was wondering that if i use a RandomAccessFile to save data, should i save empty objects (say 100,000 object) and then for every object that i need to insert ,i seek it's position and then modify the value of the empty object. that i have read in a book (Java2 how to program) is there another way to ...

43. RandomAccessFile and the lost space    coderanch.com

Ah ha, I see what you mean. I suppose you would have to create the file using dummy entries, yes, as Cay suggests. I think whether this is viable would therefore rely on how large the file would be (and hence how long it would take to create) and also how often you would need to do this (i.e. is the ...

44. RandomAccessFile    coderanch.com

Ok, im very new at this, and i've hit a road block. In a RAF i am holding data such as Name, address, DoB, phone number... etc. I have worked a way of navigating and adding the data into the file. The method of adding is shown below. Now i've come to a point where i want to ammend or change ...

45. RandomAccessFile problem during modification    coderanch.com

There's no operation for replacing different-sized data in a RandomAccessFile, therefore you have to do it yourself. If the data you are replacing is a different length to the new data, you must move the rest of the file contents appropriately. If the file is known to be small, this is pretty easy. If it could be big, you need to ...

46. RandomAccessFile Help    coderanch.com

I have a text file with a path of another file on each line. I have a Java code that reads this file searches for the file on the particular line and takes it's contents. But in case of an error, it writes the line number to a temporary file so i can start exactly where i left. My problem is ...

47. RandomAccessFile    coderanch.com

Hello everyone! I have the following problem I'm trying to solve: I have a .txt file of data that needs to be read to a mySQL database. The way the data is structured, some "fields" are blank and others are filled with data. I figured a Random Access File would be ideal for this situation. I have never used this class ...

48. RandomAccessFile()    coderanch.com

Ok fellas, come a long way and i have learned a ton. The two files that I am putting together one after another in the new file is working but...the second file contains several lines at the begining that are contained at the end of the first file so i have duplicate records in the middle of the new file!! Im ...

49. random access file    coderanch.com

Hi, I have a program that writes to and reads from a random access file. That is, it's trying to... The program loops around (it has a menu that runs in a loop until user chooses to quit), and one of the operations is to read from the file what is in there. The file contains records of an ADT I ...

50. Deleting from a RandomAccessFile    coderanch.com

Hi, I have a program that writes fixed length records to a RandomAccessFile using a hashing algorithm on a key field. This works fine, but I'm unsure how best to implement deletion of records. People have suggested different ways to deal with this, but I'm unsure which one is best, or if there is an even better method. What are the ...

51. Creating Random Access File    coderanch.com

It is a matter of scope and visibility. The variables you declared in your constructor are not visible to other code, and they become eligible for garbage collection when the constructor exits. Move them to member variables to make them visible to other methods. Here's one way to change things: class TestRaf // cap first letter, not all letters { BufferedReader ...

52. RandomAccessFile: backspacing and inserting    coderanch.com

Yes, unfortunately RAF really isn't good for this sort of thing. If you want to replace one region with another string with a different length, you need to shift all the subsequent bytes in the file to accommodate the new length. There's no simple method call to do that, and it's a fairly ineffecient process - especially if you still plan ...

53. problem with random access file    coderanch.com

I have to append some content to each line in a text file. But when i use random access file and write contents to the file.. the content in next are getting over writed. Is it not possible to write in the middle of a file using random access file without getting the contents next to it overwritten> i have a ...

54. using a RandomAccessFile    coderanch.com

55. Getting filename of open file (RandomAccessFile)    coderanch.com

Hi, I'm sure that this has been covered somewhere, but I've been unable to find it. What I want to be able to do is create a list of RandomAccessFiles (I'm using them for doing binary searches on sorted, fixed record length, text files). But since I will have a list of them, I'm trying to figure out how to get ...

56. RandomAccessFile : What is rws and rwd mode?    coderanch.com

As explained in the text the changes updated to the physical file before the I/O call returns if the file resides on the local device. The difference between the two modes is that "rws" will write both changes to file contents and file metadata while "rwd" will update only on changes to file contents.

58. RandomAccessFile Modify Help    coderanch.com

Hi all. I have used the following code to create the file. It works. import java.io.*; public class RandAccessFile{ public static void main(String[] args) throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter File name : "); String str = in.readLine(); File file = new File(str); if(!file.exists()) { file.createNewFile(); file.canRead(); file.canWrite(); // System.exit(0); } try{ //Open the file for both reading ...

59. RandomAccessFile help    coderanch.com

I am working on a program and am 95% done, but am stuck. My program creates a file and the user can file it with account ID's and balances. The issue I am having is that when a user enters a duplicate account ID it needs to throw an error and I am thinking after the error it should then ask ...

60. RandomAccessFile and a big file read/write problem    coderanch.com

I still don't see the code I asked for. Anyway, if you need to write a 67MB file into 4MB chunks, that should require 16 files of 4MB each and one file with the remaining 3MB. I get a feeling that you're forgetting that last file. 17.3MB gives you 4 files of 4MB which merged together gives you the 16MB from ...

61. RandomAccessFile    coderanch.com

Well, it would depend if your file was plain ASCII text or Unicode characters, but the basic idea is the same. Open up the file in read or read write mode, seek() some number of bytes (there is no fseek) then read the three bytes or three characters you need. Where are you running into a problem?

62. How to update randomaccessfile    coderanch.com

I'm studying Java, I wrote a Address book for practice. Actually I follow a sample and finally finish. It work fine. It has Add new, Move Next, Move Last, Move First, Clear Buttons. I need to add UPDATE Button. I already Add that button. How can I do the coding? I don't know how to play with " seek(position)" Can anybody ...

63. Question about RandomAccessFile and MappedByteBuffer    java-forums.org

About RandomAccessFile's pointer. Lets say I have 5 int values in a text file. I want to the 2nd int in my java program. Then I jump 4 bytes forward (x.seek (4))(the pointer start at 0), because a int is equal to 4 byte. If its a long value, I jump 8 byte forward, and so on for the rest of ...

64. Random Access File creator errors    forums.oracle.com

My main question is is it possible to create and use a Random Access File as part of a class, but outside of any of the classes methods? I have written my code that way, but when I compile, I get an error about the syntax. Below is the code and error message:

65. Problem with RandomAccessFile    forums.oracle.com

I am assuming dos.txt is a normal text file, containing some lines of text. Have you tried readLine() method of RandomAccessFile ? It returns a String, so you can read each line using a loop and append the read string to the string in which you want to store your file's contents. For checking if "str" is contained in the file, ...

66. How To Using A Random Access File    forums.oracle.com

68. HELP!Random access file    forums.oracle.com

69. RandomAccessFile help    forums.oracle.com

seek(1) will set the file-pointer offset to 1 past the beginning of the file. writeUTF ("test") will output output six bytes. So won't seek(1) go to to 1 past the beginning write 6 bytes and if you then seek(1) and readUTF() you will read 6 bytes "test". If you do it again for seek(2) writeUTF("test") it will write 6 more bytes, ...

70. RandomAccessFile readWrite    forums.oracle.com

In a text file I have: 56 Jones 33 64 59 barry 66 367 34 scott 88 100 etc I want to remove a line based on the user input - barry removes the whole line - 59 barry 66 367 the pointer is set at 0 How do I find the line and then remove it and write back to ...

71. Using RandomAccessFile    forums.oracle.com

72. random access file    forums.oracle.com

73. RandomAccessFile problem    forums.oracle.com

Hard to say what is happening. It is even hard to say where the problem might originate: it might be a code problem, but more likely it is a configuration problem in the environment (OS) that Java is running in. One thing you can try is using a profiler to seek slow parts in the application. Perhaps that will help you ...

74. RandomAccessFile help please,thank you    forums.oracle.com

two bytes are written to the file, starting at the current file pointer, as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the modified UTF-8 encoding for each ...

75. Random Access File    forums.oracle.com

here is what is listed as what i'm supposed to do with this portion, i may be reading wrong as well you will write a separate class file called SeekFile.java that access the file as a Random Access File. You will index the position of each of the lines and their PMIDs, and create a hash of that data. Again, you ...

76. Working with RandomAccessFile    forums.oracle.com

Hi everyone, I am in great trouble. I have got an assigment for school and i am not getting any idea how to do it. I would like to manipulate records file so that to be able to add,delete,modify and search. It says: 1). Adding data to an instace of Random Access File class by direct manipulation of the file pointer ...

77. Binary IO in RandomAccessFile super slow ?    forums.oracle.com

You should carefully read the API documentation is the problem here. The issue is what s does in that constructor. And what it does is cause the program to halt and wait for the changes to be flushed to disk fully before continuing. Which means it is extremely slow. I reworked your code into two tests (each wrote 20,000 floats) but ...

79. Dealing with RandomAccessFile, Questions?    forums.oracle.com

80. RandomAccessFile seek question    forums.oracle.com

What in Pattern or Matcher allows you to take a Reader as source and then get the index of the position of a match. You would have to load the whole of the file into memory and work with that which would seem to defeat the point of working with a RandomAccessFile.

81. Java i/o performance - RandomAccessFile    forums.oracle.com

I want to improve the RandomAccessFile.write(byte[],int,int) .I don't really know how is implemented by the java sun and how to implement myself to get better performances. All i know(i think i know)is that the java implementation calls the function writeBytes(byte[],int,int) so is somethink like this: public void write(byte[] b, int off, int len){ writeBytes(b,off,len); } I know this because I'm doing ...

82. Random Access File    forums.oracle.com

Thanks Hunter, I will play around with skpBytes(). Dr, honestly, I really dont want to use random access file. My problem is, i have one big file( > GB), and I was trying to use arrays for that ( if you could recall :)). Then I moved to manupulation with files. The procedure is: big file chunked into number of splits ...

83. Problem with RandomAccessFile    forums.oracle.com

calypso wrote: Im not sure what "raf" is but why not just use FileWriter and FileReader, check this example out below, it uses string but as im sue you know you can convert strings to ints/floats and visa versa.. So now you're advocating that newbies: * use non-standard naming convention: "class fileReaderWriter" * write to file without buffering * close the ...

84. RandomAccessFile and Cp850    forums.oracle.com

85. How does a RandomAccessFile work?    forums.oracle.com

Hi everybody, I've read the RandomAccessFile API, and I've never used it before, so there are a few things about it I don't understand, and I'd like to make sure I do before I risk overwriting important bytes or something like that on my computer. So, here are my questions, if anyone would mind answering them. 1. How exactly does the ...

86. Random Access File    forums.oracle.com

87. Using RandomAccessFile class...    forums.oracle.com

I want to include the following strings "002, Simon Marshall" and "001, Jackson" into the file and write them on separate lines, instead this is the following output when I write to the file 001, James Smith 002, Stephanie Macintosh 004, Stephanie Macintosh 001, Jane Jones 002, Katrina Martin 002, John Lewis 001, Brenda Jackson 002, Brenda Jackson 002, Lucy Hill002, ...

88. Read/write On RandomAccessFile ....    forums.oracle.com

hi i am using a Random Access File as a data storage ... Thus i need to do read and write as well as the delete operations simultaneously... when using synchronized methods no problems at all since only one thread will access at a time ... but when multiple threads will access without synchronized blocks then all will use the same ...

89. random access file scenario: stuck on how it works?    forums.oracle.com

Index File: Assume the hash table will be saved on file when processing is complete and will be entirely restored into memory when processing begins again. Don't use a Random Access File for this. Now what I'm having trouble with is this idea of writing an index file and a data file. So far i've implemented a hash table, which is ...

90. RandomAccessFile    forums.oracle.com

91. Using RandomAccessFile    forums.oracle.com

92. randomaccessfile or bufferedreader    forums.oracle.com

How long does that loop take as opposed to that loop plus your processing? IOW have you established where the bottleneck actually is? - in that loop or in your processing code? I am guessing the latter, and I am further guessing that a judicious use of StringBuffer or StringBuilder will fix it.

93. File pointer for RandomAccessFile    forums.oracle.com

94. usage of RandomAccessFile.    forums.oracle.com

95. Deleting part of Random Access File in Java    forums.oracle.com

Hey; I am making a program that asks the user all sorts of things about their friends and then stores it in a Random Access File. Then I need to add a delete function that asks for a friend to be deleted and for this I need to know how do I delete an entry if I know where does the ...

96. RandomAccessFile VS File    forums.oracle.com

97. RandomAccessFile addtion    forums.oracle.com

Hello all, I have a RandomAccessFile of integers. I learnt that when I add a particular integer at a particular position, the number existing at that particular position is being over written with the new one. Isnt there any way to just add the new interger at the position specified without deleting the existing one? Any help would be greatly appreciated ...

98. RandomAccessFile    forums.oracle.com

99. RandomAccessFile question    forums.oracle.com

Yes, sort of. Or not. In both of them what happens is that bytes are transferred from a piece of hardware (a disk drive) into your program's memory. Or vice versa. At the lowest level there isn't going to be much difference in which tool you use to do that byte-moving. However in sequential reading and writing you may have the ...

100. ByteInputStream and RandomAccessFile    forums.oracle.com

my objective is write and read the object to a file using RandomAccessFile concept i can only write the object to a file but i can't read it back please help me... this file(logInfo.log) contain collection of object i want to read all object and my code is private static File file = null; static RandomAccessFile raf = null; . . ...