Hi, I'm currently in the process of attempting to implement a FAT (file allocation table) filestore in java. The first part of this is to return a simple directory listing of the files on the filestore with the file lengths in bytes. The way I think it will work is to create a 2-d array and then use a for-loop to run through the array, at 2-byte intervals (note that as it is 2-bytes they will be signed.., but I have pseudo-code to produce it, so it is not a problem). To return the length of the file, perhaps create an instance of StringBuffer and then use the length() method to return the string length? Any help would be appreciated. The method being called is the dir() method. I am now posting what I was given (and what I've done so far).
Any help would be much appreciated as there might be an easier way to implement this filestore - the first bit affects the rest of the program, which is simply taking an input via BufferedReader, and then reading/deleting files, which I'm sure I am capable of doing.
MINIDISKFILESTORE.JAVA
import java.io.*; package minidisk;
public class MiniDiskFileStore { byte[][] store;
public MiniDiskFileStore(String fname, boolean newOne) throws MiniDiskException; {
//looping through the array currently at 1 byte intervals, // not 2 byte as will be the case.
public InputStream openRead(String filename) throws MiniDiskException; { }//InputStream
public OutputStream openWrite(String filename) throws MiniDiskException; { }//OutputStream
public void delete(String filename) throws MiniDiskException; { }//delete
*/ public String dir() throws MiniDiskException; { }//dir
} //class MiniDiskFileStore
MINIDISKDEVICE.JAVA (GIVEN AND NOT TO BE MODIFIED)
package minidisk ;
import java.io.* ;
// This class simulates a "MiniDisk" - storing information in a file // which will be about 1MB. It will only be useable by other classes // in the same package - which will implement the file system.
public class MiniDiskDevice implements Serializable {
static final int BLOCKSIZE = 128 ; static final int BLOCKCOUNT = 8 * 1024 + 128 ; private byte [][] store ; transient File assocFile ;
// constructor - for use within this class only!
private MiniDiskDevice(File f) throws MiniDiskException { assocFile = f ; if (f.exists()) throw new MiniDiskException("Can't create new " + f.getName() + " - it already exists!") ; store = new byte [BLOCKCOUNT][BLOCKSIZE] ; try { assocFile.createNewFile() ; } catch (Exception e) { throw new MiniDiskException("Can't create " + f.getName() + " as a minidisk!") ; } }
// The method which MiniDiskFileStore uses to get an instance of a minidisk // fname is the name of the file in which the information is/will be stored // newOne is true if the file is being created (and therefore should NOT exist now)
static MiniDiskDevice openDevice(String fname, boolean newOne) throws MiniDiskException { File f = new File(fname) ; if (newOne) return new MiniDiskDevice(f) ; MiniDiskDevice result = null ; if (f.exists()) // if fname is a file which exists, deserialize from it try { ObjectInputStream infile = new ObjectInputStream( new FileInputStream(f)) ; result = (MiniDiskDevice) infile.readObject() ; result.assocFile = f ; } catch (Exception e) { throw new MiniDiskException("Can't read " + fname + " as a minidisk!") ; } else throw new MiniDiskException("File does not exist: " + fname) ; return result ; }
// Method to "read" a block from the minidisk. // bno is the block number to be read. // the result is an array of bytes containing what is in that block
// Method to update the real file version of the minidisk. // MiniDiskFileSytem must close this as its final action or all // changes to the minidisk contents since it was opened will be lost.
void close() throws MiniDiskException { try { ObjectOutputStream outfile = new ObjectOutputStream( new FileOutputStream(assocFile)) ; outfile.writeObject(this) ; } catch (Exception e) { throw new MiniDiskException("Can't write back minidisk to " + assocFile) ; } }
}
MINIDISKEXCEPTION.JAVA - SIMPLY CALLS THE SUPERCLASS
package minidisk ;
// This exception is thrown when any operation on the // simulated minidisk fails.
public class MiniDiskException extends Exception {
public MiniDiskException(String s) { super(s) ; } }