从随机访问文件中读取和写入字符串和枚举变量的arraylist

时间:2011-11-20 21:11:55

标签: java file ram

我想使用随机访问文件来存储我的程序的信息。有没有办法可以在文件中读取和编写字符串和枚举变量的arraylist。 (我知道我可以使用Serializable接口来读取和写入二进制文件,但我正在寻找随机访问文件的解决方案。)

见代码:

public Book(RandomAccessFile file) throws IOException {
        // set up Item based on data stored in data disk file
       try {
        this.ISBN=file.readLong();
        this.title = readCharacters(file, 30);
        this.publicationDate.set(file.readInt(), file.readInt(), file.readInt());// year month date
        this.publisher = readCharacters(file, 30);
        this.authorNames= //insert method to read in arraylist of Strings           
this.salePrice=file.readDouble();
        this.bookCategory=//insert method to read in enum       
 } catch (IOException io) {
        throw io;
    }
}
private String readCharacters (RandomAccessFile dataFile,int numChars)
  throws IOException{
      //reads a specific number of characters and places text in String field
    char[] data = new char[numChars];
    String field;
    try{
        for (int i=0;i<numChars;i++)
              data[i]= dataFile.readChar();
        //now place data into the field
        field = new String (data);
        field = field.trim(); //get rid of trailing spaces
    }
    catch(IOException io){
        throw io;
    }
    return field;
}

public void writeToFile(String fileName,long location)throws Exception{
    try{
    RandomAccessFile invFile = 
        new RandomAccessFile(fileName,"rw");
    //seek to correct record in file          
       invFile.seek(location);
    //now write out the  record
     //write out the data in fixed length fields
       //String fields must be truncated if too large
       //or padded with blanks if too small
       invFile.writeLong(ISBN);
       writeCharacters(invFile,title,30);
       invFile.writeInt(publicationDate.YEAR);
       invFile.writeInt(publicationDate.MONTH);
       invFile.writeInt(publicationDate.DAY_OF_MONTH);
       writeCharacters(invFile,publisher,30);
     //insert method to write arraylist of authorNames that are Strings        
invFile.writeDouble(salePrice);
     //insert method to write out bookCategory which is an enumerated type          
 invFile.close();
    }
private void writeCharacters(RandomAccessFile invFile,
        String data, int numChars)throws IOException{
    char[] stringValue;
    String dataChars;
    try{
        //assume data has the complete number of characters
        dataChars = data.substring(0,numChars); 
        invFile.writeChars(dataChars);
    }
    catch(IndexOutOfBoundsException e){
        //data has less number of characters, must be padded with blanks
        dataChars = data.substring(0,data.length());
        invFile.writeChars(dataChars);
        //now add extra blanks
        for (int i=0; i<numChars-dataChars.length();i++)
            invFile.writeChar(' ');         
    }
    catch(IOException io){
        throw io;
    } 

}

1 个答案:

答案 0 :(得分:0)

您不以任何特殊方式阅读或书写。它与任何其他流相同:

File f = new File("outputfile");
RandomAccessFile raf = new RandomAccessFile(f, "rw");

char ch = raf.readChar(); // establish the connection
raf.seek(f.length()); // go to the end of the file
raf.writeUTF("some string"); // write something
raf.close(); // close it.