读取文件并将其存储在对象中

Reading A File and Storing It In An Object

我正在尝试读取文件并将内容存储到名为 ToDoList 的对象中(我假设是在 GetItem 方法下)。然后我应该允许用户添加到列表中。但是我不知道如何创建对象并打印它。

public class ToDoList {

private ToDoItem[] items;

ToDoItem td = new ToDoItem();
String inputline;
Scanner keyboard = new Scanner(System.in);

int i = 0;

String[] stringArray = new String[100];



private void setItems(ToDoItem[] items) throws FileNotFoundException {
    File file = new File("ToDoItems.txt");
    Scanner ReadFile = new Scanner(file);

    while (ReadFile.hasNext()) {
        String ListString = ReadFile.nextLine();
        stringArray[100] = (ListString);
    }
}

private ToDoItem[] getItems() {

    return items;
}

public void addItem(int id, String description) {
    stringArray[100] = (td.getId() + td.getDescription());

}

public String[] getAddItem() throws FileNotFoundException {

    try (PrintWriter fout = new PrintWriter(new File("ToDoItems.txt"))) {
        do {
            System.out.println("add to the list? [y/n]");
            inputline = keyboard.nextLine();

            if ("y".equals(inputline)) {
                i++;
                stringArray[i] = (td.getId() + ". " + td.getDescription() + "\n");
                fout.print(stringArray[i]);
            } else {

                System.out.println("Here is the list so far:");

            }
        } while ("y".equals(inputline));
        return stringArray;
    }
}

@Override
public String toString() {
    return "ToDoList{" + "items=" + getItems()
            + '}';
}

我应该使用 "getAddItem" 方法来允许用户添加到列表中。但我不知道如何将数组添加到对象。更不用说做对象了。

您问的是一个宽泛的问题。这里有一些设计技巧供您参考。

  1. 创建您的 collection class。这可以命名为 ToDoList。然后创建这个class的属性和行为。一个属性是待办事项列表项的 collection 变量。您可以使用 List、ArrayList 等。行为可以是添加、删除、重新排序等。
  2. 创建您的项目class。再次与属性和行为。属性可以包括做什么、日期、重要性级别等。
  3. 当您阅读文件时,让您的程序为每一行、项目等实例化您的 ToDoItem class,然后将它们保存到先前创建的容器 class 中,即您的 ToDoList。
  4. 您可以使用您的 ToDoList class' addItem 方法(行为)让您的用户将更多项目添加到您的 ToDoList 中。如果您想在程序关闭后保留列表。您可以创建一个数据库来存储您的 objects.

祝你好运。

一些代码来扩展 pininfarina 所说的内容并帮助您开始。

  1. 您需要一个 ToDoItem class。像这样:

    public class ToDoItem {
    
        private String id;
    
        private String description;
    
        public ToDoItem(String id, String description) {
            this.id = id;
            this.description = description;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
    }
    

然后您需要一个 ToDoList class 来保存每个项目。你用一个数组支持你的,但我用了一个 ArrayList:

public class ToDoList {

    private ArrayList<ToDoItem> items = new ArrayList<ToDoItem>();

    public ToDoList(String fileName) throws FileNotFoundException {
        File file = new File(fileName);
        Scanner scanner = new Scanner(file);

        try {
            while (scanner.hasNext()) {
                String nextLine = scanner.nextLine();
                StringTokenizer tokenizer = new StringTokenizer(nextLine, ",");
                String id = tokenizer.nextToken();
                String description = tokenizer.nextToken();
                items.add(new ToDoItem(id, description));
            }
        } finally {
            scanner.close();
        }
    }

    public void setItems(ArrayList<ToDoItem> newItems) {
        this.items.addAll(newItems);
    }

    public List<ToDoItem> getItems() {
        return items;
    }

    public void addItem(ToDoItem item) {
        items.add(item);

    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("ToDoList{");
        for (ToDoItem item : items) {
            builder.append(item.getId() + "," + item.getDescription() + "\n");
        }
        builder.append("}");

        return builder.toString();
    }
}

这包括一个读取文件并解析项目的构造函数。文件中的每一行都必须类似于“1,something”,因为分词器使用逗号。请注意,扫描程序实际上会在读取文件时销毁文件。您可能会考虑改用某种 FileReader。

最后你需要一个主class到运行它。像这样:

public class RunIt {

    private static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) throws FileNotFoundException {
        ToDoList list = new ToDoList("ToDoItems.txt");

        try (PrintWriter fout = new PrintWriter(new File("ToDoItems.txt"))) {
            String inputLine;
            do {
                System.out.println("add to the list? [y/n]");
                inputLine = keyboard.nextLine();

                if ("y".equals(inputLine)) {
                    System.out.println("enter a to-do using the format 'id,description'");
                    StringTokenizer tokenizer = new StringTokenizer(keyboard.nextLine(),
                            ",");
                    String id = tokenizer.nextToken();
                    String description = tokenizer.nextToken();
                    list.addItem(new ToDoItem(id, description));
                } else {
                    System.out.println("Here is the list so far:");
                    System.out.println(list);
                }
            } while ("y".equals(inputLine));
        }
    }
}

请注意,这里还有很大的改进空间(异常处理、更强大的文件读取等),但这应该可以帮助您入门。