我制作了一个小的 Java 程序,它要求一个人输入个人识别码。一旦输入密码,它就会读入存储所有密码的“bdd.txt”文件,然后显示 :) 如果正确,则 :( 如果错误。到目前为止的简单应用。
我想做的是将该“数据库”文件移动到我计算机上的虚拟机(例如 Ubuntu)中,然后执行相同的操作。这样,它就不再是本地文件了,因为该文件将不再位于我的项目的根目录下。
这是我的应用程序的样子:
如您所见,应用程序启动后,要求用户输入个人识别码。如果这是一个好的,应用程序就完成了,如果不是,他还有 2 次尝试,直到应用程序停止。
输入 pin 后,我的程序会检查“bdd.txt”是否存在 pin。它扮演数据库角色:
要了解我的需要,有必要将此程序与需要安全的东西相提并论。我们不希望 pin 数据库与程序(或现实生活中的设备)位于同一位置。所以我们把它放在虚拟机上,我们必须在 Eclipse 中的 Windows7 Java 程序和 VMWare Player 的 Ubuntu 上的 bdd.txt 文件之间进行通信。
我的问题是这怎么可能?我需要如何更改我的代码才能让我的程序到达我的 VM 上的某些内容?是否有我应该使用的特定技术?我需要先做一些配置吗?
这是我的代码:
import java.io.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel container = new JPanel();
private JPasswordField p1 = new JPasswordField(4);
private JLabel label = new JLabel("Enter Pin: ");
private JButton b = new JButton("OK");
public Main() {
this.setTitle("NEEDS");
this.setSize(300, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
container.setBackground(Color.white);
container.setLayout(new BorderLayout());
container.add(p1);
JPanel top = new JPanel();
PlainDocument document =(PlainDocument)p1.getDocument();
b.addActionListener(new BoutonListener());
top.add(label);
top.add(p1);
p1.setEchoChar('*');
top.add(b);
document.setDocumentFilter(new DocumentFilter(){
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
String string =fb.getDocument().getText(0, fb.getDocument().getLength())+text;
if(string.length() <= 4)
super.replace(fb, offset, length, text, attrs); //To change body of generated methods, choose Tools | Templates.
}
});
this.setContentPane(top);
this.setVisible(true);
}
class BoutonListener implements ActionListener {
private final AtomicInteger nbTry = new AtomicInteger(0);
ArrayList<Integer> pins = readPinsData(new File("bdd.txt"));
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
if (nbTry.get() > 2) {
JOptionPane.showMessageDialog(null,
"Pin blocked due to 3 wrong tries");
return;
}
final String passEntered=p1.getText().replaceAll("\u00A0", "");
if (passEntered.length() != 4) {
JOptionPane.showMessageDialog(null, "Pin must be 4 digits");
return;
}
//JOptionPane.showMessageDialog(null, "Checking...");
//System.out.println("Checking...");
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
boolean authenticated = false;
if (pins.contains(Integer.parseInt(passEntered))) {
JOptionPane.showMessageDialog(null, ":)");
authenticated = true;
}
if (!authenticated) {
JOptionPane.showMessageDialog(null, ":(");
nbTry.incrementAndGet();
}
return null;
}
};
worker.execute();
}
}
//Function to read/access my bdd.txt file
static public ArrayList<Integer> readPinsData(File dataFile) {
final ArrayList<Integer> data=new ArrayList<Integer>();
try {
BufferedReader reader = new BufferedReader(new FileReader(dataFile));
String line;
try {
while ((line = reader.readLine()) != null) {
try {
data.add(Integer.parseInt(line));
} catch (NumberFormatException e) {
e.printStackTrace();
System.err.printf("error parsing line '%s'\n", line);
}
}
} finally {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("error:"+e.getMessage());
}
return data;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Main();
}
});
}
}
有什么想法吗?谢谢,
佛罗伦萨。
最佳答案
共享文件夹当然可以,但拥有 VM 似乎毫无意义,因为 PIN 文件也在您的主机上,而 java 正在直接读取它。
也许您需要客户端/服务器架构?
您使用 UI 编程将成为客户端。客户端将配置调用服务器的方式(IP 地址和端口)。客户端无法访问 bdd.txt 文件,但服务器可以。
在您的 VM 上,您有另一个 Java 应用程序,即服务器。您的服务器监听来自客户端的请求。该请求将包含用户输入的 PIN。服务器然后根据文件中的 PIN 对其进行检查,并以是或否进行响应。您的客户端从服务器接收是/否响应,并将结果报告给用户。
阅读有关套接字编程的信息 here开始
关于java - 如何在 Java 中与虚拟机通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22641097/