【发布时间】:2016-10-02 10:03:39
【问题描述】:
我希望将姓名连同日期和时间一起写到一个文件中 使用一个arraylist。但是当我把它写到文件上时……一个单一的名字 with date 出现两次;它与我的暗示有关 StandardOpenOption.Append 因为当我不打印日期和值时 一次,但确实覆盖了我以前的值。][1]
//created method to write date and name onto file
public void standardRoom()
{
try {
Path p=Paths.get("test/standardRoom.text");
if(Files.notExists(p))
{
Files.createFile(p);
}
System.out.println("Enter Name");
String name=sc.next();
List<String>stdrooms=new ArrayList<>();
date = d.toString();
stdrooms.add(date);
stdrooms.add(name);
System.out.println("Standard Room booked");
Iterator<String>bookings=stdrooms.iterator();
while(bookings.hasNext())
{
String s=bookings.next();
//Files.write(p, stdrooms);
Files.write(p, stdrooms,StandardOpenOption.APPEND);
}
} catch (IOException e) {
}
}
}
【问题讨论】:
-
请更正并对齐所有缩进。谢谢。
-
糟糕!我的错误。更正