根据提供的数据文件中的实际内容量,我认为最简单的方法是按顺序进行。
在a内一次处理一条数据线
的
环
</强>
并与每一行:
读入数据行(忽略第一行,因为它是标题行);
将数据行拆分为字符串数组;
确定所需的文件名(即:GIS +“。txt”);
该文件是否已存在?如果不创造它;
将字符串数组数据元素放入HTML字符串模板中;
将HTML模板字符串写入GIS确定的文件;
处理下一个数据文件行(循环 - 再次从#1开始)。
package maketestcfg_cm;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;public class MakeTestCfg_CM {
public static void main(String[] args) {
String suppliedDataFile = args[0];
if (suppliedDataFile.isEmpty() || suppliedDataFile.equals("")) {
return;
}
// Variable to hold the actual System line separator
String ls = System.lineSeparator();
// A html template used to write to files in a single write.
// Note we use tags instead of variables. These tags are
// replaced when we write to whichever file. The tags are:
// %c% = City | %l% = Link | %n% = Node Name | %g% = GIS
String htmlTemplate = " <TABLE>" + ls +
" <TR><TD>City:</TD><TD><b>%c%</b></TD></TR>" + ls +
" <TR><TD>Node Name:</TD><TD>%n%</TD></TR>" + ls +
" <TR><TD>GIS:</TD><TD>%g%</TD></TR>" + ls +
" <TR><TD>Link:</TD><TD>%l%</TD></TR>" + ls +
" </TABLE>" + ls + ls;
try {
// Prepare to read the data file.
// Place each line in the data file into a ArrayList.
List<String> list = new ArrayList<>();
String line;
int counter = 0;
try (BufferedReader input = new BufferedReader(new FileReader(suppliedDataFile))) {
while ((line = input.readLine()) != null) {
// Ignore lines that contain nothing and ignore
// the first line which is a CSV header line.
line = line.trim();
if (line.equals("") || counter == 0) {
counter++;
continue;
}
list.add(line);
}
}
// Each data file line is now contained within a
// List interface named 'list'. Now we process
// each line (List element):
for (int i = 0; i < list.size(); i++) {
// Parse the current line into a String Array
String[] City_Link_NodeName_GIS = list.get(i).split(",");
// Get the GIS & trim off leading/trailing spaces (if any).
String gis = City_Link_NodeName_GIS[3].trim();
// Create the file name
String currentFileName = gis + ".txt";
File file = new File(currentFileName);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// Append to file...
try ( FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw)) {
// Here we replace the tags in template
// with proper values. Notice that all element
// data is trimmed of leading/trailing whitespace
// (just in case).
bw.write(htmlTemplate.
replace("%c%", City_Link_NodeName_GIS[0].trim()).
replace("%n%", City_Link_NodeName_GIS[2].trim()).
replace("%g%", City_Link_NodeName_GIS[3].trim()).
replace("%l%", City_Link_NodeName_GIS[1].trim()));
}
}
}
catch (FileNotFoundException ex) {
Logger.getLogger(MakeTestCfg_CM.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
Logger.getLogger(MakeTestCfg_CM.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
</code>
</醇>
下面的可运行代码基本上就是这样:
注意:上面代码中的读者和作者会自动关闭,因为
的
尝试与 - 资源
</强>
用来。此功能仅提供
的
Java 7+
</强>
。