|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Java help (reading a binary database)
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||||
|
Photography nut
![]() |
Well I have a database of recipes and the database is a binary file.
I'm able to take a recipe write it to the database, close the outputStream, create an inputStream, and read the database, however I've added multiple recipes to the database. I want to be able to open up the database and print everything that is inside it. Everything that got written to the database got written as an object. The method: Code:
public void printWholeDatabase(String database){
ObjectInputStream inStream = null;
IngredientList recipe=null;
try{
inStream = new ObjectInputStream(new FileInputStream(database));
while(true){
recipe=(IngredientList)inStream.readObject();
System.out.println(recipe);
}
}
catch(ClassNotFoundException e){
}
catch(FileNotFoundException e){
}
catch(IOException e){
}
}
I know I'm not making a new database file b/c the size of the file just keeps getting bigger. Any Ideas.
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
||||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
AMD Overclocker
|
Try something like this:
Code:
try {
s = new Scanner(new BufferedReader( new FileReader(args[0])));
while ( s.hasNext() )
{
String file = s.nextLine();
parse(file); // This is your parsing method. Not sure what you used. Newline would work well I'm guessing...
}
} finally {
if ( s != null )
s.close();
}
*** Edit *** I looked at your code. The way I thought of the issue was to store data in the database and then assemble the object from that data when reading back in. You have chosen to do it slightly different however the idea should still be the same. You will want to have a conditional that loops through the file. In my example hasNext() does that for you. The conditional is met when the null character is found. Namely the end of the file. The API explains this better =).
Last edited by decompiled : 03-31-07 at 02:56 PM. |
|||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||||
|
Photography nut
![]() |
Edit::
Quote:
I just don't get why its read the first object and then when it goes to read the second it throws a IOException. I wrote this program to test out reading an object from a file, and it works fine, so I don't get why it wont here. The test program though: Code:
import java.io.Serializable;
public class Abc implements Serializable{
static int count = 0;
int i;
Abc()
{
i = ++count;
System.out.println("Count Add "+count);
}
public String toString()
{
return "Count "+i;
}
}
Code:
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Test1 {
public static void main(String[] args)throws Exception {
ObjectOutputStream oop = new ObjectOutputStream(new FileOutputStream("test.dat"));
Abc a[] = new Abc[5];
int i = 0;
while(i < 5)
{
a[i++] = new Abc();
}
i = 0;
while(i < 5)
{
oop.writeObject(a[i++]);
}
oop.close();
Abc temp = null;
ObjectInputStream oip = new ObjectInputStream(new FileInputStream("test.dat"));
try
{
while((temp = (Abc)oip.readObject()) != null)
{
System.out.println(temp.toString());
}
}
catch(EOFException e)
{
System.out.println("All objects read. End Of File");
}
oip.close();
}
}
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
Last edited by dangerousHobo : 03-31-07 at 04:13 PM. |
|||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|