简体   繁体   中英

java randomAccessFile write every thing with space between each character

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 :

Writes a string to the file as a sequence of characters. Each character is written to the data output stream as if by the writeChar method. The write starts at the current position of the file pointer.

Docs for writeChar :

Writes a char to the file as a two-byte value, high byte first. The write starts at the current position of the file pointer.

Do you actually need RandomAccessFile at all? Any reason for not using a FileOutputStream wrapped in an OutputStreamWriter using whichever encoding you want? Is there any way you can avoid having to write data at arbitrary positions like this?

You could use writeBytes instead of writeChars - 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM