Java 如何将txt文件读入字节[],将字节[]读入Hashmap<;字符串,对象>;?
我的java代码有问题。任何人都知道这个问题。我试图在txt文件中保存我的hashmap,然后将txt文件读取到hashmap,但它不起作用。我想我成功地保存了hashmap,但我无法读取它。你们能帮我查一下密码吗Java 如何将txt文件读入字节[],将字节[]读入Hashmap<;字符串,对象>;?,java,hashmap,byte,Java,Hashmap,Byte,我的java代码有问题。任何人都知道这个问题。我试图在txt文件中保存我的hashmap,然后将txt文件读取到hashmap,但它不起作用。我想我成功地保存了hashmap,但我无法读取它。你们能帮我查一下密码吗 Client_Database database = new Client_Database(); String filename = "C:\\bookCafeDatabase.txt"; File file = ne
Client_Database database = new Client_Database();
String filename = "C:\\bookCafeDatabase.txt";
File file = new File(filename);
byte [] contents = new byte[(int)file.length()];
database.setFileContents(contents);
HashMap<String, Client_Database> user_map = new HashMap<>();
user_map.put(database.setIdDB(Client_Signin.idMsg), database);
try{
ObjectOutputStream bos =
new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(file,true)));
bos.writeObject(user_map);
bos.flush();
bos.close();
ObjectInputStream bis =
new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(file)));
bis.read(contents);
bis.close();
}catch (Exception e1) {
}
}
}
});
}
Client_Database=new Client_Database();
字节[]内容=新字节[(int)file.length()];
数据库.setFileContents(目录);
试一试{
FileOutputStream f=新的FileOutputStream(新文件(“C:\\bookCafeDatabase.txt”);
ObjectOutputStream o=新的ObjectOutputStream(f);
o、 写入对象(结果);
o、 close();
FileInputStream fi=新的FileInputStream(新文件(“C:\\bookCafeDatabase.txt”);
ObjectInputStream oi=新ObjectInputStream(fi);
阅读(内容);
结果=(HashMap)oi.readObject();
oi.close();
fi.close();
}catch(filenotfounde异常){
System.out.println(“未找到文件”);
}捕获(IOE异常){
System.out.println(“初始化流时出错”);
}catch(classnotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
我强烈建议不要使用对象序列化,并开始使用类似“谢谢你回答我的问题”的东西:)我希望你今天过得愉快!您好,谢谢您编写代码:)但我得到了nullpointexception,如果我尝试Sys.out。println(结果);我得到了“空”数据,你知道为什么我的代码不能通过hashmap读取这个文本文件吗?哦,我得到了数据:)非常感谢你,我希望你能得到美好的一天!!如果我的回答对你有帮助!请你选择灰色的勾号来接受我的回答。我怎么做:)请让我知道我会这么做:)你只需要勾选向上的三角形..谢谢你看看这个。
Client_Database database = new Client_Database();
String filename = "C:\\bookCafeDatabase.txt";
File file = new File(filename);
byte[] contents = new byte[(int) file.length()];
database.setFileContents(contents);
try {
ObjectOutputStream bos = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(file)));
bos.writeObject(result);
bos.close();
ObjectInputStream bis =
new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(file)));
bis.read(contents);
result = (HashMap<String, Client_Database>)bis.readObject();
bis.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Client_Database implements Serializable {
private String nameDB;
private String idDB;
private String passwordDB;
private String addressDB;
private String emailDB;
private String fileName;
private byte [] fileContents;
private ObjectOutputStream out;
public String getNameDB() {
return nameDB;
}
public void setNameDB(String nameDB) {
this.nameDB = nameDB;
}
public String getIdDB() {
return idDB;
}
public String setIdDB(String idDB) {
return this.idDB = idDB;
}
public String getPasswordDB() {
return passwordDB;
}
public void setPasswordDB(String passwordDB) {
this.passwordDB = passwordDB;
}
public String getAddressDB() {
return addressDB;
}
public void setAddressDB(String addressDB) {
this.addressDB = addressDB;
}
public String getEmailDB() {
return emailDB;
}
public void setEmailDB(String emailDB) {
this.emailDB = emailDB;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setFileContents(byte [] fileContents) {
this.fileContents = fileContents;
}
public String getFileName() {
return this.fileName;
}
public byte [] getFileContents() {
return this.fileContents;
}
public int getFileSize() {
return this.fileContents.length;
}
Client_Database() {
nameDB = Client_Signin.nameMsg;
idDB = Client_Signin.idMsg;
passwordDB = Client_Signin.passwordMsg;
addressDB = Client_Signin.addressMsg;
emailDB = Client_Signin.emailMsg;
}
}
nameMsg = name.getText().trim();
idMsg = id.getText().trim();
passwordMsg = password.getText().trim();
rePasswordMsg = rePassword.getText().trim();
addressMsg = address.getText().trim();
emailMsg = email.getText().trim();
Client_Database database = new Client_Database();
byte[] contents = new byte[(int) file.length()];
database.setFileContents(contents);
try {
FileOutputStream f = new FileOutputStream(new File("C:\\bookCafeDatabase.txt"));
ObjectOutputStream o = new ObjectOutputStream(f);
o.writeObject(result);
o.close();
FileInputStream fi = new FileInputStream(new File("C:\\bookCafeDatabase.txt"));
ObjectInputStream oi = new ObjectInputStream(fi);
oi.read(contents);
result = (HashMap<String, Client_Database>)oi.readObject();
oi.close();
fi.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}