0

So, recently, my father asked me to make a project that involves gathering usernames, passwords, etc., so that he would have a set location to find all of his logins.

I have a program that successfully gathers information and writes a new text file every single time you run the program. My goal is to make it so the information is stored in the SAME file, but there is a space between each set of information.

Here's my code (I apologize, I'm only 14):

package com.src.java;

import java.io.IOException;
import java.io.PrintWriter;

import java.net.MalformedURLException;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class Info {

    public static void main(String args[]) throws MalformedURLException,
            IOException, InterruptedException {
        boolean run = true;
        while (run) {
            JTextField companyName = new JTextField();
            JTextField userName = new JTextField();
            JTextField password = new JTextField();
            JTextField accountNumber = new JTextField();
            JTextField phoneNumber = new JTextField();
            JTextField address = new JTextField();

            UIManager.put("OptionPane.cancelButtonText", "Exit");
            UIManager.put("OptionPane.okButtonText", "Create File");
            Object[] message = { "Company name: ", companyName, "Username:",
                    userName, "Password:", password, "Account number:",
                    accountNumber, "Phone number:", phoneNumber, "Address:",
                    address, };
            int option = JOptionPane.showConfirmDialog(null, message,
                    "Fill out your info...", JOptionPane.OK_CANCEL_OPTION);
            if (option == JOptionPane.OK_OPTION) {
                String cN = companyName.getText();
                String uN = userName.getText();
                String pw = password.getText();
                String aN = accountNumber.getText();
                String pN = phoneNumber.getText();
                String ad = address.getText();

                PrintWriter writer = new PrintWriter(cN + ".txt", "UTF-8");

                writer.println("Company name: " + cN);
                writer.println("User name: " + uN);
                writer.println("Password: " + pw);
                writer.println("Account number: " + aN);
                writer.println("Phone number: " + pN);
                writer.println("Address " + ad);
                writer.close();

                System.out.println("Wrote file: " + cN + ".txt");
                Thread.sleep(500);
            } else {
                System.exit(0);
            }
        }
    }
}
3
  • 1
    possible duplicate of Java File - Open A File And Write To It Commented Oct 18, 2013 at 20:51
  • I understand this is probably a learning exercise, but when it comes to storing passwords, I would really suggest something like KeePass. If the passwords are in a plain text file, a hacker also conveniently has everything in one place (like the family online banking password). Commented Oct 18, 2013 at 20:54
  • 1
    possible duplicate of PrintWriter append method not appending Commented Oct 18, 2013 at 20:57

2 Answers 2

1

The filename you're writing to changes with whatever the name of the company is that's entered. Hardcode the filename in.

Also, set append mode to true...

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to write to the same file, you can open it up in append mode, so

new PrintWriter(new FileOutputStream(new File(filename),true));

The true opens the file in append mode

3 Comments

Okay, thanks! However, when you do set the file, will it write the info in a different line?
Yes, each println will print to a separate line. You can print as many blank lines as you want too.
Not a problem - happy to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.