View Full Version : Writing a unique field to a RAF file help

02-24-2012, 05:01 AM
Hello, so I am trying to write a unique ID number to each new record I add to my random access file, but I'm having some trouble. I can't figure out a way to do this so it increases each time, instead it just has the same number written for every record. Can someone explain to me how to have the ID number increase by 1 each time I write? I'm appending to the end of the file each time if that helps.

Thank you for any help.

02-24-2012, 03:48 PM
Can you please post your existing code so we can help you correct it?

02-25-2012, 05:09 AM
public void write(boolean append){

LinkedList Ln = new LinkedList();

String book_name = bookname_tb.getText();
String delete = delete_tb.getText();

String file_path = "123abc123abc";

Library_database a_data = new Library_database(book_name, delete);

char[] char_bookName = new char[150];
char char_delete = 0;

//book name
for(int a = 0; a < char_bookName.length; a++){//as long as a is less than the length of char_bookName keep running
char_bookName [a] = ' ';//fills the array

//overrides the spaces with the book's name
for(int i = 0; i < book_name.length(); i++){//as long as i is less than the length of book_name keep running
if(i >= 149) break;//stops overflow

char_bookName[i] = book_name.charAt(i);//finds the character position in the array, so you can print individual letters

for(int i = 0; i < 1; i++){
char_delete = delete.charAt(i);

//writing stuff
try //try and catch statement for stopping errors crashing the program
{ //for writing to a file
DataOutputStream out = new DataOutputStream(//creates an object
new FileOutputStream(file_path,true));

//book name
for(int k = 0; k < char_bookName.length; k++){//as long as k is less than the length of char_bookName keep running
char writing_book_name = char_bookName[k];//passes the values into the indexes
out.writeChar(writing_book_name);//passes the converted values of the variable to out

for(int k = 0; k < 1; k++){
char writing_delete = char_delete;

for(int j = 0; j < Ln.size(); j++){
int writing_id = Ln.size() +1;

System.out.println("WRITE FUNCTION IS WORKING");//testing, will delete on final version

out.close(); //tells it to stop writing

//catches errors and displays a message
catch (IOException iox)//catches an error with the input/output
System.out.println("Problem writing " + file_path);//tells you if there's an error, catch to the try statement

Does this help at all? :S