Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why can't I write to a file?

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
Hi,
I'm having loads of trouble writing objects to a file.
Here's my code:
-------------------Menu.java----------------------------
/**
/@Author: Joe Wardell
/@Date: 11/11/02
/@Version: Derek's Dating Agency
*/
import java.io.*;

public class Menu implements Serializable
{
public BufferedReader input = new BufferedReader(
new InputStreamReader(System.in));
public ClientList tempArray = new ClientList(50);
public File path = new File("temp.txt");

public void mainMenu() throws IOException, ClassNotFoundException
{
tempArray.readIn(tempArray);
char option = ' ';
do
{
System.out.println("\n\tWelcome to Derek's Dating Agency\n"
+ "Clients: " + tempArray.size() + "\n");
System.out.println("1: New Client\n");
System.out.println("2: Find Client\n");
System.out.println("3: Show all Data\n");
System.out.println("4: Exit");
clear(3);
System.out.print("Enter option number: ");
option = input.readLine().charAt(0);
clear(50);

switch (option)
{
case '1':
addClientMenu();
break;

case '2':
search();
break;

case '3':
showAll();
break;

case '4':
break;

default:
System.out.println("Sorry, " + option
+ " is an invalid option");
}
} while (option != '4');

}

public void showAll()
{

}

public void search() throws IOException
{
String search;
System.out.print("Surname of Customer to Search for: ");
search = (input.readLine());
clear(50);
tempArray.searchBySurname(search, array);
}




public void addClientMenu() throws IOException
{
System.out.println("\n\t********** "
+ "Add a New Client"
+ " **********\n");

System.out.print("Forename: ");
String firstName = input.readLine();
System.out.print("Surname: ");
String lastName = input.readLine();
Client clientTemp = new Client(firstName, lastName);
System.out.print("Sex (m/f): ");
char gender;
do
{

gender = (input.readLine().toLowerCase().charAt(0));
switch (gender)
{
case 'm':
clientTemp.setSex(0);
break;

case 'f':
clientTemp.setSex(1);
break;

default:
System.out.println("\t\tSorry, invalid entry. "
+ "Enter either M or F");
}
} while (clientTemp.getSex().equalsIgnoreCase(" "));

System.out.print("Height: ");
clientTemp.setHeight(Double.parseDouble(input.readLine()));
System.out.print("Age: ");
clientTemp.setAge(Integer.parseInt(input.readLine()));
residenceMenu(clientTemp);


System.out.print("Description of yourself: ");
clientTemp.setDescription(input.readLine());
System.out.print("Phone Number: ");
clientTemp.setPhone(input.readLine());
System.out.print("E-mail: ");
clientTemp.setEmail(input.readLine());

vacationMenu(clientTemp);
clientTemp.setIsObject(true);

tempArray.addClient(clientTemp, array);
return;
}

public void vacationMenu(Client Vclient) throws IOException
{
Vacation vacationTemp = new Vacation(" ", 0, 0, 0, 0);
System.out.print("Destination: ");
vacationTemp.setArea(input.readLine());
int holidayType = 0;
do
{
System.out.print("Holiday Types: ");
System.out.println("1. Beach?");
System.out.println("\t\t2. Diving?");
System.out.println("\t\t3. Climbing?");
System.out.println("\t\t4. Safari?");
System.out.println("\t\t5. Hiking?");
System.out.print("Option Number: ");
holidayType = Integer.parseInt(input.readLine());
}while (holidayType <= 0 | holidayType > 5);

holidayType--;
vacationTemp.setHolType(holidayType);

System.out.print(&quot;Minimum cost in pounds: &quot;);
vacationTemp.setMinCost(Integer.parseInt(input.readLine()));
System.out.print(&quot;Maximum cost in pounds: &quot;);
vacationTemp.setMaxCost(Integer.parseInt(input.readLine()));

int when = 0;
do
{
System.out.print(&quot;Season: &quot;);
System.out.println(&quot;\t1. Spring?&quot;);
System.out.println(&quot;\t\t2. Summer?&quot;);
System.out.println(&quot;\t\t3. Autumn?&quot;);
System.out.println(&quot;\t\t4. Winter?&quot;);
System.out.print(&quot;Option Number: &quot;);
when = (Integer.parseInt(input.readLine()));
}while (when <= 0 | when > 4);
when--;

vacationTemp.setTimeOfYear(when);
Vclient.setDestination(vacationTemp);
return;
}

public void residenceMenu(Client Rclient) throws IOException
{
System.out.print(&quot;House Name or Number: &quot;);
String houseNameOrNum = input.readLine();
System.out.print(&quot;Street Name: &quot;);
String streetName = input.readLine();
System.out.print(&quot;Town: &quot;);
String townName = input.readLine();
System.out.print(&quot;County: &quot;);
String countyName = input.readLine();
System.out.print(&quot;Post Code: &quot;);
String Pcode = input.readLine();
Residence locationTemp = new Residence(houseNameOrNum,
streetName,
townName,
countyName,
Pcode);
Rclient.setAddress(locationTemp);
return;
}

public void clear(int newLines)
{
for (int i = 0; i <= newLines; i++)
{
System.out.println();
}
}

public static void main (String[] args) throws IOException, ClassNotFoundException
{
Menu test = new Menu();
test.mainMenu();
}
}
----------------------------------------------------------
All the other classes have 'implements Serializable' in the class definition.

