如何将TreeMap写入.txt文件?

时间:2018-08-07 04:00:41

标签: java file-io nio treemap

我是这个论坛的新手,所以如果我没有在正确的位置发布此信息,请告诉我。我一直在尝试编写一个简单的程序,该程序创建联系人列表并按姓氏对它们进行排序。但是,我很难将这个My <select class="text_select" id="dropdown_1" name="dropdown_1"> <option value="">- Select -</option> <option value="Father">Father's</option> <option value="Mother">Mother's</option> <option value="Sister">Sister's</option> <option value="Brother">Brother's</option> <option value="Husband">Husband's</option> <option value="Wife">Wife's</option> <option value="Son">Son's</option> <option value="Daughter">Daughter's</option> <option value="Grandfather">Grandfather's</option> <option value="Grandmother">Grandmother's</option> <option value="Aunt">Aunt's</option> <option value="Uncle">Uncle's</option> <option value="Cousin">Cousin's</option> <option value="Father-in-Law">Father-in-Law's</option> <option value="Mother-in-Law">Mother-in-Law's</option> <option value="Sister-in-Law">Sister-in-Law's</option> <option value="Brother-in-Law">Brother-in-Law's</option> <option value="Niece">Niece's</option> <option value="Nephew">Nephew's</option> <option value="Grandson">Grandson's</option> <option value="Granddaughter">Granddaughter's</option> <option value="Great-Aunt">Great-Aunt's</option> <option value="Great-Uncle">Great-Uncle's</option> </select> <select class="text_select" id="dropdown_2" name="dropdown_2"> <option value="">- Select -</option> <option value="Father">Father</option> <option value="Mother">Mother</option> <option value="Sister">Sister</option> <option value="Brother">Brother</option> <option value="Husband">Husband</option> <option value="Wife">Wife</option> <option value="Son">Son</option> <option value="Daughter">Daughter</option> <option value="Grandfather">Grandfather</option> <option value="Grandmother">Grandmother</option> <option value="Aunt">Aunt</option> <option value="Uncle">Uncle</option> <option value="Cousin">Cousin</option> <option value="Father-in-Law">Father-in-Law</option> <option value="Mother-in-Law">Mother-in-Law</option> <option value="Sister-in-Law">Sister-in-Law</option> <option value="Brother-in-Law">Brother-in-Law</option> <option value="Niece">Niece</option> <option value="Nephew">Nephew</option> <option value="Grandson">Grandson</option> <option value="Granddaughter">Granddaughter</option> <option value="Great-Aunt">Great-Aunt</option> <option value="Great-Uncle">Great-Uncle</option> </select> <br /><br /><br /> <label class="row_1">My <span id="first"></span> <span id="second"></span></label> <br /> <label class="row_2">My <span id="third"></span> <span id="fourth"></span></label>列表保存到TreeMap文件中。

有人可以给我一些建议吗?

.txt

2 个答案:

答案 0 :(得分:1)

添加outputWriter.flush()

更新代码:

import java.io.*;
import java.util.*;

public class ContactList {
    public static void main(String[] args) {
        try {
            TreeMap<String, List<String>> lastName = new TreeMap<String, List<String>>();
            Scanner input = new Scanner(System.in);
            int choice;
            int secondChoice;
            boolean exit = true;
            int i = 0;
            System.out.println("Would you like to add a contact?" + " Type 1 for yes");
            choice = input.nextInt();
            if (choice == 1) {
                while (exit == true) {
                    // try adding a second tree map
                    List<String> array = new ArrayList<String>();
                    System.out.println("key " + lastName.isEmpty());
                    System.out.println("Plese add first name");
                    array.add(input.next());
                    System.out.println("Plese add phone number");
                    array.add(input.next());
                    System.out.println("Plese add email address");
                    array.add(input.next());
                    System.out.println("Plese add last name");
                    String last = input.next();
                    lastName.put(last, array);
                    System.out.println("This is what you just entered: " + lastName);

                    System.out.println("\nWould you like to add another contact?" + " Type 1 for yes - 2 for no");
                    secondChoice = input.nextInt();
                    if (secondChoice == 1) {
                        exit = true;
                        String hold = lastName.toString();
                        System.out.println(hold);
                    } else {
                        for (Map.Entry product : lastName.entrySet()) {
                            System.out.println(product.getKey() + " : " + product.getValue());
                        }

                        File f1 = new File("Contacts.txt");
                        System.out.println("\nLoading contacts into Contacts.txt ...");
                        BufferedWriter outputWriter = null;
                        outputWriter = new BufferedWriter(new FileWriter(f1));
                        for (Map.Entry<String, List<String>> entry : lastName.entrySet()) {
                            String key = entry.getKey();
                            List<String> value = entry.getValue();
                            outputWriter.write(key + value);
                        }
                        System.out.println("File location: " + f1.getAbsolutePath());
                        exit = false;
                        input.close();
                        outputWriter.flush();
                        outputWriter.close();
                    }
                }
            } else {
                System.out.println("Thank you for using my program");
                exit = false;
            }
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

答案 1 :(得分:0)

我认为在使用资源后关闭它很重要。在帖子的代码中,使用了io对象BufferedWriterFileWriter,使用它们之后需要将其关闭。在此应用程序中,写入文本文件是一个简单的输出操作,并且使用BufferedWriter之类的资源来有效地写入大量数据或通过网络。

可以使用PrintWriter来简单地写入文本文件,如本例所示。 PrintWriter有一个println方法,该方法写入String行,并在该行的末尾添加一个行终止符。这意味着输出文件将每个联系人的联系方式写为一行。

代码如下:

String fileName = "Contacts.txt";
System.out.println("\nLoading contacts into " + fileName + "...");
PrintWriter writer = new PrintWriter(fileName);
for (Map.Entry product : lastName.entrySet())
{
    System.out.println(product.getKey() + " : " + product.getValue());
    writer.println(product.getKey() + ", " + product.getValue());
}
writer.close(); // Important: close the io resource after using it.
相关问题