473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

writing array of objects to a file

1
The first class
import java.io.*;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.*;

import java.util.Formatter;
public class Project implements Serializable

{

TestProject s=new TestProject();
Scanner sc=new Scanner(System.in);
private int accNo;
private String company;
private int type;
private int quantity;
private double price;
private int newQuantity;
private double newPrice;
static int x=0;
private int eq;
private Formatter output;




public Project()
{
// save(TestProject[] accessory);
}


public static void save(TestProject accessory [])
{
try {

ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("student.txt"));

objOut.writeObject(accessory);
objOut.close();
}
catch(Exception e)
{
System.out.println("E");
}
}

public Project(int update)
{
getaccNo();

}
public void setaccNo(int acc_No)
{
accNo=acc_No;
}


public void setCompany(String company1)
{
company=company1;
}


public void setType(int type1)
{
type=type1;
}


public void setQuantity(int quantity1)
{
quantity=quantity1;
}


public void setPrice(double price1)
{
price=price1;
}


public int getaccNo()
{
return accNo;
}
public int getEq()
{
return eq;
}


public void startj()
{
System.out.println("** Computer Accessories Store **\n"+"How Much Equipments do you have");
int eq=sc.nextInt();
}

public String getCompany()
{
return company;
}


public int getQuantity()
{
return quantity;
}


public double getPrice()
{
return price;
}


public int getType()
{
return type;
}


String show()
{


return(" "+accNo+" "+" "+company+" "+type+" "+quantity+
" "+" "+price);
}
public void add()
{
Scanner sc=new Scanner(System.in);
System.out.println("What is the Serial ID: ");
accNo=sc.nextInt();
System.out.println("What is the Company name: ");
company=sc.next();
System.out.println("What is the type of the equipment:\n1-Mouse\n2-KeyBoard\n3-Monitor\n4-Printer ");
type=sc.nextInt();
while((type>4)||(type<1))
{
System.out.println("Sorry, there's no such a type");
System.out.println("What is the type of the equipment:\n1-Mouse\n2-KeyBoard\n3-Monitor\n4-Printer ");
type=sc.nextInt();
}
System.out.println("What is the quantity of the equipment: ");
quantity=sc.nextInt();
System.out.println("How much it costs for each equipment: ");
price=sc.nextDouble();
}
}




The second class

import java.util.Scanner;
import java.io.*;
import java.io.File;
import javax.swing.*;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.Serializable;
//import java.io.PrintWriter;





public class TestProjectSerializable
{
static int c=0;
static int eq=0;
static Project accessory[]=new Project[100];

public static void main(String args[])
{

Scanner sc=new Scanner(System.in);
int arraylength=accessory.length;
try{
objOut = new ObjectOutputStream(new FileOutputStream("AA.txt"));


}
catch(Exception e)
{
System.out.println("E");
}
int choose=0;
System.out.println("** Computer Accessories Store **\n");


//arraylength-eq;
do{
System.out.println("1-Add new Accessory\n2-Update Accessory(Quantity,Price)\n3-Sell Accessory\n4-View Reports\n5-Exit");
choose=sc.nextInt();
switch(choose)
{

case(1):
{
System.out.println("How Much Equipments do you have");
eq=sc.nextInt();

//File file=new File("AAA.txt");
//OutputStream w=new ObjectOutputStream(file);
//OutputStream output = null;
for(int i=0;i<eq;i++)
{

System.out.println("Please Fill the information for the equipment number: "+(i+1));
accessory[c]=new Project();
accessory[c].add();

try{

objOut.writeObject(accessory);
objOut.flush();
objOut.close();
}
catch(Exception e)
{
System.out.println(e);
}
c++;
}
}
}
}
}

There's an error says that TestProjectSerializable is not Serializable.
please help :(
Jan 16 '12 #1
0 1101

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Tad Marko | last post by:
Howdy! I'm trying to get my head around Python and I've come to realize that there are a lot of idioms in Python that are a bit different than in other languages. I have a class similar to what...
1
by: Daniel | last post by:
when writing out a file from .net, when is the file created? after the bytes are all written to the hard drive or before the bytes are written to the hard drive?
2
by: Raphael Iloh | last post by:
Hi all, I'm having problems comparing array objects. Take a look at this: int array1 = new int{1}; int array2 = new int{1}; Console.Writeln(array1.Equals(array2)); One would expect the above...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
5
by: Yunus's Group | last post by:
Hello, I have a need to write to a TEXT file which has the columns of data separated by a tab. I want to add another column as the last column in the file and add values to all the lines of data...
0
by: mahantesh | last post by:
Hi, here i am giving the code for writing in to file.. Byte info = new UTF8Encoding(true).GetBytes("This is some text in the file."); so my question is why use a Byte array to write to...
0
by: mahantesh | last post by:
Hi, my question is regarding writing in to file. below is the code for the same.. // Create the file. using (FileStream fs = File.Create(path, 1024)) { Byte info...
10
by: Terry Olsen | last post by:
I need to be able to write to a file simultaneously from approximately 4 different threads. I'm working on a program that will download parts of a file and combine the parts. Each thread will have...
4
by: Gilles Ganault | last post by:
Hello I'm stumped as to why PHP fails writing into a text file, with the script being called through Apache: ============== # pwd /usr/local/www/data/ ============== # ll
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.