Results 1 to 2 of 2
  1. #1
    Displonker is offline Member
    Join Date
    Mar 2011
    Rep Power

    Default Problem Writing to a RAF


    Sorry if this is the wrong thread, I figured I am decently new to java. I try not to ask many questions, but this is one that I just can't solve.

    I'm writing a program in Netbeans for Mac that allows the storing and editing of the student's timetables in my school (I'm a student - this is coursework).

    Absolutely everything works perfectly, except for deleting a student.

    Basically, I replace the first 10 characters of the student that is to be deleted with "XXXXXXXXXX" for example.
    I then create a new file, and copy all the students into the new file (so long as there isn't XXXXX... In their name), delete the old file, and rename the new file.

    The problem I have is with the method RAF.writeUTF();.
    I'm sorry if it's messy, I've not got the hang of perfect programmer styles..
    Java Code:
            //Copy entries in file 1, to file 2, excluding any beginning with x's.
                RandomAccessFile RAF = new RandomAccessFile("Students.csv", "rw");
                RandomAccessFile RAF2 = new RandomAccessFile("Students2.csv", "rw");
                //Ensure both files seek are at the beginning
                //(Not sure this is necessary, but I included it anyway.)
                //While the file pointer has not reached the end of the file{
                while(RAF.getFilePointer() < RAF.length()){
                    //Store filepointer.
                    Int filePointer = RAF.getFilePointer();
                 //if line [I]does not[/I] start with "XXXXXXXXXX"
                     //^RAF.readLine moves the file pointer, so
                    //send it back to beginning of line, so it can be read again.
                    //Write into RAF2, whatever the line of RAF is.
                    RAF2.writeUTF(RAF.readLine().toString() + "\n");
                }//End if statement
                }//End while
                //Close the Files
            }//End try
            catch(Exception e){
                System.out.println("Something wrong copying the file");
    //Code would then go to delete the first file.

    Sorry the example is long - if I make it shorter it doesn't make sense.
    My files are both ".csv".
    Last edited by Displonker; 03-29-2011 at 08:25 PM. Reason: Made code compilable. You still need example file data (below).

  2. #2
    Displonker is offline Member
    Join Date
    Mar 2011
    Rep Power


    The first two lines of "Students.csv", looks like this:
    Java Code:
    Niaz Ahmed,Y13,En13a1s,Fr13bh,Ec13h,Phy13h,Ma13s,Cs13s
    Ben Brian,Y13,En13a1s,De13b,Psy13h,Ch13s,Ma13h,Ar13h
    However, when I do the above code, the new file starts with this:
    Java Code:
    6Niaz Ahmed,Y13,En13a1s,Fr13bh,Ec13h,Phy13h,Ma13s,Cs13s
    4Ben Brian,Y13,En13a1s,De13b,Psy13h,Ch13s,Ma13h,Ar13h
    Now, I'm not sure, but I assume this extra bit on the front is due to the .writeUTF method, defining the length of the string perhaps.
    However, this messes up all other functions of my program.

    .writeChars() does not work either, as when I opened this attempt in 0xED (Hex Editor) it revealed a bunch of information about each character, that was also messing up my searches.

    An awesome solution would be if there was simply another way to write to the file, but I doubt that, so I assume I must be writing somewhat incorrectly.

    I also wonder if my ("\n") in each file has anything to do with it.
    I figured .readLine(); would read the line and ignore the line break.

    If there's any help anyone can give me, I'd be very grateful.
    Sorry for the long description.

    Last edited by Displonker; 03-29-2011 at 08:23 PM. Reason: Did not finish. / Grammar

Similar Threads

  1. Problem writing to a 2D array
    By skelter15 in forum New To Java
    Replies: 8
    Last Post: 08-11-2010, 10:31 AM
  2. writing to text file problem
    By blumdiggity in forum Networking
    Replies: 1
    Last Post: 02-26-2010, 03:43 PM
  3. A bit of a complex reading and writing problem
    By jigglywiggly in forum New To Java
    Replies: 1
    Last Post: 10-28-2009, 07:08 PM
  4. Writing to file problem
    By PeraPisar in forum Advanced Java
    Replies: 12
    Last Post: 09-16-2009, 12:00 PM
  5. Problem while writing xml file
    By Rajesh Sahu in forum Java Servlet
    Replies: 0
    Last Post: 12-23-2008, 05:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts