根据外部文本输入读取和写入多个文件

时间:2018-08-23 04:01:39

标签: java html text gis

我正在寻找一种以特定方式基于外部文本输入编写多个文本文件的方法。目前,我可以使用Java以所需的格式在单个txt文件中写入外部文本数据,但是我需要根据GIS值在单独的文件中写入文本。这意味着,如果GIS与GIS的文件名相同,则所有表都将在一个文件中,并将按GIS名称进一步拆分。

我的外部数据的值超过50000,这是无法手动完成的。请建议使用Perl,shell,PHP脚本是否可以通过其他任何方式实现此目标。

要求写入单独的文件,例如:

A。 BAB.txt:GIS在这里很常见。

 <TABLE>
   <TR><TD>City:</TD><TD><b>BHOPAL</b></TD></TR>
   <TR><TD>Node Name:</TD><TD>BAB-H1</TD></TR>
   <TR><TD>GIS:</TD><TD>BAB</TD></TR>
   <TR><TD>Link:</TD><TD>BAB-H1</TD></TR>
 </TABLE>

 <TABLE>
   <TR><TD>City:</TD><TD><b>BHOPAL</b></TD></TR>
   <TR><TD>Node Name:</TD><TD>BAB-H2</TD></TR>
   <TR><TD>GIS:</TD><TD>BAB</TD></TR>
   <TR><TD>Link:</TD><TD>BAB-H2</TD></TR>
 </TABLE>

 <TABLE>
   <TR><TD>City:</TD><TD><b>BHOPAL</b></TD></TR>
   <TR><TD>Node Name:</TD><TD>BAB-H3</TD></TR>
   <TR><TD>GIS:</TD><TD>BAB</TD></TR>
   <TR><TD>Link:</TD><TD>BAB-H3</TD></TR>
 </TABLE>

B。 RAH.txt:

 <TABLE>
   <TR><TD>City:</TD><TD><b>BHILAI</b></TD></TR>
   <TR><TD>Node Name:</TD><TD>RAH-A1</TD></TR>
   <TR><TD>GIS:</TD><TD>RAH</TD></TR>
   <TR><TD>Link:</TD><TD>RAH-A1</TD></TR>
 </TABLE>

 <TABLE>
   <TR><TD>City:</TD><TD><b>BHILAI</b></TD></TR>
   <TR><TD>Node Name:</TD><TD>RAH-A2</TD></TR>
   <TR><TD>GIS:</TD><TD>RAH</TD></TR>
   <TR><TD>Link:</TD><TD>RAH-A2</TD></TR>
 </TABLE>

我来自文本文件的外部输入:

City,Link,Node Name,GIS
BHILAI,RAH-A1,RAH-A1,RAH
BHILAI,RAH-A2,RAH-A2,RAH
BHILAI,COMBO,RCV-A1,RCV
BHILAI,COMBO,RIA-A1,RIA
BHILAI,MPCG_ALU,RJA-A1,RJA
BHILAI,MPCG_ALU,RJP-A2,RJP
BHILAI,MPCG_ALU,RKU-A1,RKU
BHILAI,COMBO,RNN-A1,RNN
Bhilai,RNN-A4,RNN-A4,RNN
BHOPAL,BAB-H1,BAB-H1,BAB
BHOPAL,BAB-H2,BAB-H2,BAB
BHOPAL,BAB-H3,BAB-H3,BAB
BHOPAL,COMBO,BAB-H4,BAB
BHOPAL,COMBO,BAB-H5,BAB

Java输出:当前正在单个文件中写入

 <TABLE>
   <TR><TD>City:</TD><TD><b>BHILAI</b></TD></TR>
   <TR><TD>Node Name:</TD><TD>RAH-A1</TD></TR>
   <TR><TD>GIS:</TD><TD>RAH</TD></TR>
   <TR><TD>Link:</TD><TD>RAH-A1</TD></TR>
 </TABLE>

 <TABLE>
   <TR><TD>City:</TD><TD><b>BHILAI</b></TD></TR>
   <TR><TD>Node Name:</TD><TD>RAH-A2</TD></TR>
   <TR><TD>GIS:</TD><TD>RAH</TD></TR>
   <TR><TD>Link:</TD><TD>RAH-A2</TD></TR>
 </TABLE>

 <TABLE>
   <TR><TD>City:</TD><TD><b>BHILAI</b></TD></TR>
   <TR><TD>Node Name:</TD><TD>RCV-A1</TD></TR>
   <TR><TD>GIS:</TD><TD>RCV</TD></TR>
   <TR><TD>Link:</TD><TD>COMBO</TD></TR>
 </TABLE>

 <TABLE>
   <TR><TD>City:</TD><TD><b>BHILAI</b></TD></TR>
   <TR><TD>Node Name:</TD><TD>RIA-A1</TD></TR>
   <TR><TD>GIS:</TD><TD>RIA</TD></TR>
   <TR><TD>Link:</TD><TD>COMBO</TD></TR>
 </TABLE>

Java:

import 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();
                }
    }
}

1 个答案:

答案 0 :(得分:0)

根据提供的数据文件中内容的实际数量,我认为最简单的方法是按顺序进行。

循环中一次处理一条数据行,每一行:

  1. 读入数据行(因为它是标题行,所以忽略第一行);
  2. 将数据行拆分为字符串数组;
  3. 确定所需的文件名(即GIS +“ .txt”);
  4. 该文件已经存在吗?如果没有创建;
  5. 将字符串数组数据元素放入HTML字符串模板;
  6. 将HTML模板字符串写入GIS确定的文件中;
  7. 处理下一个数据文件行(循环-再次从#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);
        }
    }
}

注意:由于使用了Try-With-Resources,因此上面代码中的读写器会自动关闭。此功能仅在 Java 7 + 中可用。