因此,最近,我父親要求我做一個涉及收集用戶名,密碼等的項目,以便他有一個設置位置來查找他所有的登錄信息。如何在Java的不同行上多次寫入文本文檔?
我有一個程序可以成功收集信息,並且每次運行程序時都會寫入一個新的文本文件。我的目標是讓信息存儲在同一個文件中,但每一組信息之間都有一個空格。
這裏是我的代碼(我很抱歉,我只是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);
}
}
}
}
可能的[Java文件 - 打開文件並寫入它]的副本(http://stackoverflow.com/questions/10667734/java-file-open-a-file-and - 寫到它) – sab669
我明白這可能是一個學習練習,但是當它來到t o存儲密碼,我真的會建議像KeePass。如果密碼是純文本文件,黑客也可以方便地將所有內容都放在一個地方(如家庭網上銀行密碼)。 –
[PrintWriter append method not appending]可能重複(http://stackoverflow.com/questions/8210616/printwriter-append-method-not-appending) –