[英]Storing elements in a text file and calling upon the information
I'm creating a program that basically has information about elements (From the periodic table) such as the atomic mass, number and symbol. 我正在创建一个程序,该程序基本上具有有关元素的信息(来自元素周期表),例如原子质量,数量和符号。 I want to store all the information about them in a text file, but am unsure of how to code it to call upon the information separately (And not just read the text file line by line).
我想将有关它们的所有信息存储在一个文本文件中,但是不确定如何编码以分别调用该信息(而不仅仅是逐行读取文本文件)。
This is the code I'm using for each element so far: 到目前为止,这是我用于每个元素的代码:
private Element(int atomicNumber, double atomicMass, String atomicSymbol, String atomicName) {
this.atomicNumber = atomicNumber;
this.atomicMass = atomicMass;
this.atomicSymbol = atomicSymbol;
this.atomicName = atomicName;
}
In the same file, I was creating an element like this: 在同一文件中,我正在创建一个像这样的元素:
Element H = new Element(1, 1.008, "H", "Hydrogen");
How could I, instead of having all 118 elements in the same file as the code, put them in a text file and read them from there? 我怎么能将所有118个元素与代码放在同一个文件中,而不是将所有118个元素都放在一个文本文件中并从中读取它们?
I'm using methods such as getAtomicNumber() and getAtomicMass(), etc to call upon the information. 我正在使用诸如getAtomicNumber()和getAtomicMass()之类的方法来调用信息。
Thanks. 谢谢。
You have multiple choices, since you can add any structure in a text file. 您可以选择多个选项,因为您可以在文本文件中添加任何结构。 My recomendation would be XML or CSV format.
我的建议是XML或CSV格式。 A CSV is more easy to manage/edit, but the XML is more structured and read friendly.
CSV更加易于管理/编辑,但是XML的结构更易于阅读。 I'm go with a simple CSV.
我使用一个简单的CSV。
Put in your file a line for each Element (that will be readed into your Element class), and each Element attribute (number, mass, symbol and name) separated by comma in a specific order, something like this: 在文件中,为每个元素(将被读入您的Element类)以及每个Element属性(数字,质量,符号和名称)以特定顺序按行分隔,如下所示:
1,1.008,H,Hydrogen
2,5.021,C,Carbon
etc..
Then, your code should read the file, line by line, create instances of your element class and maybe save it in a list. 然后,您的代码应逐行读取文件,创建元素类的实例,然后将其保存在列表中。 Something like this:
像这样:
Your Element Class 您的元素类
public class Element {
private int atomicNumber;
private double atomicMass;
private String atomicSymbol;
private String atomicName;
public Element(int atomicNumber, double atomicMass, String atomicSymbol, String atomicName) {
this.atomicNumber = atomicNumber;
this.atomicMass = atomicMass;
this.atomicSymbol = atomicSymbol;
this.atomicName = atomicName;
}
public double getAtomicMass() {
return atomicMass;
}
public void setAtomicMass(double atomicMass) {
this.atomicMass = atomicMass;
}
public String getAtomicSymbol() {
return atomicSymbol;
}
public void setAtomicSymbol(String atomicSymbol) {
this.atomicSymbol = atomicSymbol;
}
public String getAtomicName() {
return atomicName;
}
public void setAtomicName(String atomicName) {
this.atomicName = atomicName;
}
public int getAtomicNumber() {
return atomicNumber;
}
public void setAtomicNumber(int atomicNumber) {
this.atomicNumber = atomicNumber;
}
@Override
public String toString() {
return "atomicMass: " + atomicMass
+ " | atomicSymbol: " + atomicSymbol
+ " | atomicName: " + atomicName
+ " | atomicNumber: " + atomicNumber;
}
}
Elements file reader 元素文件阅读器
public class ElementsFileReader {
public static List<Element> read(String filePath) throws IOException {
FileReader fileReader = new FileReader(new File(filePath));
BufferedReader br = new BufferedReader(fileReader);
List<Element> elements = new ArrayList<>();
String line = null;
while ((line = br.readLine()) != null) {
String[] lineParts = line.split(",");
int atomicNumber = Integer.parseInt(lineParts[0]);
double atomicMass = Double.parseDouble(lineParts[1]);
String atomicSymbol = lineParts[2];
String atomicName = lineParts[3];
elements.add(new Element(atomicNumber, atomicMass, atomicSymbol, atomicName));
}
return elements;
}
}
Your main application using ElementsFileReader and Element 您的主要应用程序使用ElementsFileReader和Element
public static void main(String[] args){
String filePath = "the path to your text file";
List<Element> elements;
try {
elements = ElementsFileReader.read(filePath);
} catch(IOException e){
System.err.println("Something gone wrong reading elements file...");
e.printStackTrace();
return;
}
for(Element element : elements){
System.out.println(element);
// do your stuff
}
}
Careful with the parseInt and parseDouble method, I'm assuming the right format in your text file. 小心parseInt和parseDouble方法,我假设您的文本文件中使用正确的格式。 Hope this can help you.
希望这可以帮到你。
Write all the elements to a file in the same format (1,1.008,H,Hydrogen), read line by line and create a Pojo object for each line and write it to Map. 将所有元素以相同的格式(1,1.008,H,Hydrogen)写入文件,逐行读取并为每行创建一个Pojo对象,然后将其写入Map。
public Map readFile(String filePath) {
Map<Integer, Element> elementsMap = new HashMap<>();
Scanner s;
try {
s = new Scanner(new File(filePath));
while (s.hasNextLine()){
String[] properties = s.nextLine().split(",");
Element element = new Element();
element.setAtomicNumber(Integer.parseInt(properties[0]));
element.setAtomicMass(Double.parseDouble(properties[1]));
element.setAtomicSymbol(properties[2]);
element.setAtomicName(properties[3]);
elementsMap.put(element.getAtomicNumber(), element);
}
s.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return elementsMap;
}
You can have the Map key as any other unique element property according to your convenience. 根据您的方便,可以将Map键作为任何其他唯一元素属性使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.