Any Java programmers? Need help with IOStream.

Hi guys,

So I have been using Java for 6 months and I have come across a problem.

I have a program where you populate a hashmap through textboxes and a combobox. I also have an add button which adds the information to the hashmap. When I exit the program through the use of a menubar the hashmap is then written to a .ser file. I also have a form which a textarea to display all the data that has been populated into the hashmap.

When the program is closed the .ser has been written, but when I open the program again it does not populate the hashmap as I can no longer see the populated entries in the textarea, nor does it allow me to add data to the .ser file.

(Terrible explanation, I know)

Hopefully the code below will explain what I can't.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  //Code on my main menu for file creation / reading to the hashmap

		try
		{
			ObjectInputStream ois =  new ObjectInputStream(new FileInputStream("carFile.ser"));
			HashMap<String, Car> cars = (HashMap<String, Car>)ois.readObject();
			ois.close();
		}

		catch(ClassNotFoundException cnfx)
		{
			JOptionPane.showMessageDialog(null, "The data could not be read.");
			cnfx.printStackTrace();
		}
		catch(FileNotFoundException fnfx)
		{
			JOptionPane.showMessageDialog(null, "No file found.");
			fnfx.printStackTrace();
		}
		catch(IOException iox)
		{
			JOptionPane.showMessageDialog(null, "Could not read from file.");
			iox.printStackTrace();
		}

//Code to output data to .ser file, also on main menu

		if ( s.equalsIgnoreCase("Exit"))
		{
			try{
				
				ObjectOutputStream oos =  new ObjectOutputStream(new FileOutputStream("carFile.ser"));
				oos.writeObject(cars);
				oos.close();
			}
			catch(FileNotFoundException fnfx)
			{
				JOptionPane.showMessageDialog(null, "No file found!");
				fnfx.printStackTrace();
			}
			
			catch(IOException iox)
			{
				JOptionPane.showMessageDialog(null, "Could not write file");
				iox.printStackTrace();
			}
			System.exit(0);
		}

//Code on Display page, which stops working after I close the program after the file has been created.

		if ( e.getSource()==btnExit)
		{
			txtArea.setText("");
			this.setVisible(false);
		}
			
		else if ( e.getSource()==btnDisplay)
		{
			txtArea.setText("");
			
			String s;
			s="Reg No.   Make             Model             Description        						Colour		Category		Mileage		Year	Cost(£)		Selling Price(£)\n";
			txtArea.append(s);									
			s="------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n";
			txtArea.append(s);
			
			Set set = cars.entrySet();
			Iterator i = set.iterator();
			
			while (i.hasNext())
			{
				Map.Entry me = (Map.Entry)i.next();
				s = me.getValue() + "\n";
				txtArea.append(s);
			}


I have searched the intenet for hours and have not found a similar problem.

Thank you
-Sailorman444
So I have been using Java for 6 months and I have come across a problem.

Your first problem is creating a thread in the wrong forum.
I come to this forum because I have had a lot of help on C++ and remember seeing people posting questions about Java.
Line 6: the new local variable cars shadows the member cars which is not modified

ie:
1
2
3
4
5
6
		try
		{
			ObjectInputStream ois =  new ObjectInputStream(new FileInputStream("carFile.ser"));
			HashMap<String, Car> cars = (HashMap<String, Car>)ois.readObject();
			ois.close();
		}

Last edited on
Tried the above method. I don't think the file is reading in as it is not populating the hashmap even after creating and writing to the file, once I close the program nothing displays, and it does not allow to add to the file. Tested this by using my textarea which displays all the data using an iterator
Last edited on
Topic archived. No new replies allowed.