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

我有一个名为的方法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;


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


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


在文本文件编辑器(记事本)中查看时,我注意到行尾有一些 LF 与 CR LF 混合在一起。


我回顾了Contact课程,一切似乎都是正确的。


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


感谢您的时间。


当年话下
浏览 70回答 1
1回答

三国纷争

将具有 id 地址的条目的第一个方法输出12与前面有换行符的其他条目进行比较。可能有些数据是在 Windows 上插入的(因此需要 CR LF 空白),有些数据是在 unix 系统上插入的(仅使用 LF)。不管怎样,看起来数据本身包含新的线标记,可以PrinterWriter按照你想要的方式工作。一个小测试:import java.util.ArrayList;import java.io.*;public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Hello");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Contact> list = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Contact());&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Contact());&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Contact());&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Contact());&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Contact());&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File file = new File("output.txt");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PrintWriter p = new PrintWriter(file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int count = list.size();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < count; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Contact temp = list.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p.println(temp.getFavColour() + ";" + temp.getSurname() + ";" + temp.getName() + ";");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p.close();&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static class Contact {&nbsp; &nbsp; &nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "John";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public String getSurname() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "Black";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public String getFavColour() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "red";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java