private Part read() throws IOException { StringBuffer sb = new StringBuffer(); for (int i = 0; i < PNUMLEN; i++) sb.append(raf.readChar()); String partnum = sb.toString().trim(); sb.setLength(0); for (int i = 0; i < DESCLEN; i++) sb.append(raf.readChar()); String partdesc = sb.toString().trim(); int qty = raf.readInt(); int ucost = raf.readInt(); return new Part(partnum, partdesc, qty, ucost); }
/** {@inheritDoc} */ public char readChar() throws IOException { fileSize -= 1; if (fileSize < 0) { throw new EOFException(); } return dataInput.readChar(); }
public static void main(String args[]) { try { String fname = "d:\\q.txt"; String mode; // mode = "r";//r : file must exist mode = "rw"; // rw : file will be created or opened // open the file RandomAccessFile raf = new RandomAccessFile(fname, mode); /* //seek and write demo raf.seek(10);//position file r/w pointer at index 10 wrt BOF //a seek beyond file size causes file to grow upto the seek value raf.write(65);//raf.write('A'); */ // r/w java datatypes int i1, i2; float f1, f2; char c1, c2; String s1, s2; i1 = -10; f1 = 1234.5678F; c1 = 'q'; s1 = "hello files"; raf.seek(0); // reach BOF raf.writeInt(i1); raf.writeFloat(f1); raf.writeChar(c1); raf.writeUTF(s1); raf.seek(0); // reach BOF i2 = raf.readInt(); f2 = raf.readFloat(); c2 = raf.readChar(); s2 = raf.readUTF(); System.out.println(i2); System.out.println(f2); System.out.println(c2); System.out.println(s2); // close the file raf.close(); } catch (IOException ex) { System.out.println(ex); // ex converts into ex.toString() } } // main
/** * Create a new DBWriter according to the input file name. * * @param fileName the name of the file * @exception FileNotFoundException from library call * @exception IOException from library call or if file is corrupted */ public DBWriter(String fileName) throws IOException { outStream = new RandomAccessFile(fileName, "rw"); lastPosition = outStream.length(); // case this is a new database if (lastPosition == 0) { wroteColumnNames = false; headerSize = 0; numRows = 0; CRC = 0; needReposition = false; } // case this might be an old database else if (lastPosition > MIN_DATA_OFFSET) { // check that we have a valid database here checkID(); wroteColumnNames = true; // read header data outStream.seek(HEAD_SIZE_OFFSET); headerSize = outStream.readLong(); numRows = outStream.readLong(); numColumns = outStream.readLong(); // read description outStream.seek(headerSize - DESCRIPTION_SIZE - ID_SIZE - CRC_SIZE); description = ""; for (int i = 0; i < DESCRIPTION_LENGTH; i++) description += outStream.readChar(); description = description.trim(); // read CRC outStream.seek(headerSize - CRC_SIZE); CRC = outStream.readInt(); // by default we append to an existing database needReposition = true; } // case this is not a database else throw new IOException("Attempting to load invalid database"); }
/** This reads a binary char from the file. */ public char readBinaryChar(int col, int row) throws IOException { raf.seek(row * nBytesPerRow + columnStartAt[col]); return raf.readChar(); }
// call this method to check the existence of the ID // the file pointer must be already positioned private void checkID() throws IOException { for (int i = 0; i < ID.length(); i++) if (outStream.readChar() != ID.charAt(i)) throw new IOException("Attempting to load invalid database"); }
/* @see java.io.DataInput.readChar() */ public char readChar() throws IOException { return raf.readChar(); }