Results 1 to 5 of 5
  1. #1
    Goodwine's Avatar
    Goodwine is offline Member
    Join Date
    Nov 2008
    Location
    Mexico
    Posts
    14
    Rep Power
    0

    Default Can someone help me working with files?

    this is a simple school-project so i hope someone can help me
    basically i just need to solve some doubts about using files

    Well, my program is an abstract class User which is extended by Customer and Admin classes, then i want to write customer and admin objects on a file for each kind

    first doubt, can i put a method in User to write objects on files to save code typing on the other classes?, like

    Java Code:
    public void write(String fileName)
    {
    	try
    	{
    		ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(fileName, true));
    		objOut.writeObject(this);
    		objOut.close();
    	}
    	catch (Exception e)
    	{
    		e.printStackTrace();
    	}
    }
    an example will be on Customer add a method like
    Java Code:
    public void write
    {
    	write("Customer.dat");
    }
    supposing i can, or even if i cant and have to write it twice, do i need to use the constructor of FileOutputStream(String, boolean) so i can add objects, or what should i do instead of that?

    also, Customer has an "account" field which is an integer, and if i write many customers, how do i look for specifically one according to that field?
    (the admin has a String "account" field but i guess it would be similar)

    another doubt is... when i want to modify a customer's fields, supposing he has a float field called "owes" which indicates how much money does he owe to the store, the the customer pays and now i have to reduce the "owes" by the amount that was paid,
    i will first look for the Customer object on the file the open it and then edit it and then save it again
    but how do i replace that customer for the new one?

    and if i want to delete a customer forever? what should i do?

    last doubt, i want the admin to be able to show all customers in a list by name/customer number (account)/ amount debt (owes)
    in this case i have to read all objects to an array, right?
    Si vis pasem para bellum
    -Vagecio

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Exclamation Work awhile, then ask a question or two.

    Quote Originally Posted by Goodwine View Post
    this is a simple school-project so i hope someone can help me
    basically i just need to solve some doubts about using files
    Ok.
    Quote Originally Posted by Goodwine View Post
    Well, my program is an abstract class User which is extended by Customer and Admin classes, then i want to write customer and admin objects on a file for each kind
    Ok, but we don't do homework verbatim. Gladly help on what to think about or what to look for.
    Quote Originally Posted by Goodwine View Post
    first doubt, can i put a method in User to write objects on files to save code typing on the other classes?, like
    Yes, very simple idea here - and it will work.
    Quote Originally Posted by Goodwine View Post
    an example will be on Customer add a method like
    A little more details, more work,... still basic answer is yes. Just a matter of study, practice and reading various explainations.
    Quote Originally Posted by Goodwine View Post
    Supposing i can, or even if i cant and have to write it twice, do i need to use the constructor of FileOutputStream(String, boolean) so i can add objects, or what should i do instead of that?
    Constructors, then methods, thus:
    Java Code:
    class FirstClass
    {
        SecondClass variable = new SecondClass();//
        variable.doSomething();//
    }
    Which for file classes often take a String name for the file name but it gets dicey just to grab one and code, be prepared to study or go ahead and fill application to get old job back flippin burgers. I pushed Pizza, did better than at Burgers 'N Stuffit.
    Quote Originally Posted by Goodwine View Post
    also, Customer has an "account" field which is an integer, and if i write many customers, how do i look for specifically one according to that field?
    (the admin has a String "account" field but i guess it would be similar)
    read in the customers and numbers and so on from a file, start with a short + simple array in the class to resemble a file. Go to bigger and better later.

    You can start with an OO approach, just give names to whatever you can think of then write class Customer{} and so on .... stuff variables in each as you think of what matters.

    Quote Originally Posted by Goodwine View Post
    another doubt is... when i want to modify a customer's fields, supposing he has a float field called "owes" which indicates how much money does he owe to the store, the the customer pays and now i have to reduce the "owes" by the amount that was paid,
    i will first look for the Customer object on the file the open it and then edit it and then save it again
    but how do i replace that customer for the new one?
    That is what the work is all about. Work awhile, then ask a question or two.

    Quote Originally Posted by Goodwine View Post
    and if i want to delete a customer forever? what should i do?
    Set a field ( member variable ) to some value. Never delete due to issues in the real world. Somebody delete something, Head Moe In Charge need to know who delete and why. Usually but not always, start with negative one to signifiy Deletions, Backpedalling, Backflow and other Blindside work. Give special handling to anything with (-1) value. Use zero to signifiy Blank Customer, no name, no history, un-used Customer number and so on.
    Quote Originally Posted by Goodwine View Post
    last doubt, i want the admin to be able to show all customers in a list by name/customer number (account)/ amount debt (owes)
    in this case i have to read all objects to an array, right?
    Pretty much, figure out how to get a file open first. Before that, just do:
    Java Code:
    Customer[] customerArray = {"Bill","Boe","Moe"};
    Which will actually be strings at first. When we move to an acutal Customer class as I supposedly coded here we can promote this simple sample to working code.

    You will get a lot of people telling you to think, but they won't tell you what to think about. Deal with it, we had to.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Goodwine's Avatar
    Goodwine is offline Member
    Join Date
    Nov 2008
    Location
    Mexico
    Posts
    14
    Rep Power
    0

    Default

    I do have the program, But I just don't get how to read files, I'll try what you suggested me to.
    btw, i wanted to post the program but it had some Spanish words, so it might have been confusing, if i got any doubt I'll post the part of the code and if necessary, a slight translation
    Si vis pasem para bellum
    -Vagecio

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Talking reading files

    Skip the translation of a few habla espanola in fora, we get people from all nations and languages - just don't overdo it.
    Java Code:
    import java.io.*;//
    public class SampleFileReader
    {
        //escribir archivos del equipo
        BufferedReader  fileOne;
        SampleFileReader(String filename)
        {
            fileOne = new BufferedReader(new InputStreamReader(filename));//
        }
        String ReadFile() throws IOException
        {
            //  Super simple demo, .....
            String placeToReadFileTo = fileOne.readLine();
            return placeToReadFileTo;
        }
        /**
        * Reads weather data for Ciudad Guzman, Mexico
        * File probably should be there, we can write
        * files with Java just as we can read them.
        * This approach is minimal and reads strings.
        */
        public static void main(String[] args)
        {
            try
            {
                //
                SampleFileReader sfr = new SampleFileReader("CiudadGuzman.dat");//
                System.out.println(sfr.ioe.ReadFile());
            }
            catch(IOException ioe)
            {
                System.out.println(ioe.getMessage());
            }
        }
     }
    Much work available depending on what you want to do while before moving on.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    Goodwine's Avatar
    Goodwine is offline Member
    Join Date
    Nov 2008
    Location
    Mexico
    Posts
    14
    Rep Power
    0

    Default

    I kinda solved it with this (because it told me something about "this is a superclass object blablabla not compatible with the subclass object")
    Java Code:
    public void escribir(String fileName, Object objeto)
    	{
    		try
    		{
    			ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(fileName, true));
    			objOut.writeObject(objeto);
    			objOut.close();
    		}
    		catch (Exception e)
    		{
    			System.err.println( "Error al crear usuario." );
    			e.printStackTrace();
    		}
    	}
    I just need to make some validations so if a user already exists, it doesn't duplicate, but that another case

    and I tested it this way

    Java Code:
    public static void main (String[] args)
    	{
    		Master a = Master();
    		a.escribir() //returns escribir("Masters.dat", Master);
    
    		Master yo;
    		ObjectInputStream in;
    		try
    		{
    			in = new ObjectInputStream(new FileInputStream("Masters.dat"));
    			yo = (Master)in.readObject();
    			JOptionPane.showMessageDialog (null, yo.getInfo (), "Cliente: " + yo.getCuenta (), JOptionPane.PLAIN_MESSAGE);
    			in.close();
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    Thanks for helping btw :DD
    Last edited by Goodwine; 11-18-2008 at 04:07 AM.
    Si vis pasem para bellum
    -Vagecio

Similar Threads

  1. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM
  2. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  3. Writing to files within jar files
    By erhart in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2008, 02:50 AM
  4. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 04:43 AM
  5. how to convert mpeg files to .wav files
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •