Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 03-30-07   #1 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,470

FAQs Submitted: 7
Folding Team Rank: 390
Trader Rating: 0
Default Java help (reading a binary database)

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){
			
		}
	}
My problem is that it is only reading the first thing that I entered into the database.

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:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

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

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline I fold for Overclock.net Overclocked Account dangerousHobo's Gallery   Reply With Quote
Old 03-31-07   #2 (permalink)
AMD Overclocker
 
decompiled's Avatar
 
amd nvidia

Join Date: Feb 2006
Location: Redsox Nation
Posts: 468

Rep: 47 decompiled is acknowledged by some
Unique Rep: 45
Hardware Reviews: 9
Trader Rating: 0
Default

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 =).

System: Slow Poke
CPU
AMD X2 4400
Motherboard
MSI K8N Diamond +
Memory
3gb TCC5
Graphics Card
eVGA 7900GS
Hard Drive
74gb Raptor + Raid 1 640's
Sound Card
Audigy SE
Power Supply
OCZ 520 PowerStream
Case
Antec P180
CPU cooling
Stock AMD
GPU cooling
Stock eVGA
OS
Vista x64
Monitor
Dell 1905FP

Last edited by decompiled : 03-31-07 at 02:56 PM.
decompiled is offline   Reply With Quote
Old 03-31-07   #3 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,470

FAQs Submitted: 7
Folding Team Rank: 390
Trader Rating: 0
Default

Edit::
Quote:
<Edit::>
I just found out that I'll have to read everything from the file(probably add it to an arrayList and then add the new recipe in and then write the new arrayList to the database file, overwriting the old file and not appending to it. I guess when java writes the header file to the binary file it says how long it is. SO after adding a new object to the file it still thinks that it is the same size after the fisrt entry. I guess there are ways around this, just given the timeline of this project it would be to difficult. So problem solved for know, I'll just have to make a class that manages the database with and arrayList.

<end::Edit>
I don't want to have to "assemble the object from that data when reading back in", thats way I want about and wrote it to the database as an Object. It save work and less code. I'm working with binary files too so, Scanner and nextLine, etc, don't exists.

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:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

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

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64
Monitor
Acer AL2216W 22" WS LCD

Last edited by dangerousHobo : 03-31-07 at 04:13 PM.
dangerousHobo is offline I fold for Overclock.net Overclocked Account dangerousHobo's Gallery   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 02:03 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.21649 seconds with 8 queries