数据结构文件输入输出
Data structure file input output
我正在尝试编写一个使用 ArrayLists 和文件输入输出的简单程序的程序。
该程序应读取文本文件并将每个字段存储在数组列表中,然后执行计算。该文件包含城市名称、人口和国家名称。即(伦敦,7500000,英国)
当我 运行 带有命令行参数的程序时,它应该打印语句说明有多少城市的人口大于命令行参数。
我希望有人能指导我正确的方向。谢谢。
没有给出'ready to run'答案,我认为它应该是这样的(伪代码)
public static void main(String[] args) {
convert first argument to integer
set up a reader for your file
while (another line is found) {
split the line by comma
convert second item in array to an integer
if integer >= integer passed in args
print current line
}
}
请记住,您必须进行异常处理(IOExceptions、NumberFormatException 等)。我故意不包括保存到数组列表,因为这意味着你必须循环两次(一次遍历文件,一次遍历列表)这是没有必要的。
这是一个很好的学校练习:-)
假设您在 C:\temp\cities.txt 中的输入文件包含:
London,83000000,UK
Paris,22000000,France
Madrid,32000000,Spain
以下classTest.java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
private static ArrayList<City> loadDataFromFile() throws IOException {
ArrayList<City> list = new ArrayList<City>();
BufferedReader br = null;
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\temp\cities.txt"));
while ((sCurrentLine = br.readLine()) != null) {
String[] splittedLine = sCurrentLine.split(",");
City city = new City(splittedLine[0], Integer.parseInt(splittedLine[1]), splittedLine[2]);
list.add(city);
}
br.close();
return list;
}
public static void main(String[] args) throws IOException {
ArrayList<City> list = loadDataFromFile();
int input = Integer.parseInt(args[0]);
Iterator<City> it = list.iterator();
while (it.hasNext()) {
City currentCity = it.next();
if (currentCity.getPopulation() > input) {
System.out.println("Cities with population greater than " + input + " : " + currentCity.getName() + "," + currentCity.getCountry());
} else if (currentCity.getPopulation() == input) {
System.out.println("City with population equals to " + input + " : " + currentCity.getName() + "," + currentCity.getCountry());
} else {
System.out.println("Cities with population lower than " + input + " : " + currentCity.getName() + "," + currentCity.getCountry());
}
}
}
}
和一个简单的 POJO :
public class City {
private String name;
private int population;
private String country;
public City(String name, int population, String country) {
this.name = name;
this.population = population;
this.country = country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
会给你:
java Test 3200000
Cities with population greater than 32000000 : London,UK
Cities with population lower than 32000000 : Paris,France
City with population equals to 32000000 : Madrid,Spain
我正在尝试编写一个使用 ArrayLists 和文件输入输出的简单程序的程序。 该程序应读取文本文件并将每个字段存储在数组列表中,然后执行计算。该文件包含城市名称、人口和国家名称。即(伦敦,7500000,英国) 当我 运行 带有命令行参数的程序时,它应该打印语句说明有多少城市的人口大于命令行参数。
我希望有人能指导我正确的方向。谢谢。
没有给出'ready to run'答案,我认为它应该是这样的(伪代码)
public static void main(String[] args) {
convert first argument to integer
set up a reader for your file
while (another line is found) {
split the line by comma
convert second item in array to an integer
if integer >= integer passed in args
print current line
}
}
请记住,您必须进行异常处理(IOExceptions、NumberFormatException 等)。我故意不包括保存到数组列表,因为这意味着你必须循环两次(一次遍历文件,一次遍历列表)这是没有必要的。
这是一个很好的学校练习:-)
假设您在 C:\temp\cities.txt 中的输入文件包含:
London,83000000,UK
Paris,22000000,France
Madrid,32000000,Spain
以下classTest.java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
private static ArrayList<City> loadDataFromFile() throws IOException {
ArrayList<City> list = new ArrayList<City>();
BufferedReader br = null;
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\temp\cities.txt"));
while ((sCurrentLine = br.readLine()) != null) {
String[] splittedLine = sCurrentLine.split(",");
City city = new City(splittedLine[0], Integer.parseInt(splittedLine[1]), splittedLine[2]);
list.add(city);
}
br.close();
return list;
}
public static void main(String[] args) throws IOException {
ArrayList<City> list = loadDataFromFile();
int input = Integer.parseInt(args[0]);
Iterator<City> it = list.iterator();
while (it.hasNext()) {
City currentCity = it.next();
if (currentCity.getPopulation() > input) {
System.out.println("Cities with population greater than " + input + " : " + currentCity.getName() + "," + currentCity.getCountry());
} else if (currentCity.getPopulation() == input) {
System.out.println("City with population equals to " + input + " : " + currentCity.getName() + "," + currentCity.getCountry());
} else {
System.out.println("Cities with population lower than " + input + " : " + currentCity.getName() + "," + currentCity.getCountry());
}
}
}
}
和一个简单的 POJO :
public class City {
private String name;
private int population;
private String country;
public City(String name, int population, String country) {
this.name = name;
this.population = population;
this.country = country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
会给你:
java Test 3200000
Cities with population greater than 32000000 : London,UK
Cities with population lower than 32000000 : Paris,France
City with population equals to 32000000 : Madrid,Spain