【问题标题】:Read and write in multiple file based on external text inputs基于外部文本输入在多个文件中读写
【发布时间】:2018-08-23 04:01:38
【问题描述】:

我正在寻找一种基于外部文本输入以特定方式编写多个文本文件的方法。目前,我可以使用 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();
                }
    }
}

【问题讨论】:

  • 注意:Java 和 Javascript 是不同的语言,Java 不是脚本语言。
  • 无法理解奉献的理由?我的问题是与 Java 还是 JavaScript 有关,没有争议,我在这里可能错了..但我正在寻找我的要求的解决方案。
  • “无法理解投入的理由?” DYM 否决票?如果是这样,您必须询问投反对票的人。

标签: java html text gis


【解决方案1】:

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

循环中一次处理一个数据行,并且每行:

  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+ 中可用。

【讨论】:

  • 您好 DevilsHnd 感谢您的更新。不幸的是,由于我试图修复多个错误,我无法运行此代码。我是java新手,所以需要时间。
  • 有什么错误?提供的代码已经过测试并且功能正常。
  • Hello DevilsHnd 我在使用 javac 编译类文件时遇到多个错误。这里我需要一些指导来运行这段代码。
  • 在您的 Java IDE 中运行代码,直到您知道如何使用 javac.exe 和 java.exe。
  • 您遇到的错误很可能是因为代码中使用了Try-With-Resouces 语句。此功能仅在 Java 7+ 中可用。您很可能使用 Java 6- 进行编译。下载并安装 Java 8,然后使用更新的 JDK (1.8.0.xxx) 进行编译,您的错误应该会消失。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
  • 2016-08-14
  • 1970-01-01
相关资源
最近更新 更多