2017-12-03 17 views
0

私はかなり簡単なプログラムを書いています。これは実行の間に単純なデータを保存する必要があります。このデータは、UserDataをによって定義以下に概説される:ObjectInput/OutputStreamがシリアル化/デシリアライズされていないようです

public class UserData implements Serializable { 

/** 
* 
*/ 
private static final long serialVersionUID = -3542558265070011448L; 

public static ArrayList<String> projectList; 

public static ArrayList<Bill> billList; 

public static String userName; 

public static String userEmail; 

public UserData() { 

} 

public UserData(String name, String email) { 
    super(); 
    userName = name; 
    userEmail = email; 
    projectList = new ArrayList<String>(); 
    billList = new ArrayList<Bill>(); 

} 

public String getUserName() { 
    return userName; 
} 

public String getUserEmail() { 
    return userEmail; 
} 

public ArrayList<Bill> getBillList() { 
    return billList; 

} 

public ArrayList<String> getProjectList() { 
    return projectList; 

} 

public void setBillList(Bill theBill) { 
    billList.add(theBill); 
} 

public void setProjectList(String projectName) { 
    projectList.add(projectName); 
} 

}

私はこのデータのシリアライズ/デシリアライズを処理し、様々なコールが直接又は間接的に行うと、それは、ファイルへのインスタンスのクラスを持っています別の郭クラスのイベントでこれは、それは次のとおりです。

public class FileHandler implements Serializable { 

/** 
* 
*/ 
private static final long serialVersionUID = 473118590700911358L; 
private static JFileChooser fileChooser; 
public UserData myUserData; 

public FileHandler() throws ClassNotFoundException, IOException { 

    fileChooser = new JFileChooser(); 
    initData(); 
} 

public FileHandler(UserData newUser) { 
    fileChooser = new JFileChooser(); 
    myUserData = newUser; 
    System.out.println("Entered User: " + myUserData.getUserName()); 
    System.out.println("Entered User: " + myUserData.getUserEmail()); 
} 

private void createProgramData() throws FileNotFoundException, IOException { 
    FileOutputStream fileOut = new FileOutputStream("data\\ProgramData.diy"); 
    ObjectOutputStream encoderp = new ObjectOutputStream(fileOut); 
    System.out.println("createProgramData: " + myUserData.getUserName()); 
    System.out.println("createProgramData: "+ myUserData.getUserEmail()); 
    encoderp.writeObject(myUserData); 
    encoderp.close(); 
    fileOut.close(); 

} 

public void exportData() throws FileNotFoundException, IOException { 
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
    final int selectedFile = fileChooser.showOpenDialog(null); 
    ObjectOutputStream encodere = null; 

    if (selectedFile == JFileChooser.APPROVE_OPTION) { 
     final File selectedPath = fileChooser.getSelectedFile(); 
      FileOutputStream fileOut = new FileOutputStream(selectedPath + "\\UserData.diy"); 
      encodere = new ObjectOutputStream(fileOut); 
      System.out.println("Writing Data: " + myUserData.getUserName()); 
      System.out.println("Writing Data: " + myUserData.getUserEmail()); 
      encodere.writeObject(myUserData); 
      encodere.close(); 
      fileOut.close(); 
    } 

} 

public void importData() throws ClassNotFoundException, IOException { 
    fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 
    final int selectedFile = fileChooser.showSaveDialog(null); 
    ObjectInputStream decoderim = null; 

    if (selectedFile == JFileChooser.APPROVE_OPTION) { 
      final File selectedPath = fileChooser.getSelectedFile(); 
      FileInputStream fileIn = new FileInputStream(selectedPath); 
      decoderim = new ObjectInputStream(fileIn); 
      myUserData = (UserData)decoderim.readObject(); 
      decoderim.close(); 
      fileIn.close(); 
      System.out.println("importing Data: " + myUserData.getUserEmail()); 
      System.out.println("importing Data: " + myUserData.getUserName()); 

    } 

} 

private void initData() throws IOException, ClassNotFoundException { 

    FileInputStream fileIn = new FileInputStream("data\\ProgramData.diy"); 
    ObjectInputStream decoderi = new ObjectInputStream(fileIn); 
    myUserData = (UserData)decoderi.readObject(); 
    decoderi.close(); 
    fileIn.close(); 
    System.out.println("initializing Data: " + myUserData.getUserName()); 
    System.out.println("Initializing Data: " + myUserData.getUserEmail()); 

} 

public UserData getUserData() { 
    return myUserData; 
} 

私はObjectInputStreamそしておそらくObjectOutputStreamで抱えている問題は、再入力し、そのファイルをインポートしようとインスタンス、私はUserDataのをシリアル化するとき、私のプログラムを終了することであるように思われ、 UserDataの値は変更されません。私は何が欠けているのかわからない。私が見落としたことを指摘しても助けになります。

+0

「戻ってきません」。それはすべてのフィールドがヌルに戻ってくるかもしれませんが、ヌルをシリアライズした場合にのみヌルとして戻ることができます。 – EJP

答えて

0

UserDataには静的フィールドのみがあり、静的フィールドはシリアル化されません。あなたのコードの外観から、それらはすべてインスタンスメンバーでなければなりません。なぜあなたがそうしているのか正確に分からない限り、何もしないでください。static

関連する問題