簡體   English   中英

如何將txt文件讀取到字節[]和字節[]到Hashmap <String, Object> ?

[英]how to read txt file to byte [] and byte[] to Hashmap<String, Object>?

我的Java代碼有問題。 誰都知道這個問題。 我試圖將哈希圖保存在txt文件中,然后將txt文件讀取到哈希圖中,但是它不起作用。 我想我已經成功保存了哈希圖,但無法閱讀。 你們能幫我的代碼嗎?

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();
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM