如何将txt文件读取到byte []和byte []到Hashmap ?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将txt文件读取到byte []和byte []到Hashmap ?相关的知识,希望对你有一定的参考价值。
我的java代码有问题。任何人都知道这个问题我试图将我的hashmap保存在txt文件中,然后将txt文件读取到hashmap但它无法正常工作。我想我成功保存了我的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);
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 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();
}
}
以上是关于如何将txt文件读取到byte []和byte []到Hashmap ?的主要内容,如果未能解决你的问题,请参考以下文章
InputStream读取文件到string后OutputStream到文件,按String和Bytes拷贝
如何使用Argument将路径文件作为输入数据获取到PowerShell中的其他脚本?
java读取文件时,InputStream的read(byte[])方法的byte[]的长度不知如何设置,请教大虾们
Python读取txt文件报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0(示