简体   繁体   English

程序终止后,是否可以保存输入信息?

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

I'm a little new to java, having just started a high-school level class in August. 我是Java的新手,八月份才刚开始上高中课程。 I am still learning the basic information, and have a general understanding of the basics, such as JOptionPane, Scanner, Array and system lines. 我仍在学习基本信息,并对基本知识有大致的了解,例如JOptionPane,Scanner,Array和系统行。 My question is if there is a way to create a standalone Java program, where you can just double click an icon on the desktop to launch it. 我的问题是是否可以创建一个独立的Java程序,您可以双击桌面上的一个图标来启动它。 If so, is there a way to create a file dump where you can only access it by a password? 如果是这样,有没有办法创建只能通过密码访问它的文件转储? I already have a basic login program that allows access based on a predetermined user and password. 我已经有一个基本的登录程序,该程序允许基于预定的用户和密码进行访问。 The goal of this program is to create a secure (even though it's basic) file share between people who are meant to have it and keep out those who aren't, that contains specific files (.docx, .jpeg, .pptx, etc.) that are utilized for school work. 该程序的目标是在打算拥有该文件的人之间创建安全的(即使是基本的)文件共享,并禁止那些包含特定文件(.docx,.jpeg,.pptx等)的文件共享。 。)用于学校作业。 If so, is there also a way to limit access to these files to being only accessed through this program? 如果是这样,是否还有办法将对这些文件的访问限制为只能通过此程序访问?

This is the code I have so far: 这是我到目前为止的代码:

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    

    }

}

This is just the login screen for the program, but I just want to be able to: 这只是该程序的登录屏幕,但我只希望能够:

  • Edit the user or password by choice, such as enter old pass: now new: repeat: and saving the new password. 通过选择编辑用户或密码,例如输入旧密码:现在新密码:重复密码:并保存新密码。
  • Create a file dump only accessed through this program containing basic files such as docx, pptx, etc 创建仅通过包含基本文件(例如docx,pptx等)的此程序访问的文件转储
  • How to edit the Username, password, or add new users to the program, only by having an admin password. 仅通过拥有管理员密码才能编辑用户名,密码或向程序添加新用户。
  • If I have the only account to this program and someone else wants one, the lines reading would be: 如果我拥有该程序的唯一帐户,而其他人想要一个帐户,则读取的行将是:

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

    and it then creates a permament account. 然后创建一个永久账户。

Sorry for the unorthodox questions, but I am extremely curious, as well as new to these boards. 很抱歉提出了非正统的问题,但我非常好奇,而且对这些委员会还是陌生的。 I just want to know if it's possible with java, and if so, where I could reference the material to learn/teach myself. 我只想知道java是否有可能,如果可以,我可以在哪里参考资料来学习/自学。

Yes, it is possible, but there are too many things to answer in one post I think. 是的,这是可能的,但我认为一篇文章中有太多要回答的问题。

What you should look into is the Scanner class in the Java Core Library. 您应该查看的是Java Core Library中的Scanner类。 With the Scanner class you can read from and to file. 使用Scanner类,您可以读取和读取文件。 Search here for some tips. 在此处搜索一些提示。 With Scanner you can thus have a file with the password written (keep the file protected), and you can change the password by rewriting the password on the file (and reading from the same file). 因此,使用Scanner,您可以拥有一个写入了密码的文件(保持文件受保护),并且可以通过在文件上重写密码(并从同一文件读取)来更改密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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