2011-11-20 29 views
0

我想使用随机访问文件来存储我的程序的信息。有没有一种方法可以读取和写入字符串的数组列表以及将文件中的变量枚举出来。 (我知道我能做到这一点使用Serializable接口来读取和写入,并从一个二进制文件,但我找了随机访问文件的解决方案。)从随机访问文件读写字符串和枚举变量的arraylist

见代码:

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; 
    } 

} 
+0

什么不行?另外,是否有一些特别的理由使用'RandomAccessFile'与数据库或序列化?嵌入式Derby非常有用。 – Dave

+0

我不知道如何读取和写入ArrayList和枚举进出RAM文件 – trs

回答

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.