Backup af fil, der skrives
Hej exp'ere,Jeg har en klasse kaldet BinIO, som sørger for al I/O til mit program.
Jeg ønsker at tage backup af den fil der gemmes - bare i et bibliotek jeg kalder "backup", men har ingen videre erfaring med hvordan det kan gøres. Dvs. den skal vel egentlig gemmes to gange - eller der skal oprettes en kopi af den fil der skrives til, bare i et andet bibliotek?
Min klasse er som følger:
import java.util.*; // Required for the Hashtable class
import java.io.*; // Required for handling the IOExceptions
public class BinIO
{
// Method for writing the file
public static void writeList(EventTable eventcollection)
{
EventComponents tempEvent;
try{
// Create a new FileOutputStream object
FileOutputStream eventFile = new FileOutputStream("Arrangementer.bin");
// Create a new DataOutputStream object
DataOutputStream eventWriter = new DataOutputStream(eventFile);
// Write each element of the hashTable to the file using enumeration
Enumeration en = eventcollection.getEvents();
while(en.hasMoreElements()){
EventComponents nextEvent = (EventComponents) en.nextElement();
eventWriter.writeUTF(nextEvent.getEventName());
eventWriter.writeUTF(nextEvent.getArtist());
eventWriter.writeUTF(nextEvent.getTitle());
eventWriter.writeUTF(nextEvent.getGenre());
eventWriter.writeUTF(nextEvent.getMonth());
eventWriter.writeUTF(nextEvent.getDate());
eventWriter.writeUTF(nextEvent.getTime());
eventWriter.writeUTF(nextEvent.getLocation());
eventWriter.writeUTF(nextEvent.getPrice());
}
/* Close the file so that it is no longer accessible to
the program */
eventWriter.close();
}
catch(IOException e)
{
System.out.println("Der opstod et problem med at skrive til filen");
}
}
// Method for reading the file
public static void readList(EventTable eventcollection)
{
String tempEventName;
String tempArtist;
String tempTitle;
String tempGenre;
String tempMonth;
String tempDate;
String tempTime;
String tempLocation;
String tempPrice;
boolean endOfFile = false;
try{
FileInputStream eventFile = new FileInputStream("Arrangementer.bin");
DataInputStream eventStream = new DataInputStream(eventFile);
while(!endOfFile)
{
try {
tempEventName = eventStream.readUTF();
tempArtist = eventStream.readUTF();
tempTitle = eventStream.readUTF();
tempGenre = eventStream.readUTF();
tempMonth = eventStream.readUTF();
tempDate = eventStream.readUTF();
tempTime = eventStream.readUTF();
tempLocation = eventStream.readUTF();
tempPrice = eventStream.readUTF();
try {
eventcollection.addEvent(new EventComponents(tempEventName, tempArtist, tempTitle, tempGenre, tempMonth, tempDate, tempTime, tempLocation, tempPrice));
}
catch (InvalidENException ex){
System.out.println(ex.getMessage());
}
}
catch (EOFException eof){
endOfFile = true;
}
}
/* Close the file so that it is no longer accessible to
the program */
eventStream.close();
}
catch(FileNotFoundException e)
{
System.out.println("\nIngen fil blev indlæst - eksisterer ikke(?)");
}
catch(IOException e)
{
System.out.println("\nDer opstod et problem med at læse filen");
}
}
}//end class