Dúvida Datafile

1 resposta
A

Por favor alguém sabe como alocar um espaço determinado de 8 mb em disco para um datafile em java, sem usar nenhum sgbd?

1 Resposta

A

Veja se isso quebra seu galho…





/**************************************************************************

* Course: Comp118-s02

* Program: HandleServer for Addressbook

* Programmer: Hiu Ling Wong

* Date: 2/16/02

* Description: This class extends thread and deals with incoming and

* outgoing message (which is in the protocol form).

* It processes different request including add, delete, search,

* and update. It´s also responsible for retrieving the date

* from hard drive and writing the data to the hard drive when

* the connection is closed. It is also responsible for

* returning appropriate status code with the reply protocol.

****************************************/



import java.io.;

import javax.servlet.
;

import javax.servlet.http.;

import java.net.
;

import java.util.;



public class HandleServer extends Thread

{

//connection between server and applet

Socket connection;



//establish connection for protocol transfer

//between server and client

InputStream inStream;

DataInputStream inDataStream;

OutputStream outStream;

DataOutputStream outDataStream;



/
//establish connection for the server and the file on hard disk

File ioFile;

FileOutputStream outFileStream;

ObjectOutputStream outObjectStream;

FileInputStream inFileStream;

ObjectInputStream inObjectStream;

*/



String requestProtocol; //message from client

String replyProtocol; //message sent from server to client

Hashtable dataFile=new Hashtable(); //copies all the entries to hash table

Hashtable temp; //stores each client request of each transaction

String name; //contains first and last name of each request



Entry newHashtable;



// ************************* HandleServer



public HandleServer( Socket socket )

{

super ();

newHashtable=new Entry();

connection = socket;

} // end constructor





// ******************************** run



public void run () {



dataFile=newHashtable.read_data();





try {



//process the request protocol from the applet

outStream = connection.getOutputStream ();

outDataStream = new DataOutputStream ( outStream );

inStream = connection.getInputStream ();

inDataStream = new DataInputStream ( inStream );



while ( true ) {

//replyProtocol="STATUS=000"; //reset reply message after each reply

requestProtocol = inDataStream.readUTF ();



//store the request into a temp hashtable

temp=HttpUtils.parseQueryString(requestProtocol);



/


* ADD request

/

if((((String[])temp.get("METHOD"))[0]).equals("ADD"))

{

name=(((String[])(temp.get("F_NAME")))[0]);

name=name.concat(((String[])(temp.get("L_NAME")))[0]);

//new entry is added to the temp dataFile in the server

//the hash key "name" is decoded but the rest of the entry

//is kept encoded since we don´t care how the data is stored as

//long as we can retrive the info correctly.



dataFile.put(name, requestProtocol);

replyProtocol="STATUS=200";

}//done with add_op



/


* DELETE request

/

if((((String[])temp.get("METHOD"))[0]).equals("DELETE"))

{

//if no entry is in the hash table, we can skip this operation

if(dataFile.isEmpty())

replyProtocol="STATUS=000";

else{

name=(((String[])(temp.get("F_NAME")))[0]);

name=name.concat(((String[])(temp.get("L_NAME")))[0]);

//go thru the data hash table and look for it

if(dataFile.containsKey(name))

{

//delete the data if it exist

dataFile.remove(name);

replyProtocol="STATUS=201";

}

else

replyProtocol="STATUS=404"; //entry not found

}



}//done with delete_op



/


* SERARCH request

/

if((((String[])temp.get("METHOD"))[0]).equals("SEARCH"))

{

if(dataFile.isEmpty())

replyProtocol="STATUS=000";

else{

name=(((String[])(temp.get("F_NAME")))[0]);

name=name.concat(((String[])(temp.get("L_NAME")))[0]);



//go thru the data hash table and look for it

if(dataFile.containsKey(name))

{

//no need to encode the entry since it was in the encoded form when

//it stored in the dataFile hash table

replyProtocol="STATUS=202&"+((String)(dataFile.get(name)));

}

else

replyProtocol="STATUS=404"; //entry not found

}

}//end of method=search





/


* UPDATE request

*******************************/

if((((String[])temp.get("METHOD"))[0]).equals("UPDATE"))

{

name=(((String[])(temp.get("F_NAME")))[0]);

name=name.concat(((String[])(temp.get("L_NAME")))[0]);



//look thru the data hash table and delete the update_name

//if it´s in the hash table

if(dataFile.containsKey(name))

dataFile.remove(name);



//put the updated entry in the hash table

dataFile.put(name, requestProtocol);

replyProtocol="STATUS=203";



}//end of method=update



//write the reply to the outputStream

outDataStream.writeUTF (replyProtocol);



} // end while



} // end try



catch ( EOFException e )

{

try {

newHashtable.write_data(dataFile);

//toDataFile(dataFile); //write to file on hard disk

connection.close(); //close the socket

System.out.println("HandleServer: EOFException, handleSocket closed ok");

//e.printStackTrace();

return;

}

catch ( IOException ee ) {

newHashtable.write_data(dataFile);

//toDataFile(dataFile); //write to file

System.out.println("HandleServer: IOException, handleSocket closed ok");

ee.printStackTrace();

return;

} // end IOException



} // end catch EOFException

catch ( IOException e )

{

newHashtable.write_data(dataFile);



//toDataFile(dataFile); //write to file

System.out.println("HandleServer: IOException caught");

e.printStackTrace();

return;

} // end catch IOException





} // end run





/
public void fromDataFile()

{

Hashtable ht=new Hashtable();



//String fileName="/afs/cs.unc.edu/project/courses/comp118-s02/members/wong/WEB-INF/classes/entry.txt";

String fileName="entry.txt";

//String path=".";



// Instantiate the file object

ioFile = new File (fileName );



// Instantiate and chain the FileOutputStream



try{

inFileStream = new FileInputStream ( ioFile );

inObjectStream = new ObjectInputStream ( inFileStream );

}



catch(IOException e)

{

System.out.println("File not found");

return;

}

try {

System.out.println ( "nReading from file: " + ioFile.getName() );

ht = (Hashtable)(inObjectStream.readObject());

}

catch ( IOException e ) {

System.out.println ( "Error reading entry.txt " + e.toString() );

return;

} // end catch

catch ( ClassNotFoundException e ) {

System.out.println ( "Error: ClassNotFoundException " + e.toString() );

return;

} // end catch



dataFile=ht;

} //end with from Data method



public void toDataFile(Hashtable ht)

{

//String fileName="/afs/cs.unc.edu/project/courses/comp118-s02/members/wong/WEB-INF/classes/entry.txt";

String fileName="entry.txt";

//String path=".";



// Instantiate the file object

ioFile = new File(fileName);



// Instantiate and chain the FileOutputStream



try{

outFileStream = new FileOutputStream ( ioFile );

outObjectStream = new ObjectOutputStream ( outFileStream );

}



catch(IOException e)

{System.out.println("OutFile not found");

}

try {

outObjectStream.writeObject (ht);

outObjectStream.flush ();

outObjectStream.close ();



}

catch ( IOException e ) {

System.out.println ( "Error writing entry.txt " + e.toString() );

return;

} // end catch





} //end with to Data method

/

}//end of HandleServer class





Criado 25 de junho de 2002
Ultima resposta 25 de jun. de 2002
Respostas 1
Participantes 2