2016-11-23 30 views
4

我想開發一個團隊管理軟件。我有一個團隊對象:是否可以將包含BufferedImage的對象的ArrayList作爲Java中的一個對象的私有實例變量進行序列化?

public class Team implements Serializable{ 
    private String name;// 
    private ArrayList<Player> teamMembers;// 
    private String sport;// 
    private ArrayList<Staff> staff;//Non-Player Members such as head coach, assisstant coach, physio, etc. 
    private Object schedule; 
    private String teamHometown; 
    private String teamState; 

    // Getter and setter methods go here 

    public Team(String name, String sport, ArrayList<Player> players, ArrayList<Staff> staff){ 
     teamMembers = players; 
     this.staff = staff; 
     this.sport =(sport); 
     this.name = (name); 

    } 

} 

團隊對象包含播放器對象的ArrayList。在每一個選手對象的選手有許多屬性,如名稱,統計信息和球員的照片(緩衝圖像):

public class Player implements Serializable{ 

    private BufferedImage playerPhoto; 
    private String firstName; 
    private String lastName; 
    private int jerseyNumber; 
    private String playerPosition; 
    private String status;//Is the player Eligible to plays 
    private int gameAbscenses; 
    private int goals; 
    private int appearances; 
    private int yellowCards; 
    private int redCards; 
    private LocalDate birthday; 
    private boolean starter; 
    private int shots; 
    private int shotsOnGoal; 
    private int assists; 

    //Getter and Setter Methods go here 

    public Player(String firstName, String lastName, int jerseyNumber, String playerPosition,BufferedImage playerPhoto) { 
     this.firstName = (firstName); 
     this.lastName = (lastName); 
     this.jerseyNumber = (jerseyNumber); 
     this.playerPosition = (playerPosition); 
     this.gameAbscenses = (0); 
     this.status = ("Healthy"); 
     this.starter = false; 

     //stats 
     this.shots = (0); 
     this.appearances = (0); 
     this.shotsOnGoal = (0); 
     this.redCards = (0); 
     this.yellowCards = (0); 
     this.assists = (0); 
     this.goals = (0); 

     this.birthday = LocalDate.now(); 

    } 

我的目標是序列化團隊的ArrayList對象然而緩衝圖像不是序列並且由於Player對象包含一個緩衝圖像作爲其私有實例變量之一,所以我無法序列化Team對象的ArrayList。

我本來試着這樣做:

ArrayList<Team> finalTeams = new ArrayList<>(SignInController.userTeams);//Converting the user teams Observable list to an Array List 

      //Saving the user's Teams locally 

      FileOutputStream fos = new FileOutputStream(SignInController.userfname+"'s Teams/"+SignInController.userfname+"_teams.xtm"); 
      ObjectOutputStream oos = new ObjectOutputStream(fos); 

      oos.writeObject(finalTeams);//serializing the ARRAYlist of teams 
      oos.close(); 
      fos.close(); 

然而,它沒有工作。我也嘗試着查看其他與序列化緩衝圖像有關的堆棧溢出問題,但沒有人能夠幫助我。

如果有人能向我解釋我做錯了什麼,那將是非常棒的!謝謝!

+0

,如果它不是,它意味着定製這個類只有當你聲明這個字段爲「transient」時,你才能序列化對象(意思是 - 它不會被序列化,並且這個信息不會通過線)。 – alfasin

+0

是的,我知道,但那是我不想發生的事情。我想知道如果BufferedImage可以序列化 – xkunle97

回答

1

是的,你可以,爲什麼不呢?!但我們將按照非直線的方法和它的工作我都沒

  1. 據瞭解,基本類型和原始類型的數組都是序列化
  2. 它也知道,我們可以緩衝圖像實例轉換爲字節陣列和逆也適用
  3. 所以不是一個序列化緩衝的圖像,我們將序列化的字節數組,表示

我已經創建了一個模擬你的問題演示這個緩衝圖像

import java.awt.image.*; 
import java.io.*; 
import javax.imageio.ImageIO; 
public class Member implements Serializable 
{ 

    private String name; 
    private byte[] imagebytes; 

    public Member(String name, BufferedImage image) throws IOException 
    { 
     this.name = name; 
     this.setImage(image); 
    } 

    public Member(String name, File imageFile) throws IOException 
    { 
     this.name = name; 
     this.setImage(imageFile); 
    } 

    public final void setImage(BufferedImage image) throws IOException 
    { 
     ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
     ImageIO.write(image, "jpg", outputStream); 
     this.imagebytes = outputStream.toByteArray(); 
    } 

    public final void setImage(File imageFile) throws IOException 
    { 
     BufferedImage bufferedImage = ImageIO.read(imageFile); 
     this.setImage(bufferedImage); 
    } 


    public BufferedImage getImage() 
    { 

     try 
     { 
      return ImageIO.read(new ByteArrayInputStream(imagebytes)); 
     } catch (Exception io) 
     { 
      return null; 
     } 
    } 

    public String getName() 
    { 
     return name; 
    } 

} 

你可以添加方法爲u喜歡和u需要 我希望如果緩衝的圖像是序列化,你不應該有任何問題,這個答案是非常有用的

+0

它的工作。謝謝! – xkunle97

+1

好消息兄弟,這沒什麼,很樂意幫忙 – Anas

0

按的javadoc

類的序列化和 反序列化過程中需要進行特殊處理必須實現這些 準確簽名的特殊方法:

private void writeObject(java.io.ObjectOutputStream out) 
    throws IOException 
private void readObject(java.io.ObjectInputStream in) 
    throws IOException, ClassNotFoundException; 
private void readObjectNoData() 
    throws ObjectStreamException; 

所以也許延長BufferedImage與你自己的班級並實施上述方法