将对象保存到文件中并从文件中读取对象

时间:2019-09-10 13:47:20

标签: java json json-arrayagg

我是OOP编程的新手,正在做一个项目。 在某些时候,我必须使用json表示法将信息保存到文件中。 我的课程: FeedGroup

public class FeedGroup implements FeedGroupContract {

    private int feedGroupID;
    private String feedGroupTitle;
    private String feedGroupDescription;
    private Feed[] feeds;
    private int tamanho;
    private final int DEFAULT_SIZE = 10;

    /*
    private int feedGroupIDGenerator(){

    }
*/
    public FeedGroup(String feedGroupTitle, String feedGroupDescription) {
        this.feeds= new Feed[DEFAULT_SIZE];
        this.feedGroupTitle = feedGroupTitle;
        this.feedGroupDescription = feedGroupDescription;
    }

    public FeedGroup(){
        this.feeds= new Feed[DEFAULT_SIZE];
    }

    public FeedGroup(int feedGroupID, String feedGroupTitle, String feedGroupDescription, Feed[] feeds) {
        this.feedGroupID = feedGroupID;
        this.feedGroupTitle = feedGroupTitle;
        this.feedGroupDescription = feedGroupDescription;
        this.feeds = new Feed[DEFAULT_SIZE];
    }

    private void increaseSize() {
        this.tamanho++;
    }

    @Override
    public int getID() {
        return this.feedGroupID;
    }

    @Override
    public String getTitle() {
        return this.feedGroupTitle;
    }

    @Override
    public void setTitle(String string) {
        this.feedGroupTitle = feedGroupTitle;
    }

    @Override
    public String getDescription() {
        return this.feedGroupDescription;
    }

    @Override
    public void setDescription(String string) {
        this.feedGroupDescription = feedGroupDescription;
    }

    @Override
    public boolean addFeed(String feedS) throws GroupException {
        Feed newFeed = new Feed();

        for (int i = 0; i < this.feeds.length; i++) {
            System.out.println("Saving...");
            if (this.feeds[i] == null) {
                //this.feeds[i] = (Feed) ;
                System.out.println("Add object class: " + this.feeds[i]);
                System.out.println("Saved successfully.");
                increaseSize();
                return true;
            }
        }
        return false;

    }

