简体   繁体   English

Java从文件中读取内容到ArrayList(对象)

[英]Java read contents from file into ArrayList (Objects)

I was able to save the contents in an ArrayList which contains objects to a file by using the PrintWriter, 我可以使用PrintWriter将内容保存在包含对象的ArrayList中,

Declaring 声明

public static ArrayList<AccountRecords> userRecords = new 
                                            ArrayList<AccountRecords>();

Method 方法

private static Boolean saveObj(){

   try{

   File userRecordsFile = new File("userRecords");



     PrintWriter pw2 = new PrintWriter(new FileOutputStream(userRecordsFile));
   for(int x=0;x<userRecords.size();x++){
        pw2.println(userRecords.get(x));
   }
    pw2.close();

    return true;
   }catch(Exception e){
       e.printStackTrace();
       return true;
   }



  }

AccountRecords AccountRecords

class AccountRecords{

private String identificationNo;
private int accountNo;
private int monthId;
private double balance;
private double credit;
private double debit;
private int year;


public AccountRecords(String identificationNo, int accNo,int monthId, double balance, double creditAmount, double debitAmount, int year){

    this.identificationNo = identificationNo;
    this.accountNo = accNo;
    this.balance = balance;
    this.monthId = monthId;
    this.credit = creditAmount;
    this.debit = debitAmount;
    this.year = year;
}

public double getCredit() {
    return credit;
}

public double getDebit() {
    return debit;
}

 public double getBalance() {
    return balance;
}

public String getIdentificationNo() {
    return identificationNo;
}

public int getAccountNo() {
    return accountNo;
}

public int getMonthId() {
    return monthId;
}

public int getYear() {
    return year;
}

But now i want to read the contents in the file userRecords into the arrayList again, how can achieve this? 但是现在我想再次将文件userRecords的内容读入arrayList中,如何实现呢?

private static Boolean readObj(){

   return true;

} }

PS, the contents in the arrayList is inserted at a point in the program therefore the arrayList is not empty. PS,将arrayList中的内容插入到程序中的某个点,因此arrayList不为空。

There's no way you can do this as-is. 您无法原样进行此操作。 You don't even declare a toString() method that you could parse later. 您甚至没有声明稍后可以解析的toString()方法。 The simplest method would be to use an object stream. 最简单的方法是使用对象流。

Here's some example code. 这是一些示例代码。 I write the data to a base 64 encoded string, but you could just as easily write the data to a file. 我将数据写入以64为基数的字符串,但是您也可以轻松地将数据写入文件。

  // Create a base 64 encoded string and print it
  byte[] data = {-1,-2,-3,0,1,2,3,};
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream enc = new ObjectOutputStream( bos );
  enc.writeObject( data );
  enc.close();

  String b64 = Base64.getEncoder().encodeToString( bos.toByteArray() );
  System.out.println( "data=" + b64 );

  // Read some base 64 encoded data and print it
  String dataIn = "rO0ABXVyAAJbQqzzF/gGCFTgAgAAeHAAAAAH//79AAECAw==";
  byte[] bdataIn = Base64.getDecoder().decode( dataIn );
  ByteArrayInputStream bais = new ByteArrayInputStream( bdataIn );
  ObjectInputStream ois = new ObjectInputStream( bais );
  byte[] readData = (byte[]) ois.readObject();

  System.out.println( "orig array: " + Arrays.toString( readData ) );

To write an array list, just write it: 要编写一个数组列表,只需编写它:

OutputStream os = new FileOutputStream(userRecordsFile);
ObjectOutputStream enc = new ObjectOutputStream( os );
enc.writeObject( userRecords );
enc.close();

Reverse the process to read the data in. 逆向过程以读取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM