PrintWriter 在文件中打印 "line.separator" 两次

PrintWriter printing "line.separator" twice in file

我有一个名为 saveAgendaDataArrayList() 的方法,它应该将 ArrayList 中的数据保存在 TXT 文件中,如下所示。

public void saveAgendaDataArrayList(String path, ArrayList<Contact> agendaDataArrayList) {
        try {
            if(agendaDataArrayList!=null) {
                File file = new File(path);
                PrintWriter p = new PrintWriter(file);

                int count = agendaDataArrayList.size();

                for(int i=0; i<count; i++) {
                    Contact temp = new Contact();
                    temp = agendaDataArrayList.get(i);
                    p.println(temp.getIdAdress()+";"+temp.getContactType()+";"+temp.getName()+";"+temp.getBirthdayDay()+
                            ";"+temp.getBirthdayMonth()+";"+temp.getBirthdayYear()+";"+temp.getTel1()+";"+temp.getTel2()+
                            ";"+temp.getNeigborhood()+";"+temp.getAddress()+";"+temp.getCep()+";"+temp.getEmail()
                            +";"+temp.getOtherInformation()+";"+temp.getCreationDate()+";");
                }
                p.close();

            } else {
                File file = new File(path);
                PrintWriter p = new PrintWriter(file);
                p.print("empty agenda");
                p.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

但是,当它运行时,我有一些新行来自我不知道从哪里来。往下看

1;1;Guilhermee;00;00;;8666666;;sem bairro;;;;;12-09-2019 04:45:47;

2;1;Gabriella;00;00;;;;Morada do Sol;;;;;12-09-2019 04:45:57;

3;1;joao;00;00;;;;sem bairro;;;;;12-09-2019 05:38:13;

4;1;lua;00;00;;;;sem bairro;;;;;12-09-2019 06:11:15;

5;1;roberto;00;00;;;;sem bairro;;;;;12-09-2019 06:12:22;

6;1;joquina;00;00;;;;Monte Verde;;;;;12-09-2019 07:38:30;

7;1;luan silva;00;00;;;;sem bairro;;;;;12-09-2019 07:40:07;

8;1;manoel;00;00;;89898989;;sem bairro;asdasd;;;;12-09-2019 07:44:44;

9;1;joana;19;01;1954;;;Cidade Jardim;;;;;12-09-2019 07:48:03;

10;1;mariana;00;00;;;;sem bairro;;;;;12-09-2019 07:57:43;

11;1;agoradeucerto;00;00;;;;Morros;;;;;12-09-2019 08:01:46;
12;1;mais uma tentativa;00;00;;;;sem bairro;;;;;12-09-2019 08:43:19;

我想要一个如上所述的输出文件,但没有空行。

我试着用方法 System.out.println() 看看在控制台中是否会发生同样的情况,它也发生在那里。

查看文本文件编辑器,记事本,我注意到在行尾有一些 LF 和 CR LF 混合。

我已经查看了 Contact class 并且一切似乎都是正确的。

那么,我该怎么做才能达到那个结果并避免那些空行,为什么只有最后一行在正确的位置?

感谢您的宝贵时间。

编辑 1 - 输入法

这是输入法。有两种方法可以将数据添加到 agendaDataArrayList。第一种是通过读取 txt 文件(第一种方法),第二种是通过输入界面(第二种方法)。

第一种方法

public ArrayList<Contact> getAgendaDataArrayList(String path) {
        try {
            FileReader reader = new FileReader(path);
            Scanner scanner1 = new Scanner(reader);
            scanner1.useDelimiter("\r\n|\n");
            int count = 0;
            while(scanner1.hasNext()) {
                scanner1.next();
                count++;
            }
            System.out.println(count);
            scanner1.close();
            reader.close();

            ArrayList<Contact> agendaDataArrayList = new ArrayList<Contact>();

            FileReader reader2 = new FileReader(path);
            Scanner scanner2 = new Scanner(reader2);
            scanner2.useDelimiter(";");

            for(int i=0; i<count; i++) {
                Contact temp = new Contact();               
                temp.setIdAdress(scanner2.next());          //[0]  id
                temp.setContactType(scanner2.next());       //[1]  type
                temp.setName(scanner2.next());              //[2]  name
                temp.setBirthdayDay(scanner2.next());       //[3]  birthdayDay
                temp.setBirthdayMonth(scanner2.next());     //[4]  birthdayMonth
                temp.setBirthdayYear(scanner2.next());      //[5]  birthdayYear
                temp.setTel1(scanner2.next());              //[6]  tel1
                temp.setTel2(scanner2.next());              //[7]  tel2
                temp.setNeigborhood(scanner2.next());       //[8]  neighborhood
                temp.setAddress(scanner2.next());           //[9]  address
                temp.setCep(scanner2.next());               //[10] cep
                temp.setEmail(scanner2.next());             //[11] email
                temp.setOtherInformation(scanner2.next());  //[12] other information
                temp.setCreationDate(scanner2.next());      //[13] creation date

                agendaDataArrayList.add(temp);
            }
            scanner2.close();
            reader2.close();
            return agendaDataArrayList;
        } catch (IOException e) {
            e.printStackTrace();
            ArrayList<Contact> agendaDataArrayList = new ArrayList<Contact>();
            return agendaDataArrayList;
        }
    }

第二种方法

    public void saveActionButton() {
        Date creationDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        Contact newContact = new Contact();

        newContact.setIdAdress(mainApp.getNextIdAddress());

        if(typeChoiceBox.getValue()==null) {
            newContact.setContactType("1");
        } else {
            newContact.setContactType(typeChoiceBox.getValue());
        }

        if(nameTextField.getText()==null) {
            newContact.setName("sem nome");
        } else {
            newContact.setName(nameTextField.getText());
        }

        if(dayChoiceBox.getValue()==null) {
            newContact.setBirthdayDay("00");
        }else {
            newContact.setBirthdayDay(dayChoiceBox.getValue());
        }

        if(monthChoiceBox.getValue()==null) {
            newContact.setBirthdayMonth("00");
        }else {
            newContact.setBirthdayMonth(monthChoiceBox.getValue());
        }

        if(yearTextField.getText()==null) {
            newContact.setBirthdayYear("0000");
        }else {
            newContact.setBirthdayYear(yearTextField.getText());
        }

        if(tel1TextField.getText()==null) {
            newContact.setTel1("sem número");
        }else {
            newContact.setTel1(tel1TextField.getText());
        }

        if(tel2TextField.getText()==null) {
            newContact.setTel2("sem número");
        }else {
            newContact.setTel2(tel2TextField.getText());
        }

        if(neighborhoodChoiceBox.getValue()==null) {
            newContact.setNeigborhood("sem bairro");
        } else {
            newContact.setNeigborhood(neighborhoodChoiceBox.getValue());
        }

        if(addressTextField.getText()==null) {
            newContact.setAddress("sem endereço");
        } else {
            newContact.setAddress(addressTextField.getText());
        }

        if(cepTextField.getText()==null) {
            newContact.setCep("sem CEP");
        }else {
            newContact.setCep(cepTextField.getText());
        }

        if(emailTextField.getText()==null) {
            newContact.setEmail("sem e-mail");
        } else {
            newContact.setEmail(emailTextField.getText());
        }

        if(otherInfoTextArea.getText()==null) {
            newContact.setOtherInformation("sem mais informações");
        }else {
            newContact.setOtherInformation(otherInfoTextArea.getText());
        }

        newContact.setCreationDate(formatter.format(creationDate).toString());
        mainApp.addContactToAgendaDataArrayList(newContact);
        mainApp.refreshFullContentInMainLayout();
        mainApp.saveFile();

        Stage stage = (Stage) saveButton.getScene().getWindow();
        stage.close();
    }
}

比较 id 地址为 12 的条目的第一个方法输出和前面有新行的其他条目。

有可能一些数据被插入到 windows(因此是 CR LF 空格)和一些在 unix 系统(仅使用 LF)上。无论如何,数据本身似乎包含换行符,PrinterWriter 可以按您的意愿工作。 小测试:

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

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello");  

        ArrayList<Contact> list = new ArrayList<>();
        list.add(new Contact());
        list.add(new Contact());
        list.add(new Contact());
        list.add(new Contact());
        list.add(new Contact());

        try {
                File file = new File("output.txt");
                PrintWriter p = new PrintWriter(file);

                int count = list.size();

                for (int i = 0; i < count; i++) {
                    Contact temp = list.get(i);
                    p.println(temp.getFavColour() + ";" + temp.getSurname() + ";" + temp.getName() + ";");
                }
                p.close();


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static class Contact {
        public String getName() {
            return "John";
        }

        public String getSurname() {
            return "Black";
        }

        public String getFavColour() {
            return "red";
        }
    }
}