循环 .txt 文件以创建和填充 .html 文件
Loop on .txt files to create and fill .html files
所以我的问题是我有一个 .txt 文件,其中包含我从另一个名为“name.txt”的 .txt 文件中检索到的名称和数据。
然后我循环处理每个文件名,并为每个文件创建一个 .html 文件,该文件结合了 html 骨架和我从 .txt 文件中检索到的数据。
第一个 .html 文件已正确填充,循环也很好(我有 5 个名字,它循环了 5 次)但只有第一个 .html 文件已填充,其他的是空的,我不能不知道为什么。
这是带有名称 (staff.txt) 的 txt:
cberthier
sconnor
jmacclane
agent1
Dorian
我创建的 name.txt 个文件之一:
Berthier
Corinne
Surveillance entrepôt
pmNd1ldFE7WTk
kit
lampe
lacrymo
这是我的代码:
public void ajouterInfosAgent() throws IOException {
BufferedReader squeletteHtml = new BufferedReader(new FileReader("item-agent.html"));
BufferedReader staffTxt = new BufferedReader(new FileReader("src/main/Txt/staff.txt"));
// for each line in staff.txt
String ligne;
while ((ligne = staffTxt.readLine()) != null) {
// substring on the end of the filename to remove the extension
ligne.substring(ligne.length() - 5);
// Creation of the html file
BufferedReader txtFile = new BufferedReader(new FileReader("src/main/Txt/" + ligne + ".txt"));
File htmlFile = new File("src/main/Html/Generation/Agents/" + ligne + ".html");
// Creation of a writer to fill the html file
BufferedWriter writer = new BufferedWriter(new FileWriter(htmlFile));
// first line of the txt is the agent's last name
String nomAgent = txtFile.readLine();
// then I take the first name
String prenomAgent = txtFile.readLine();
// and then his mission
String missionAgent = txtFile.readLine();
// we pass the pwd and blank line
int i = 0;
while(i < 2) {
txtFile.readLine();
i++;
}
// we take the agent's equipment
StringBuilder equipementAgent = new StringBuilder();
String equipement;
while((equipement = txtFile.readLine()) != null) {
equipementAgent.append("<br><input type=\"checkbox\" name=\"\" checked>\n" + " <label>").append(equipement);
equipementAgent.append("</label>");
}
String ligneSquelette;
String strRecherche = "id=\"main-container\">";
// for each line of the HTML skeleton
while ((ligneSquelette = squeletteHtml.readLine()) != null) {
writer.write(ligneSquelette);
// I split the line in words
String[] words = ligneSquelette.split(" ");
// for each word
for (String word : words)
{
// if the word is the same as the string I'm searching for
if (word.equals(strRecherche))
{
// I add the last name, first name, the mission and the agent's equipment
writer.write(nomAgent + " " +prenomAgent + " - " + missionAgent + " ");
writer.write(String.valueOf(equipementAgent));
}
}
}
writer.flush();
writer.close();
}
System.out.println("\nAgent's informations added");
}
感谢 David Conrad,我发现我只需要将 HTML 骨架放入我的 while 循环中。
所以我的问题是我有一个 .txt 文件,其中包含我从另一个名为“name.txt”的 .txt 文件中检索到的名称和数据。
然后我循环处理每个文件名,并为每个文件创建一个 .html 文件,该文件结合了 html 骨架和我从 .txt 文件中检索到的数据。
第一个 .html 文件已正确填充,循环也很好(我有 5 个名字,它循环了 5 次)但只有第一个 .html 文件已填充,其他的是空的,我不能不知道为什么。
这是带有名称 (staff.txt) 的 txt:
cberthier
sconnor
jmacclane
agent1
Dorian
我创建的 name.txt 个文件之一:
Berthier
Corinne
Surveillance entrepôt
pmNd1ldFE7WTk
kit
lampe
lacrymo
这是我的代码:
public void ajouterInfosAgent() throws IOException {
BufferedReader squeletteHtml = new BufferedReader(new FileReader("item-agent.html"));
BufferedReader staffTxt = new BufferedReader(new FileReader("src/main/Txt/staff.txt"));
// for each line in staff.txt
String ligne;
while ((ligne = staffTxt.readLine()) != null) {
// substring on the end of the filename to remove the extension
ligne.substring(ligne.length() - 5);
// Creation of the html file
BufferedReader txtFile = new BufferedReader(new FileReader("src/main/Txt/" + ligne + ".txt"));
File htmlFile = new File("src/main/Html/Generation/Agents/" + ligne + ".html");
// Creation of a writer to fill the html file
BufferedWriter writer = new BufferedWriter(new FileWriter(htmlFile));
// first line of the txt is the agent's last name
String nomAgent = txtFile.readLine();
// then I take the first name
String prenomAgent = txtFile.readLine();
// and then his mission
String missionAgent = txtFile.readLine();
// we pass the pwd and blank line
int i = 0;
while(i < 2) {
txtFile.readLine();
i++;
}
// we take the agent's equipment
StringBuilder equipementAgent = new StringBuilder();
String equipement;
while((equipement = txtFile.readLine()) != null) {
equipementAgent.append("<br><input type=\"checkbox\" name=\"\" checked>\n" + " <label>").append(equipement);
equipementAgent.append("</label>");
}
String ligneSquelette;
String strRecherche = "id=\"main-container\">";
// for each line of the HTML skeleton
while ((ligneSquelette = squeletteHtml.readLine()) != null) {
writer.write(ligneSquelette);
// I split the line in words
String[] words = ligneSquelette.split(" ");
// for each word
for (String word : words)
{
// if the word is the same as the string I'm searching for
if (word.equals(strRecherche))
{
// I add the last name, first name, the mission and the agent's equipment
writer.write(nomAgent + " " +prenomAgent + " - " + missionAgent + " ");
writer.write(String.valueOf(equipementAgent));
}
}
}
writer.flush();
writer.close();
}
System.out.println("\nAgent's informations added");
}
感谢 David Conrad,我发现我只需要将 HTML 骨架放入我的 while 循环中。