Я пытаюсь создать программу, которая обрабатывает несколько городов в ArrayList, который затем сохраняет их в текстовый файл и загружает данные из того же файла, если он был создан ранее. Проблема в том, что когда я жестко кодирую данные в ArrayList, две первые строки (имя и страна) становятся пустыми, а все остальные в порядке. Спасибо за ваше время. Вот код:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.io.*;
public class Start {
public static void main(String[] args) throws IOException {
City test = new City("Paris","France",0,0,"Sunny",0,123.123,123.123);
City test2 = new City("Tokyo","Japan",0,0,"Sunny",0,123.123,123.123);
ArrayList<City> list=new ArrayList<City>();
list.add(test);
list.add(test2);
File file = new File("test.txt");
if(file.exists()) {
try{
load(file, list);
}catch(IOException e) {
System.out.println("There was an error loading the file");
System.exit(0);
}
System.out.println("Data have been found in the machine and have been loaded succesfully");
}
else {
System.out.println("No data have been found on the machine");
}
City obj;
ListIterator<City> i = list.listIterator();
while(i.hasNext()) {
obj = i.next();
System.out.println(obj.getName()+" "+obj.getCountry());
}
save(file,list);
}
static void load(File fl, List<City> ct) throws IOException {
BufferedReader bf = new BufferedReader(new FileReader(fl));
String str;
String[] objs = new String[8];
City city = new City("nothing", "nothing", 0, 0, "nothing", 0, 0, 0);
while((str=bf.readLine()) != null) {
objs = str.split(" ");
city.setName(objs[0]);
city.setCountry(objs[1]);
city.setCafeRestaurants(Integer.valueOf(objs[2]));
city.setLatitude(Double.valueOf(objs[3]));
city.setLongitude(Double.valueOf(objs[4]));
city.setMuseums(Integer.valueOf(objs[5]));
city.setParks(Integer.valueOf(objs[6]));
city.setWeather(objs[7]);
ct.add(city);
}
bf.close();
}
static void save(File fl, List<City> users) throws IOException {
FileWriter fw = new FileWriter(fl);
City obj;
ListIterator<City> i = users.listIterator();
while(i.hasNext()) {
obj= i.next();
fw.write(obj.getName()+" "+obj.getCountry()+" "+obj.getCafeRestaurants()+" "+obj.getLatitude()+" "+obj.getLongitude()+" "+obj.getMuseums()+" "+obj.getParks()+" "+obj.getWeather()+"\n");
}
fw.close();
}
}
Класс города:
public class City {
String name;
String country;
int museums;
int cafeRestaurants;
String weather;
int parks;
double latitude;
double longitude;
public City(String name,String Country,int museums, int cafeRestaurants, String weather, int parks, double latitude, double longitude) {
this.museums = museums;
this.cafeRestaurants = cafeRestaurants;
this.weather = weather;
this.parks = parks;
this.latitude = latitude;
this.longitude = longitude;
}
public City(int museums, int cafeRestaurants, String weather, int parks) {
super();
this.museums = museums;
this.cafeRestaurants = cafeRestaurants;
this.weather = weather;
this.parks = parks;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getMuseums() {
return museums;
}
public void setMuseums(int museums) {
this.museums = museums;
}
public int getCafeRestaurants() {
return cafeRestaurants;
}
public void setCafeRestaurants(int cafeRestaurants) {
this.cafeRestaurants = cafeRestaurants;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public int getParks() {
return parks;
}
public void setParks(int parks) {
this.parks = parks;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public void setDefault() {
this.setCafeRestaurants(0);
this.setMuseums(0);
this.setParks(0);
this.setWeather("Sunny");
}
}