簡體   English   中英

程序終止后,是否可以保存輸入信息?

[英]Is there a way to save input information after the program terminates?

我是Java的新手,八月份才剛開始上高中課程。 我仍在學習基本信息,並對基本知識有大致的了解,例如JOptionPane,Scanner,Array和系統行。 我的問題是是否可以創建一個獨立的Java程序,您可以雙擊桌面上的一個圖標來啟動它。 如果是這樣,有沒有辦法創建只能通過密碼訪問它的文件轉儲? 我已經有一個基本的登錄程序,該程序允許基於預定的用戶和密碼進行訪問。 該程序的目標是在打算擁有該文件的人之間創建安全的(即使是基本的)文件共享,並禁止那些包含特定文件(.docx,.jpeg,.pptx等)的文件共享。 。)用於學校作業。 如果是這樣,是否還有辦法將對這些文件的訪問限制為只能通過此程序訪問?

這是我到目前為止的代碼:

import javax.swing.JOptionPane.*;
import java.lang.Math.*;
import java.lang.System.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.JFrame;

public class UserLog extends JFrame  

{

public static void main(String[]Args) throws InterruptedException 
    {
    boolean isValid=false;
    while(!isValid)
        {
    // Components related to "login" field    
    JLabel label_loginname = new JLabel("Enter your login name:");    
    JTextField loginname = new JTextField(15);    
    // loginname.setText("EnterLoginNameHere"); 
    // Pre-set some text    
    // Components related to "password" field    
    JLabel label_password = new JLabel("Enter your password:");    
    JPasswordField password = new JPasswordField();    
    // password.setEchoChar('@'); 
    // Sets @ as masking character    
    // password.setEchoChar('\000'); 
    // Turns off masking    
    JCheckBox rememberCB = new JCheckBox("Remember me");

    Object[] array = {label_loginname,
    loginname,                       
    label_password,                       
    password,                       
    rememberCB};
    Object[] options = {"Login", "Cancel"};
    int res = JOptionPane.showOptionDialog(null,
                                            array,
                                            "Login",
                                            JOptionPane.YES_NO_OPTION,
                                            JOptionPane.QUESTION_MESSAGE,
                                            null,     //do not use a custom Icon
                                            options,  //the titles of buttons
                                            options[0]); //default button title

    // User hit Login    
    if (res == 0) 
        { 
            System.out.println( "Login" ); 
        }    
    // User hit CANCEL    
    if (res == 1) 
        { 
            System.out.println( "Canceled" ); 
        }    
    // User closed the window without hitting any button    
    if (res == JOptionPane.CLOSED_OPTION) 
        { 
            System.out.println( "CLOSED_OPTION" ); 
        }


    // Output data in "login" field, if any    
    String newloginname = loginname.getText();    
    String newpassword = new String(password.getPassword());    
    if (newloginname.equalsIgnoreCase("Cody_Coulter") && newpassword.equals("cheche1"))
        {
            System.out.println("Login Successful!");
            boolean selectedCB = rememberCB.isSelected();    
            System.out.println( "selectedCB: " + selectedCB );
            Thread.sleep(3000);
            Object[] array1= {"It's about time to choose"};
            Object[] options1= {"Leave", "Keep Going"};
            int res1 = JOptionPane.showOptionDialog(null,
                                            array1,
                                            "There",
                                            JOptionPane.YES_NO_OPTION,
                                            JOptionPane.QUESTION_MESSAGE,
                                            null,     //do not use a custom Icon
                                            options1,  //the titles of buttons
                                            options1[0]); //default button title
            if(res1==1)
                {
                    String name1 = JOptionPane.showInputDialog(null,
                                                        "What is your name?");
                    int length = 0;
                    length = newpassword.length();
                    String Pass = "*";
                    newpassword =newpassword.replaceAll(".","*");
                    System.out.println("Username: "+newloginname+"\nPassword: "+
                                        newpassword+"\nName: "+name1);
                }

        }
    else {
            JOptionPane.showMessageDialog(null,"Wrong Username or Password!");
            isValid=false;
         }
        }
    // Output data in "password" field, if any    
    // Output state of "remember me" check box    

    }

}

這只是該程序的登錄屏幕,但我只希望能夠:

  • 通過選擇編輯用戶或密碼,例如輸入舊密碼:現在新密碼:重復密碼:並保存新密碼。
  • 創建僅通過包含基本文件(例如docx,pptx等)的此程序訪問的文件轉儲
  • 僅通過擁有管理員密碼才能編輯用戶名,密碼或向程序添加新用戶。
  • 如果我擁有該程序的唯一帳戶,而其他人想要一個帳戶,則讀取的行將是:

     Username: _____ Password: _____ Retype Password:_____ Authentication: _____ 

    然后創建一個永久賬戶。

很抱歉提出了非正統的問題,但我非常好奇,而且對這些委員會還是陌生的。 我只想知道java是否有可能,如果可以,我可以在哪里參考資料來學習/自學。

是的,這是可能的,但我認為一篇文章中有太多要回答的問題。

您應該查看的是Java Core Library中的Scanner類。 使用Scanner類,您可以讀取和讀取文件。 在此處搜索一些提示。 因此,使用Scanner,您可以擁有一個寫入了密碼的文件(保持文件受保護),並且可以通過在文件上重寫密碼(並從同一文件讀取)來更改密碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM