Java 基于外部文本输入在多个文件中读写
我正在寻找一种方法来编写多个文本文件的基础上,外部文本输入的具体方式。目前,我能够使用Java以所需格式在单个txt文件中写入外部文本数据,但我需要基于GIS值在单独的文件中写入文本。这意味着,如果GIS文件名为GIS,则所有表都将位于单个文件中,并将根据GIS名称进一步拆分 我的外部数据有超过50000个值,这是手动无法实现的。请建议是否可以使用Perl、shell、PHP脚本以任何其他方式实现这一点 要求在单独的文件中写入,如: 答:地理信息系统在这里很常见Java 基于外部文本输入在多个文件中读写,java,html,text,gis,Java,Html,Text,Gis,我正在寻找一种方法来编写多个文本文件的基础上,外部文本输入的具体方式。目前,我能够使用Java以所需格式在单个txt文件中写入外部文本数据,但我需要基于GIS值在单独的文件中写入文本。这意味着,如果GIS文件名为GIS,则所有表都将位于单个文件中,并将根据GIS名称进一步拆分 我的外部数据有超过50000个值,这是手动无法实现的。请建议是否可以使用Perl、shell、PHP脚本以任何其他方式实现这一点 要求在单独的文件中写入,如: 答:地理信息系统在这里很常见 城市:博帕尔 节点名称:BAB
城市:博帕尔
节点名称:BAB-H1
地理信息系统:巴布
链接:BAB-H1
城市:博帕尔
节点名称:BAB-H2
地理信息系统:巴布
链接:BAB-H2
城市:博帕尔
节点名称:BAB-H3
地理信息系统:巴布
链接:BAB-H3
B.RAH.txt:
城市:比莱
节点名称:RAH-A1
地理信息系统:RAH
链接:RAH-A1
城市:比莱
节点名称:RAH-A2
地理信息系统:RAH
链接:RAH-A2
我的外部输入来自文本文件:
Java输出:当前在单个文件中写入
城市:比莱
节点名称:RAH-A1
地理信息系统:RAH
链接:RAH-A1
城市:比莱
节点名称:RAH-A2
地理信息系统:RAH
链接:RAH-A2
城市:比莱
节点名称:RCV-A1
地理信息系统:RCV
链接:组合
城市:比莱
节点名称:RIA-A1
地理信息系统:RIA
链接:组合
爪哇:
import java.io.*;
导入java.util.*;
导入java.lang.*;
公共类MakeTestCfg\u CM
{
公共静态void main(字符串[]args)
{
试一试{
BufferedReader输入=新BufferedReader(新文件读取器(args[0]);
FileWriter outFile=新的FileWriter(“table.txt”);
PrintWriter out=新的PrintWriter(输出文件);
字符串行=null;
字符串city=null;
字符串链接=null;
字符串nodename=null;
字符串gis=null;
而((line=input.readLine())!=null)
{
StringTokenizer strT=新的StringTokenizer(行,“,”);
city=strT.nextToken();
link=strT.nextToken();
nodename=strT.nextToken();
gis=strT.nextToken();
out.println(“”);
out.println(“城市:+City+”);
println(“节点名:+nodename+”);
out.println(“GIS:+GIS+”);
out.println(“链接:+Link+”);
out.println(“”);
out.println();
}
input.close();
out.close();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
}
根据提供的数据文件中的实际内容量,我认为最简单的方法是按顺序进行
在循环中每次处理一条数据线,每条数据线:
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);
}
}
}
package-maketestcfg\u-cm;
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.logging.Level;
导入java.util.logging.Logger;
公共类MakeTestCfg\u CM{
公共静态void main(字符串[]args){
字符串suppliedDataFile=args[0];
if(suppliedDataFile.isEmpty()| | suppliedDataFile.equals(“”){
返回;
}
//用于保存实际系统行分隔符的变量
字符串ls=System.lineSeparator();
//用于在一次写入中写入文件的html模板。
//注意,我们使用标签而不是变量
//在写入任何文件时替换。标记为:
//%c%=城市|%l%=链接|%n%=节点名称|%g%=GIS
字符串htmlTemplate=“+ls+
“城市:%c%”+ls+
“节点名称:%n%”+ls+
“地理信息系统:%g%”+ls+
“链接:%l%”+ls+
“+ls+ls;
试一试{
//准备读取数据文件。
//将数据文件中的每一行放入ArrayList。
列表=新的ArrayList();
弦线;
int计数器=0;
try(BufferedReader input=new BufferedReader(new FileReader(suppliedDataFile))){
而((line=input.readLine())!=null){
//忽略不包含任何内容的行并忽略
//第一行是CSV标题行。
line=line.trim();
如果(行等于(“”| |计数器==0){
计数器++;
继续;
}
列表。添加(行);
}
}
//现在,每个数据文件行都包含在
//列表接口名为“List”。现在我们处理
//每行(列表元素):
对于(int i=0;iimport java.io.*;
import java.util.*;
import java.lang.*;
public class MakeTestCfg_CM
{
public static void main(String[] args)
{
try {
BufferedReader input = new BufferedReader(new FileReader(args[0]));
FileWriter outFile = new FileWriter("table.txt");
PrintWriter out = new PrintWriter(outFile);
String line = null;
String city = null;
String link = null;
String nodename = null;
String gis = null;
while (( line = input.readLine()) != null)
{
StringTokenizer strT = new StringTokenizer(line,",");
city = strT.nextToken();
link = strT.nextToken();
nodename = strT.nextToken();
gis = strT.nextToken();
out.println(" <TABLE>");
out.println(" <TR><TD>City:</TD><TD><b>"+city+"</b></TD></TR>");
out.println(" <TR><TD>Node Name:</TD><TD>"+nodename+"</TD></TR>");
out.println(" <TR><TD>GIS:</TD><TD>"+gis+"</TD></TR>");
out.println(" <TR><TD>Link:</TD><TD>"+link+"</TD></TR>");
out.println(" </TABLE>");
out.println();
}
input.close();
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
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);
}
}
}