使用ObjectInputStream迭代文件

时间:2017-06-17 17:19:23

标签: java

我不能多次使用这3个字符串迭代一个文件。我只能在第一次循环迭代中得到这个字符串。

使用此方法,我检查用户名和密码是否与存储在文件中并使用Caesar Cipher编码的数据相等

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">
//...
</web-app>

我在这样的文件中写数据:

private boolean confirmCredentials(String username, String password) throws ClassNotFoundException {
    ObjectInputStream ois = null;
    boolean confirm = false;
    try {
        ois = new ObjectInputStream(new FileInputStream(loginDataPath));
        do {
            String username2 = (String) ois.readObject();
            String password2 = (String) ois.readObject();
            String localShift = (String) ois.readObject();

            String s = CaeserCipher.decode(localShift, shift);
            String p = CaeserCipher.decode(password2, Integer.parseInt(s));
            confirm = username2.equals(username) && p.equals(password);
        } while(!confirm);
    } catch (ClassNotFoundException e) {
        throw e;
    } catch(IOException e) {
    } finally {
        try {
            ois.close();
        } catch(IOException e) {
        }
    }
    return confirm;
}

1 个答案:

答案 0 :(得分:-1)

嗯......你认为你需要多少次阅读文件以及实际上是什么(你没有指定)?在我看来,当用户名与从文件中读取的内容匹配而不是解码每个密码并遇到 localShift 时,您的解密应该仅处理

如果密码确认失败,则继续阅读,直到文件中遇到另一个同名实例(如果有):

public typealias DataElement = OneDimensionalDataPoint