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 4 of 4

Thread: PrintStream Help!

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb PrintStream Help!

    I want to create a simple console account application that allow's you to create accounts and the accounts will be save in a txt files. Code:
    package main;
     
    import static java.lang.System.out;
     
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    public class mainClass {
     
            public static void main(String[] args) throws FileNotFoundException {
                    Scanner sn = new Scanner(new File("Usernames.txt"));
                    Scanner s = new Scanner(System.in);
                    newAccount a = new newAccount();
                    int ID = 0;
     
                    while(sn.hasNext()){
                            ID = ID + 1;
                            sn.nextLine();
                    }
                    do{
            out.print("Enter username: ");
            a.user = s.nextLine();
            out.print("Enter password: ");
            a.password = s.nextLine();
            a.ID = ID;
            a.print();
            out.println();
            out.print("Create another account?");
                    }while(s.findWithinHorizon(".", 0).charAt(0) == 'y');
     
            }
     
    }
     
    //Method class
     
    package main;
     
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
     
    public class newAccount {
     
            String user;
            String password;
            int ID;
     
            void print() throws FileNotFoundException{
     
                PrintStream u = new PrintStream("Usernames.txt");
                    PrintStream p = new PrintStream("Passwords.txt");
     
     
                    for(int i = 0; i < ID; i++){
                            p.println();
                            u.println();
                    }
            u.println(user);
            p.println(password);
            }
    }
    My problem: Every time a new account is created all previous accounts will be deleted because I use: u.println(""); and p.println(""); How can I fix this problem? Thanks!
    Last edited by Norm; June 22nd, 2014 at 11:53 AM. Reason: change quote to code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: PrintStream Help!

    Please post the code here that you are having problems with.

    Is this the problem: when adding a new record to an existing file, the old file is overwritten instead of the new record added to the end?
    Look at using the file writing class's constructor with the append option.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    DayzZombie (June 22nd, 2014)

  4. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: PrintStream Help!

    Yes that is the problem, and can you give me an example of the file append option? Thanks for the reply!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: PrintStream Help!

    Use the class that has the append option in the class's constructor. Set it to true.
    I don't know the name of the class right now. It might be FileWriter. Look in the API doc.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. PrintStream, strings and reading from a file.
    By taze in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 1st, 2013, 08:50 PM
  2. PrintStream not printing in file
    By rrahulvverma in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: November 2nd, 2010, 05:32 PM