The thing is, all the classes compile, then when it actually tries to write to a file I get this command prompt output:
-----------command prompt output from Menu.java-----------
Writing to disk....Exception in thread &quot;main&quot; java.io.NotSerializableException: java.io.Bu
fferedReader
at java.io_ObjectOutputStream.writeObject0(ObjectOutputStream.java:1054)
at java.io_ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1330)
at java.io_ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1302)
at java.io_ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1245)
at java.io_ObjectOutputStream.writeObject0(ObjectOutputStream.java:1052)
at java.io_ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at Menu.writeIt(Menu.java:132)
at Menu.addClientMenu(Menu.java:121)
at Menu.mainMenu(Menu.java:42)
at Menu.main(Menu.java:161)

C:\Java\CompSci\Project1>
-----------------------------------------------------------
The thing I don't understand, is hy is it bothered about BufferedReader? that has nothing to do with writing a file!
I know it will be an attribute of any Menu instance, but why does it not like it? an how else could I make a object to take input?

Please, if anyone could giv me a hand I'd be so grateful, I just CANNOT get this to work, and I'm beginning to lose faith in Java :O

=) allllll comments and suggestions are damn welcome!
Trinculo we are all of us living in the gutter.
But some of us are looking at the stars.
 
Maybe you should declare the input, tempArray and path variables to 'transient'. When you try write out the Menu class the systems wants to write every variable and the BufferedReader is not serializable.

Hope this help.
Otto
 
Some objects are not made to Serializable (does that word strike anyone else as a ugly word?), BufferedReader is a good example. It is in effect just a link to a file (simplistic but not to inaccurate), so saving it would be pointless and as such it cannot be written when you write an object.

To know where or not an object is Serializable check out the Java doc pages and near the top of each page for each object there is an &quot;implements&quot; heading. If under the &quot;implements&quot; heading the word Serializable appears then that means the designers intended for it to be able to be saved, if it doesnt mention Serializable or if the &quot;implements&quot; header is not there at all then that means the designers did not mean for the the object to be saved.

As Otto suggested, using the
Code:
transient
keyword means that what ever it is applied to will not persist when the object is stored.

eg
Code:
class T implements Serializable 
{
   transient private final int a; // will not be stored
   private final int b; // will be stored
}

so trying
Code:
transient public BufferedReader input = new BufferedReader(
                                  new InputStreamReader(System.in));

may work.


For improved performance, make anything else which you do not need to be saved to a file transient also, that means there is less IO going on and a smaller file at the end of it all.

Let me know if it works, I honestly have never encounters the situation you have, but I have used transient now and again.

----------------------------------------
There are no onions, only magic
----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top