简体   繁体   中英

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. I am still learning the basic information, and have a general understanding of the basics, such as JOptionPane, Scanner, Array and system lines. 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. 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. 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
  • 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.

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. With the Scanner class you can read from and to file. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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