2017-05-02 9 views
-2

私は2つのクラスを持っています。この2つのarraylist実装のリストとユーザーです。異なるデータ型のサンプルコードスニペットに示されているように、私はファイルの読み書きのための一般的なメソッドを実行しています。メソッドが機能していない、データはファイルに保存されていません。私は何かがあなたが投稿したコードに誤りがないパラメータ 「リストarrayname」メソッドが2つのタイプのリストを引数として受け入れる方法を教えてください。

public static List<Book> books = new ArrayList<Book>(); 
public static List<User> users = new ArrayList<User>(); 
//Sample array list 

public static void writeFile(String filename,List<Object> arrayname){ 

    File file = new File(filename+".ser"); 

    try { 
     FileOutputStream fos = new FileOutputStream(file); 
     ObjectOutputStream oos = new ObjectOutputStream(fos); 

     oos.writeObject(arrayname); 

     oos.close(); 
     fos.close(); 

    }catch(IOException i) { 
    System.out.println("The file " + file.getPath() + " was not found."); 
    } 

} 

public static void readFile(String filename, List<?> arrayname){ 

    File file = new File(filename+".ser"); 
    try { 


     FileInputStream fis = new FileInputStream(file); 
     ObjectInputStream ois = new ObjectInputStream(fis); 

     arrayname = (List<?>)ois.readObject(); 

     ois.close(); 
     fis.close(); 

    }catch(IOException i) { 
     System.out.println("The file " + file.getPath() + " was not found."); 
    } catch (Exception e) { 
      e.printStackTrace(); 
     } 
} 
+1

私は問題を抱えている* - 問題は何ですか? –

+0

@ScaryWombat私の間違いは、ユーザーでなければなりません。メソッドが機能していない、データはファイルに保存されていません。私は何かが "List arrayname"パラメータで間違っていると思います。 – user3178327

+0

ArrayList内のオブジェクトがSerializableを実装していないことがありますか? –

答えて

0

で間違っていたと思います。私はそれを実行しようとし、それは動作します。ここにプログラムされています

class Book implements Serializable { 
    private String title; 
    private String author; 

    public Book() { 
    } 

    public Book(String title, String author) { 
     super(); 
     this.title = title; 
     this.author = author; 
    } 

    @Override 
    public String toString() { 
     return "Book [title=" + title + ", author=" + author + "]"; 
    } 

} 

public class Snippet { 
    public static void main(String[] args) { 
     List<Book> list = Arrays.asList(new Book("book1", "author1"), new Book("book2", "author2"), 
       new Book("book3", "author3")); 
     writeFile("C:\\books", list); 
     List<Book> list2 = readFile("C:\\books", list); 
     list2.forEach(System.out::println); 
    } 

    public static List<Book> readFile(String filename, List<?> arrayname) { 

     File file = new File(filename + ".ser"); 
     try { 

      FileInputStream fis = new FileInputStream(file); 
      ObjectInputStream ois = new ObjectInputStream(fis); 

      List<Book> books = (List<Book>) ois.readObject(); 

      ois.close(); 
      fis.close(); 

      return books; 
     } catch (IOException i) { 
      System.out.println("The file " + file.getPath() + " was not found."); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    public static void writeFile(String filename, List<Book> arrayname) { 

     File file = new File(filename + ".ser"); 

     try { 
      FileOutputStream fos = new FileOutputStream(file); 
      ObjectOutputStream oos = new ObjectOutputStream(fos); 

      oos.writeObject(arrayname); 

      oos.close(); 
      fos.close(); 

     } catch (IOException i) { 
      System.out.println("The file " + file.getPath() + " was not found."); 
     } 

    } 

プリント:*

Book [title=book1, author=author1] 
Book [title=book2, author=author2] 
Book [title=book3, author=author3] 
関連する問題