    @Override
    public boolean addFeed(FeedContract fc) throws GroupException {
        for (int i = 0; i < this.feeds.length; i++) {
            System.out.println("Saving...");
            if (this.feeds[i] == null) {
                this.feeds[i] = (Feed) fc;
                System.out.println("Add object class: " + this.feeds[i]);
                System.out.println("Saved successfully.");
                increaseSize();
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean removeFeed(FeedContract fc) throws ObjectmanagementException {
        boolean found = false;
        for (int i = 0; i < this.feeds.length; i++) {
            if (feeds[i] == fc) {
                found = true;
                this.feeds[i] = this.feeds[i + 1];
            } else {
                found = false;
            }
        }
        return found;
    }

    @Override
    public FeedContract getFeed(int i) throws ObjectmanagementException {
        return this.feeds[i];

    }

    @Override
    public FeedContract getFeedByID(int i) throws ObjectmanagementException {
        Feed found = new Feed();
        for (int j = 0; j < this.feeds.length; j++) {
            if (feeds[i].getID() == i && this.feeds[i] != null) {
                found = this.feeds[i];
            }
        }
        return found;
    }

    @Override
    public int numberFeeds() {
        int count = 0;
        for (Feed feed : feeds) {
            if (feed != null) {
                count++;
            }
        }
        return count;
    }

    @Override
    public void getData() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String toString() {
        return "FeedGroup{" + "feedGroupID=" + feedGroupID + ", feedGroupTitle=" + feedGroupTitle + ", feedGroupDescription=" + feedGroupDescription + ", feeds=" + feeds + ", tamanho=" + tamanho + ", DEFAULT_SIZE=" + DEFAULT_SIZE + '}';
    }

}

Feed

public class Feed implements FeedContract {

    private String feedTitle;
    private String feedDescription;
    private String feedLanguage;
    private Calendar buildDate;
    private FeedItem feedItemPos;
    private String feedURL;
    private String[] categories;
    private int categoryID;
    private FeedItem[] items;
    private int tamanho;
    private final int DEFAULT_SIZE = 10;

     public Feed(String feedTitle, String feedDescription, String feedLanguage, Calendar buildDate, FeedItem feedItemPos, String feedURL, String[] categories, int categoryID) {
        this.feedTitle = feedTitle;
        this.feedDescription = feedDescription;
        this.feedLanguage = feedLanguage;
        this.buildDate = buildDate;
        this.feedItemPos = feedItemPos;
        this.feedURL = feedURL;
        this.categories = categories;
        this.categoryID = categoryID;
        this.items = new FeedItem[DEFAULT_SIZE];
    }

    public Feed(String feedGroupURL){

    }

    public Feed() {
        this.items = new FeedItem[DEFAULT_SIZE];
    }

    @Override
    public String getTitle() {
        return this.feedTitle;
    }

    @Override
    public void setTitle(String string) {
        this.feedTitle = feedTitle;
    }

    @Override
    public String getDescription() {
        return this.feedDescription;
    }

    @Override
    public void setDescription(String string) {
        this.feedDescription = feedDescription;
    }

    @Override
    public String getLanguage() {
        return this.feedLanguage;
    }

    @Override
    public void setLanguage(String string) {
        this.feedLanguage = feedLanguage;
    }

    @Override
    public Calendar getBuildDate() {
        return this.buildDate;
    }

    @Override
    public void setBuildDate(Calendar clndr) {
        this.buildDate = buildDate;
    }

    private void increaseSize() {
        this.tamanho++;
    }

    @Override
    public boolean addItem(String string, String string1, String string2, Calendar clndr, String string3, String string4) {
        FeedItem item = new FeedItem(string, string1, string2, clndr, string3, string4);
        System.out.println(item.getAuthor());
        //System.out.println(items);
        if (this.items[0] == null) {
            System.out.println("ENTROU");
            this.items[0] = item;
        } else {
            for (int i = 0; i < this.items.length; i++) {
                System.out.println("AQUII");
                if (items[i] == null) {
                    items[i] = item;
                    System.out.println("Add object: " + this.items[i]);
                    increaseSize();
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public FeedItemContract getItem(int i) throws ObjectmanagementException {
        return this.feedItemPos;
    }

    @Override
    public boolean addCategory(String categoria) {
        for (int i = 0; i < this.categories.length; i++) {
            if (categories[i] == null) {
                categories[i] = categoria;
                System.out.println("Add object: " + categoria);
                increaseSize();
                return true;
            }
        }
        return false;
    }

    @Override
    public String getCategory(int i) throws ObjectmanagementException {
        return this.categories[i];
    }

    @Override
    public int numberCategories() {
        int count = 0;
        for (String category : categories) {
            if (category != null) {
                count++;
            }
        }
        return count;
    }

    @Override
    public int numberItems() {
        int count = 0;
        for (FeedItem item : items) {
            if (item != null) {
                count++;
            }
        }
        return count;
    }

    @Override
    public int getID() {
        return this.categoryID;
    }

    @Override
    public String getURL() {
        return this.feedURL;
    }

    @Override
    public void setURL(String string) throws FeedException {
        this.feedURL = string;
    }

}

和应用

public class App implements AppContract {

    private FeedGroup feedGroupPosition;
    private FeedGroup feedGroupID;
    private Tag tag;
    private FeedItem feedItem;
    private FeedGroup[] groups;
    private int tamanho;

    public App() {

        this.groups = new FeedGroup[10];
    }

    private void increaseSize() {
        this.tamanho++;
    }

    /**
     * Método para adicionar um grupo
     *
     * @param string titulo do grupo
     * @param string1 descrição do grupo
     * @return true se adicionar, false se não o fizer
     */
    @Override
    public boolean addGroup(String string, String string1) {
        FeedGroup group = new FeedGroup(string, string1);
        //System.out.println(group);
        //System.out.println(this.groups.length);
        if (this.groups.length == 0) {
            this.groups[0] = group;
        } else {
            for (int i = 0; i < this.groups.length; i++) {
                System.out.println("Saving...");
                if (this.groups[i] == null) {
                    this.groups[i] = group;
                    System.out.println("Add object class: " + this.groups[i]);
                    System.out.println("Saved successfully.");
                    increaseSize();
                    //System.out.println(this.groups.length);
                    return true;
                }
            }

        }
        return false;
    }

    @Override
    public boolean removeGroup(int i) throws ObjectmanagementException {
        boolean found = false;
        for (int j = i; j < this.groups.length; j++) {
            if (groups[j] != null) {
                found = true;
                this.groups[j] = this.groups[j + 1];
            } else {
                found = false;
            }
        }
        return found;
    }

    @Override
    public FeedGroupContract getGroup(int i) throws ObjectmanagementException {
        return this.groups[i];
    }

    @Override
    public FeedGroupContract getGroupByID(int i) throws ObjectmanagementException {
        FeedGroup found = new FeedGroup();
        for (int j = 0; j < this.groups.length; j++) {
            if (groups[i].getID() == i && this.groups[i] != null) {
                found = this.groups[i];
            }
        }
        return found;
    }

    @Override
    public int numberGroups() {
        int count = 0;
        for (FeedGroup group : groups) {
            if (group != null) {
                count++;
            }
        }
        return count;
    }

    @Override
    public FeedItemContract[] getItemsByTag(String string) {
        //for(int i = 0; i<)
        return null;
    }

    @Override
    public void saveGroups() throws Exception {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        for (int i = 0; i < this.groups.length; i++) {
            FeedGroup fg = (FeedGroup) this.getGroup(i);
            JSONArray jsonArrayTemp = new JSONArray();

            for (int j = 0; j < fg.numberFeeds(); j++) {
                Feed feed = (Feed) fg.getFeed(i);
                jsonArrayTemp.add(feed.getURL());
            }

            jsonObject.put("Group", jsonArrayTemp);
            jsonObject.put("Title", groups[i].getTitle());
            jsonObject.put("Description", groups[i].getDescription());
//            System.out.println("URL: "+ groups[i].getFeed(i).getURL());
            jsonObject.put("URL", groups[i].getFeed(i).getURL());
//            if(groups[i].getFeed(i).getURL() != null){
//                jsonObject.put("URL", groups[i].getFeed(i).getURL());
//            } else {
//                jsonObject.put("URL", "");
//            }

            jsonArray.add(jsonObject);
        }
        FileWriter file = null;
        file = new FileWriter("group.json");
        file.write(jsonArray.toJSONString());
        file.flush();
    }

    @Override
    public void loadGroups() throws Exception {

    }

    @Override
    public FeedGroupContract[] getAllGroups() {
        return this.groups;
    }

    @Override
    public FeedItemContract[] getAllSavedItems() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean removeSavedItem(int i) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

App类是一种对象容器。 所以在简历中,我想使用json将FeedGroup属性保存在文件中,然后加载该文件。这些方法在App类中实现(saveGroup()和loadGroup())。 FeedGroup有一个Feed类的实例:“私有Feed [] feeds”,从Feed类中,我只想获取feedURL来保存在文件中。 我做对了吗? 我试图做loadResults()方法来查看其他项目(是的,即使不知道saveGroup()方法是否正确完成),我也明白了。我必须先使用set然后使用valueOf,但是我认为如果没有正确完成saveGroup()方法,对我来说一文不值。 有人能帮我吗? 很抱歉使用long(?)描述。 谢谢。

0 个答案:

没有答案
相关问题