Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Print Writer Problem

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Print Writer Problem

    I need help on figuring out how to send data to a file without it replacing the data and also repeating other data. The code below doesnt replace any darta, but it repeats data that i want in there (which i dont want replacing):

     public void writeCustomerData()
     
        {
            try{
            Frame owner = null; //Sets the owner of the file to null.
            FileDialog newFileDialog1 = new FileDialog(owner, "Open", FileDialog.SAVE); //Creates a fileDialog object.
            newFileDialog1.setVisible(true); //Chooses the file that will be read in.
            File newFile1 = new File(newFileDialog1.getFile());
     
            if(customerMap.size()> 0)
            {
     
              for(Customer customers : customerMap.values())
                {
                 PrintWriter nw = new PrintWriter(new FileWriter(newFile1,true)); 
                 nw.print("ok");
                  customers.writeData(nw);
                  nw.close();
                }
            }
            else
            {
                System.out.println("No Customers In The List");
            }
        }
        catch(FileNotFoundException a)
        {
            System.out.print("File cannot be found");
        }
        catch(IOException a)
        {
            System.out.print("File cannot be found");
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print Writer Problem

    To keep from writing data to a file that is already in the file, the code will have to read the file, examine the data and select what data should be written out to the file and replace what is currently in the file with the new version of the data.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Print Writer Problem

    well the data i want is in a map , but i need to have that in a loop to get them all , but have other data not repeat where it says ok i need that only once!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print Writer Problem

    If you don't want some data to be in the file, the code will need to determine what data should not be written to the file.

    How can the program look at all the data that could be written to the file and select the data that should be written to the file and write it and not write the data it does not want written to the file?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Print Writer Problem

    i need one piece of data to write once , followed by all data in a hashmap with out no replacing of the data beause all the data append each other

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print Writer Problem

    Can you use a Set to hold the data and keep the data from being repeated?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Print Writer Problem

    it has to be a hash map cz i will have to be able to get them later via the key. is there a way of doing it the way i want . n its not the map thats repeating its the other information.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print Writer Problem

    What is the "other information" that is repeating? How is it related to what is in the disk file?

    If you want unique data values written to the file, put the data in a Set before writing it to the file. That will remove duplicates.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Buferred Writer in TextFile
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2013, 02:37 AM
  2. Read Writer button wont add to file.
    By miller4103 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2013, 08:08 AM
  3. [SOLVED] Coin Loop Selective Print Statement Problem
    By sternfox in forum Loops & Control Statements
    Replies: 1
    Last Post: January 30th, 2013, 12:30 AM
  4. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  5. [SOLVED] Buffered writer in a .jar file
    By lorider93p in forum Java Theory & Questions
    Replies: 8
    Last Post: February 8th, 2012, 01:03 PM