似乎我也无法读取文件。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package daos;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.City;
import model.YearData;
import repositories.Repository;
/**
*
* @author mga
*/
public class DAOTextImpl implements DAOInterface {
static final char DELIMITER = ',';
ArrayList yeardata=new ArrayList<>();
@Override
public Repository load(String filename) {
Repository repository = new Repository();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String[] temp;
String line = br.readLine();
while (line != null) {
temp = line.split(Character.toString(DELIMITER));
int id = Integer.parseInt(temp[0]);
String cityName = stripQuotes(temp[1]);
String country = stripQuotes(temp[2]);
int noOfLines = Integer.parseInt(temp[2]);
// for (int i = 0; i < noOfLines; i++)
// {
line = br.readLine();
temp = line.split(Character.toString(DELIMITER));
String year = stripQuotes(temp[0]);
float precipitation = Float.parseFloat(temp[1]);
int maxTemp = Integer.parseInt(temp[2]);
int minTemp = Integer.parseInt(temp[3]);
int windSpeed = Integer.parseInt(temp[4]);
String windDirection = stripQuotes(temp[5]);
yeardata.add(year);
yeardata.add(precipitation);
yeardata.add(maxTemp);
yeardata.add(minTemp);
yeardata.add(windSpeed);
yeardata.add(windDirection);
City city=new City(id,cityName,country,yeardata);
// }
repository.add(city);
line = br.readLine();
}
br.close();
} catch (IOException ex) {
Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return repository;
}
@Override
public void store(String filename, Repository repository) {
try (PrintWriter output = new PrintWriter(filename)) {
output.print(repository.toString());
output.print(repository);
output.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
private String stripQuotes(String str) {
return str.substring(1, str.length() - 1);
}
}
我的存储库类如下:
package repositories;
import daos.DAOTextImpl;
import java.util.ArrayList;
import java.util.function.Predicate;
import model.City;
import model.YearData;
public class Repository implements RepositoryInterface {
private ArrayList<City> items;
static char DELIMITER=',';
public Repository() {
this.items = new ArrayList<>();
}
public Repository(ArrayList <City> items) {
this.items = items;
}
public Repository(String filename) {
this();
// Create dao and execute load
DAOTextImpl dao = new DAOTextImpl();
}
@Override
public ArrayList<City> getItems() {
return this.items;
}
@Override
public void setItems(ArrayList<City> items) {
this.items = items;
}
@Override
public void add(City items) {
this.items.add(items);
}
@Override
public void remove(int id) {
Predicate<City> predicate = e->e.getId() == id;
this.items.removeIf(predicate);
}
@Override
public City getItem(int id) {
for (City item:this.items) {
if (item.getId() == id)
return item;
}
return null;
}
@Override
public String toString() {
System.out.println(items);
return "\nItems: " + this.items;
}
@Override
public void store(String filename) {
// create dao and execute store
DAOTextImpl dao = new DAOTextImpl();
dao.store(filename, this);
}
}
我已经对该程序进行了压力测试,并在其中插入了多个output.print(repository.toString());
语句。例如,插入20条output.print(repository.toString());
语句将插入生成的文件20 items:[]
中,这会使我认为Repository类/接口内有错误,但我终生都会我完全不确定这是为什么/为什么产生的。
我不确定当前似乎没有与文件正确读取有关的事实,还是一个错误只是掩盖了另一个错误。
由于我不确定导致问题的原因,我觉得我需要包括完整的代码长度,如果只是为了便于进行错误检查的话。