Java readObject每次都返回相同的对象
我正在使用ObjectOutputStream根据需要将对象写入文件。但是,我只能读取写入文件的第一个对象,即使文件中添加了几个对象。这意味着它只是循环,因为用户对象从不为null 我确信这是否是将对值保存到文本文件的正确方法,因为我必须使用新对定期更新文件。有没有更好的办法 这是对象类Java readObject每次都返回相同的对象,java,file,object,objectinputstream,Java,File,Object,Objectinputstream,我正在使用ObjectOutputStream根据需要将对象写入文件。但是,我只能读取写入文件的第一个对象,即使文件中添加了几个对象。这意味着它只是循环,因为用户对象从不为null 我确信这是否是将对值保存到文本文件的正确方法,因为我必须使用新对定期更新文件。有没有更好的办法 这是对象类 package com.example.testaware.offlineAuth; import java.io.Serializable; public class AuthenticatedUser
package com.example.testaware.offlineAuth;
import java.io.Serializable;
public class AuthenticatedUser implements Serializable {
public String ipAddress;
public String pubKey;
public AuthenticatedUser(){
}
public AuthenticatedUser(String ipAddress, String pubKey){
this.ipAddress=ipAddress;
this.pubKey=pubKey;
}
public String getIpAddress() {
return ipAddress;
}
public String getPubKey() {
return pubKey;
}
public void setIpAddress(String ipAddress){
this.ipAddress=ipAddress;
}
public void setPubKey(String pubKey){
this.pubKey=pubKey;
}
}
写和读这些东西
package com.example.testaware.offlineAuth;
import android.util.Log;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.PublicKey;
import java.util.HashMap;
/*Methods used to verify user that server wants to connect to - mappings of pub key and mac/Ip- after done challenge response*/
public class VerifyUser {
private static String LOG = "LOG-Test-Aware-Verify-User";
// Used to check key provided and IP of connected client
public static boolean isAuthenticatedUser(String peerIP, String key){
String thisLine =null;
boolean cont= true;
boolean isAuthenticated=false;
while (cont) {
try {
FileInputStream fileIn = new FileInputStream("/data/data/com.example.testaware/authenticatedUsers.txt");
ObjectInputStream in = new ObjectInputStream(fileIn);
AuthenticatedUser user = (AuthenticatedUser) in.readObject();
String ip = user.getIpAddress();
String clientKey= user.getPubKey();
if(user!=null) { //TODO:find out why it only checks first object?
if (ip.equals(peerIP) && clientKey.equals(key)) {
isAuthenticated = true;
in.close();
fileIn.close();
Log.i(LOG, "Successfully read key and ip to the file.");
cont = false;
}
}
else{
in.close();
fileIn.close();
System.out.println("No users authenticated in file");
Log.i(LOG, "No users authenticated in file");
}
} catch (IOException | ClassNotFoundException e) {
System.out.println("An error occurred. ");
e.printStackTrace();
cont= false;
}
}
return isAuthenticated;
}
//used to set map key and IP of new users , after challenge response //TODO: call this somewhere
public static void setAuthenticatedUser( String connectedPeerIP,String connectedPeerKey) {
AuthenticatedUser user= new AuthenticatedUser(connectedPeerIP,connectedPeerKey);
try{
FileOutputStream fileOut =
new FileOutputStream("/data/data/com.example.testaware/authenticatedUsers.txt",true);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(user);
out.close();
fileOut.close();
Log.i(LOG, "Successfully wrote key and ipaddr to the file.");
} catch (IOException i) {
System.out.println("An error occurred.");
i.printStackTrace();
}
}
}
```
您打开文件,读取一个对象,然后关闭它,因此您自然只读取第一个对象。我的“有限”搜索似乎表明,不支持或不容易支持将对象附加到文件中。您需要将所有内容作为原子工作流进行读取、更新和重写。在一些实验和阅读文章之后,阅读代码也有一些问题,比如,为了将多个对象存储到一个文件中,您应该使用某种类型的
列表。这将改变您的工作流程,因为您需要在内存中维护一个列表
,并在写入文件之前对其进行更新谢谢您的回复。我想我会尝试使用@MadProgrammer建议的列表。我会给你更新,看看是否有效。