如何将数据保存到文件并在运行程序时检查它?

时间:2017-06-12 09:56:27

标签: java file bufferedreader

我正在尝试编写一个基本密码管理应用程序,以便将所有密码保存到其中。我是编程的新手,所以我遇到了很多障碍。我需要能够检查用户是否已经拥有帐户,如果用户没有帐户,则应该让他们创建一个新帐户。

例如,在文件中,如果他们没有帐户,那么它将是HasAccount = 0,如果他们有帐户,那么HasAccount = 1我会让它读取和写入字符串,我我现在肯定如何保存值或布尔值。

这是我的写作课

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

public class WriteFile {

    private String path;
    private boolean append_to_file = false;

    public WriteFile(String file_path) {
        path = file_path;

    }
    public WriteFile( String file_path, boolean append_value) {
        path = file_path;
        append_to_file = append_value;
    }
    public void writeToFile(String textLine) throws IOException {
        FileWriter write = new FileWriter(path, append_to_file);
        PrintWriter print_line = new PrintWriter(write);

        print_line.printf("%s" + "%n", textLine);
        print_line.close();
        }

}

这是我的阅读课。

import java.io.IOException;

import java.io.FileReader;
import java.io.BufferedReader;

public class ReadFile {
    private String path;

    public ReadFile(String file_path) {
        path = file_path;
    }
    public String[] OpenFile() throws IOException {
        FileReader fr = new FileReader(path);
        BufferedReader textReader = new BufferedReader(fr); 

        int numberOfLines = readLines();
        String[] textData = new String[numberOfLines];

        int i;

        for (i=0; i < numberOfLines; i++) {
            textData[ i ] = textReader.readLine();

            }
        textReader.close();
        return textData;

    }

    int readLines() throws IOException {

        FileReader file_to_read = new FileReader(path);
        BufferedReader bf = new BufferedReader(file_to_read);

        String aLine;
        int numberOfLines = 0;

        while (( aLine = bf.readLine()) != null) {
            numberOfLines++;
        }
        bf.close();

        return numberOfLines;
    }

        }

如果他们有帐户,应该让他们输入。

Scanner input = new Scanner(System.in);
    String MainUsername;
    System.out.print("Please enter your username: ");
    MainUsername = input.next();
    profile = 1;

    String MainPassword;
    System.out.print("Please enter your password: ");
    MainPassword = input.next();

0 个答案:

没有答案