简体   繁体   中英

Writing to a file with printwriter

Hi I'm a beginner to file I/O and I'm having a small problem with writing to a file. What my program should do is write a username and password to a file. Here's my code (ill describe my problem after the code because its specific to program):

public class Helper {

    public static void main(String[] args) throws Exception {

        Home();
        UserInput();
    }

    private static void Home() throws FileNotFoundException{
        System.out.println("Here are the instrucions for the chat program:");
        System.out.println("Type *R for registration" );
    }

    private static void UserInput() throws Exception{
        Scanner scan = new Scanner(System.in);
        String input = scan.next();

        if(input.equals("*R")){
            Register(); 
        }
        main(new String[0]);
    }

    private static void Register() throws FileNotFoundException{
        try{
            File info = new File("info.txt");
            info.createNewFile();
            FileOutputStream outputStream = new FileOutputStream(info);
            PrintWriter out = new PrintWriter(outputStream);
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter a username: ");
            String username = scan.nextLine();
            System.out.println("Enter a password: ");
            String password = scan.nextLine();
            out.println(username + " " + password);

            out.flush();


        }catch(IOException e){
            e.printStackTrace();
        }

    }

What I need is my info.txt file to store all of the usernames and passwords each pair on a different line, however it only stores the most recent one. That is, each time I write to info.txt it overwrites the most recent pair(username and password). Is there a way around this?

Use this constructor instead. new FileOutputStream(info,true);

Java FileWriter constructor is called like this:

new FileWriter(String s, boolean append);

This simple constructor indicates that you want to write to the file in append mode.

Try following code:

private static void Register() throws FileNotFoundException{
        try{            

            FileWriter fw = new FileWriter("info.txt", true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter out = new PrintWriter(bw);           


            Scanner scan = new Scanner(System.in);
            System.out.println("Enter a username: ");
            String username = scan.nextLine();
            System.out.println("Enter a password: ");
            String password = scan.nextLine();
            out.println(username + " " + password);

            out.flush();


        }catch(IOException e){
            e.printStackTrace();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM