数据存储在一个文件的不同行中,如何将它们作为参数加载到一个class?
Data stored in different lines on a file, how to load them as parameters to a class?
我正在尝试做一个小句子翻译器,每个句子都有一个或多个翻译,将从格式如下的文件中加载:
EN – english sentence 1
IT – italian translation 1
EN – english sentence 2
IT – italian translation 1
IT – italian translation 2
这些信息应该作为参数载入class:
public class Sentence {
private String fraseENG;
private ArrayList<String> translations = new ArrayList<>();
private boolean correct;
public void setFraseENG(String fraseENG) {
this.fraseENG = fraseENG;
}
public void setTranslation(String translation){
translations.add(translation);
}
}
我无法决定何时在读取文件时启动 class 语句。
public ArrayList<Sentence> getFrasi() {
ArrayList<Sentence> sentences = new ArrayList<>();
File file = new File(filename);
try {
BufferedReader reader=new BufferedReader(new FileReader(file));
String line=reader.readLine();
while(line!=null){
String[] readLine = line.split("-");
String lang = readLine[0];
String sentence = readLine[1];
Sentence sentenceClass = new Sentence();
if(lang.equals("EN")){
sentenceClass.setFraseENG(sentence);
}else{
sentenceClass.setTranslation(readLine[1]);
}
sentences.add(sentenceClass);
line=reader.readLine();
}
reader.close();
} catch //TODO
return sentences;
}
但是,这样一来,每当我读到新的一行时,我就会有一个新的 class。我应该怎么做?谢谢
嵌套两个 while
循环,外部循环创建新的 Sentence
s,内部循环为当前句子添加翻译。像这样:
String line = reader.readLine();
while(line != null) {
Sentence sentence = new Sentence();
String[] readLine = line.split(" - ");
String lang = readLine[0];
String phrase = readLine[1];
sentence.setFraseENG(phrase);
line = reader.readLine();
while(line != null) {
readLine = line.split(" - ");
lang = readLine[0];
phrase = readLine[1];
if(!"IT".equals(lang)) {
// not a translation, break and create a new sentence
break;
}
sentence.setTranslation(phrase);
line = reader.readLine();
}
sentences.add(sentence);
}
这假定第一行和任何不以 "IT"
开头的内容都是英语短语。
只有在找到英文字符串并在 while 循环外声明它时,才应创建一个新的 Sentence
实例:
Sentence sentenceClass = null;
while(line!=null) {
// file reading part
if (lang.equals("EN")){
sentenceClass = new Sentence();
sentences.add(sentenceClass);
sentenceClass.setFraseENG(sentence);
}
else {
sentenceClass.setTranslation(readLine[1]);
}
line=reader.readLine();
}
代码的编写方式看起来不错。让它每次都是一个新的 class ,这是为每个循环插入 arraylist 的正确方法。垃圾收集器将负责清理冗余对象。
我正在尝试做一个小句子翻译器,每个句子都有一个或多个翻译,将从格式如下的文件中加载:
EN – english sentence 1
IT – italian translation 1
EN – english sentence 2
IT – italian translation 1
IT – italian translation 2
这些信息应该作为参数载入class:
public class Sentence {
private String fraseENG;
private ArrayList<String> translations = new ArrayList<>();
private boolean correct;
public void setFraseENG(String fraseENG) {
this.fraseENG = fraseENG;
}
public void setTranslation(String translation){
translations.add(translation);
}
}
我无法决定何时在读取文件时启动 class 语句。
public ArrayList<Sentence> getFrasi() {
ArrayList<Sentence> sentences = new ArrayList<>();
File file = new File(filename);
try {
BufferedReader reader=new BufferedReader(new FileReader(file));
String line=reader.readLine();
while(line!=null){
String[] readLine = line.split("-");
String lang = readLine[0];
String sentence = readLine[1];
Sentence sentenceClass = new Sentence();
if(lang.equals("EN")){
sentenceClass.setFraseENG(sentence);
}else{
sentenceClass.setTranslation(readLine[1]);
}
sentences.add(sentenceClass);
line=reader.readLine();
}
reader.close();
} catch //TODO
return sentences;
}
但是,这样一来,每当我读到新的一行时,我就会有一个新的 class。我应该怎么做?谢谢
嵌套两个 while
循环,外部循环创建新的 Sentence
s,内部循环为当前句子添加翻译。像这样:
String line = reader.readLine();
while(line != null) {
Sentence sentence = new Sentence();
String[] readLine = line.split(" - ");
String lang = readLine[0];
String phrase = readLine[1];
sentence.setFraseENG(phrase);
line = reader.readLine();
while(line != null) {
readLine = line.split(" - ");
lang = readLine[0];
phrase = readLine[1];
if(!"IT".equals(lang)) {
// not a translation, break and create a new sentence
break;
}
sentence.setTranslation(phrase);
line = reader.readLine();
}
sentences.add(sentence);
}
这假定第一行和任何不以 "IT"
开头的内容都是英语短语。
只有在找到英文字符串并在 while 循环外声明它时,才应创建一个新的 Sentence
实例:
Sentence sentenceClass = null;
while(line!=null) {
// file reading part
if (lang.equals("EN")){
sentenceClass = new Sentence();
sentences.add(sentenceClass);
sentenceClass.setFraseENG(sentence);
}
else {
sentenceClass.setTranslation(readLine[1]);
}
line=reader.readLine();
}
代码的编写方式看起来不错。让它每次都是一个新的 class ,这是为每个循环插入 arraylist 的正确方法。垃圾收集器将负责清理冗余对象。