基于外部文本输入读写多个文件


誓言
2025-03-18 09:12:26 (3天前)
  1. 我正在寻找一种基于外部文本输入以特定方式编写多个文本文件的方法。目前我能够使用Java以所需格式在单个txt文件中编写外部文本数据,但我...

2 条回复
  1. 0# 妖邪 | 2019-08-31 10-32



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



    在a内一次处理一条数据线


    </强>
    并与每一行:




    1. 读入数据行(忽略第一行,因为它是标题行);


    2. 将数据行拆分为字符串数组;


    3. 确定所需的文件名(即:GIS +“。txt”);


    4. 该文件是否已存在?如果不创造它;


    5. 将字符串数组数据元素放入HTML字符串模板中;


    6. 将HTML模板字符串写入GIS确定的文件;


    7. 处理下一个数据文件行(循环 - 再次从#1开始)。

    8. </醇>


      下面的可运行代码基本上就是这样:




      1. package maketestcfg_cm;

      2. 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;

      3. public class MakeTestCfg_CM {

      4. public static void main(String[] args) {
      5.     String suppliedDataFile = args[0];
      6.     if (suppliedDataFile.isEmpty() || suppliedDataFile.equals("")) {
      7.         return;
      8.     }
      9.     // Variable to hold the actual System line separator
      10.     String ls = System.lineSeparator();
      11.     // A html template used to write to files in a single write.
      12.     // Note we use tags instead of variables. These tags are 
      13.     // replaced when we write to whichever file. The tags are:
      14.     // %c% = City | %l% = Link | %n% = Node Name | %g% = GIS
      15.     String htmlTemplate = " <TABLE>" + ls + 
      16.                           "   <TR><TD>City:</TD><TD><b>%c%</b></TD></TR>" + ls +
      17.                           "   <TR><TD>Node Name:</TD><TD>%n%</TD></TR>" + ls + 
      18.                           "   <TR><TD>GIS:</TD><TD>%g%</TD></TR>" + ls + 
      19.                           "   <TR><TD>Link:</TD><TD>%l%</TD></TR>" + ls +
      20.                           " </TABLE>" + ls + ls;
      21.     try {
      22.         // Prepare to read the data file.
      23.         // Place each line in the data file into a ArrayList.
      24.         List<String> list = new ArrayList<>();
      25.         String line;
      26.         int counter = 0;
      27.         try (BufferedReader input = new BufferedReader(new FileReader(suppliedDataFile))) {
      28.             while ((line = input.readLine()) != null) {
      29.                 // Ignore lines that contain nothing and ignore
      30.                 // the first line which is a CSV header line.
      31.                 line = line.trim();
      32.                 if (line.equals("") || counter == 0) {
      33.                     counter++;
      34.                     continue;
      35.                 }
      36.                 list.add(line);
      37.             }
      38.         }
      39.         // Each data file line is now contained within a 
      40.         // List interface named 'list'. Now we process
      41.         // each line (List element):
      42.         for (int i = 0; i < list.size(); i++) {
      43.             // Parse the current line into a String Array
      44.             String[] City_Link_NodeName_GIS = list.get(i).split(",");
      45.             // Get the GIS & trim off leading/trailing spaces (if any).
      46.             String gis = City_Link_NodeName_GIS[3].trim();
      47.             // Create the file name
      48.             String currentFileName = gis + ".txt";
      49.             File file = new File(currentFileName);
      50.             // if file doesnt exists, then create it
      51.             if (!file.exists()) {
      52.                 file.createNewFile();
      53.             }
      54.             // Append to file...
      55.             try ( FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); 
      56.                   BufferedWriter bw = new BufferedWriter(fw)) {
      57.                   // Here we replace the tags in template 
      58.                   // with proper values. Notice that all element
      59.                   // data is trimmed of leading/trailing whitespace
      60.                   // (just in case).
      61.                   bw.write(htmlTemplate.
      62.                         replace("%c%", City_Link_NodeName_GIS[0].trim()).
      63.                         replace("%n%", City_Link_NodeName_GIS[2].trim()).
      64.                         replace("%g%", City_Link_NodeName_GIS[3].trim()).
      65.                         replace("%l%", City_Link_NodeName_GIS[1].trim()));
      66.             }
      67.         }
      68.     }
      69.     catch (FileNotFoundException ex) {
      70.         Logger.getLogger(MakeTestCfg_CM.class.getName()).log(Level.SEVERE, null, ex);
      71.     }
      72.     catch (IOException ex) {
      73.         Logger.getLogger(MakeTestCfg_CM.class.getName()).log(Level.SEVERE, null, ex);
      74.     }
      75. }
      76. }

      77. </code>


      注意:上面代码中的读者和作者会自动关闭,因为


      尝试与 - 资源
      </强>

      用来。此功能仅提供

      Java 7+
      </强>


登录 后才能参与评论