Teaching Random Access Files! - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Java Programming (https://www.supportforums.net/forumdisplay.php?fid=22) +---- Thread: Teaching Random Access Files! (/showthread.php?tid=5089) |
Teaching Random Access Files! - Project Evolution - 02-27-2010 This concept of I/O using Random Access File is relatively new to a lot of people and im not sure as to why, so I thought my next tutorial will be on Random Access Files (RAF). As you may or may not know, the usual way we go about I/O streams such as reading/writting data is sequential. It reads/writes one at a time, which is inefficient when trying to locate certain data quickly. Also, they do not provide a means of updating data without copying the entire files contents and updating. This can all be optimized using RAF. The RandomAccessFile class can be found here, http://java.sun.com/j2se/1.4.2/docs/api/java/io/RandomAccessFile.html Here is a quick example of a program that first writes to a specified txt file using a FileOutputStream wrapped by a DataOutputStream. After a successful write, we use RAF to read and write to the txt file. Some parts of this snippet may confuse some, but dont be afraid to ask. Code: import java.io.DataOutputStream; Thanks for reading. |