I used this function to write a line in specific index :
private void write_line(Student s,int index){
try {
Student_pool.seek(0);
Student_pool.seek(index*(25*Integer.SIZE+4*Character.SIZE));
Student_pool.writeChars(s.getId());
Student_pool.writeChars(s.getHw().substring(0,2));
Student_pool.writeChars(".");
Student_pool.writeChars(s.getHw().substring(3,5));
Student_pool.writeChars(s.getPrj().substring(0,2));
Student_pool.writeChars(".");
Student_pool.writeChars(s.getPrj().substring(3,5));
Student_pool.writeChars(s.getMidtermExam().substring(0,2));
Student_pool.writeChars(".");
Student_pool.writeChars(s.getMidtermExam().substring(3,5));
Student_pool.writeChars(s.getFinalExam().substring(0,2));
Student_pool.writeChars(".");
Student_pool.writeChars(s.getFinalExam().substring(3,5));
Student_pool.writeChars("\n");
Student_pool.getFD().sync();
} catch (IOException ex) {
Logger.getLogger(Storagelmpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
But when I try to open the file the result is not correct and every character has a space after it.
The result looks like this:
8 1 0 1 8 7 3 1 2 2 0 . 0 0 2 0 . 0 0 2 0 . 0 0 2 0 . 0 0
8 1 0 1 8 8 2 7 5 1 6 . 0 0 1 0 . 0 0 1 3 . 0 0 1 9 . 0 0
I think there is problem with my encoding and another problem with my seek operand.
But I don’t know how to fix it.
It’s behaving exactly as documented:
Docs for
writeChars:Docs for
writeChar:Do you actually need
RandomAccessFileat all? Any reason for not using aFileOutputStreamwrapped in anOutputStreamWriterusing whichever encoding you want? Is there any way you can avoid having to write data at arbitrary positions like this?You could use
writeBytesinstead ofwriteChars– that will basically write just ISO-8859-1 characters – would that be good enough for you? It’s not going to preserve non-ISO-8859-1 data though…