java - 通过查找特定行将其写在文本文档中的特定位置

标签 java

在文本文档中找到此行 Min:30 后,我正在尝试执行以下操作 我想在新的 stop mon-fri 行之后写它text documnet 或者如果它可能在同一个文本文档中,然后删除空的换行符。

感谢任何帮助。

简单:

4
stop mon-fri
dominant 05:02 05:32 06:02 06:32 07:02 07:32 08:02 17:02 17:32 18:02 18:32 19:02 19:32
death Radbruch-Platz 05:05 05:35 06:05 06:35 07:05 07:35 08:05 17:05 17:35 18:05 18:35 19:05 19:35
operator 05:08 05:38 06:09 06:39 07:09 07:39 08:09 17:09 17:39 18:09 18:39 19:09 19:39
Min:30
apologized 05:09 05:39 06:11 06:41 07:11 07:41 08:11 17:11 17:41 18:11 18:41 19:11 19:41
cooperate 05:16 05:46 06:21 06:51 07:21 07:51 08:21 17:21 17:51 18:21 18:51 19:21 19:51
4
stop mon-fri
government computers 04:32 05:07 05:37 06:03 06:33 07:03 07:26 08:03 18:33 19:03 19:33
WASHINGTON 04:34 05:09 05:39 06:05 06:35 07:05 07:28 08:05 18:35 19:05 19:35
suspected 04:38 05:13 05:43 06:11 06:41 07:11 07:34 08:11 18:41 19:11 19:41
Min:15
Chinese 04:43 05:18 05:48 06:15 06:45 07:15 07:24 07:38 08:15 18:45 19:15 19:45
hackers  04:50 05:25 05:55 06:25 06:55 07:25 07:34 07:48 08:25 18:55 19:25 19:55

结果应该是这样的

4
stop mon-fri
Min:30
dominant 05:02 05:32 06:02 06:32 07:02 07:32 08:02 17:02 17:32 18:02 18:32 19:02 19:32
death Radbruch-Platz 05:05 05:35 06:05 06:35 07:05 07:35 08:05 17:05 17:35 18:05 18:35 19:05 19:35
operator 05:08 05:38 06:09 06:39 07:09 07:39 08:09 17:09 17:39 18:09 18:39 19:09 19:39
apologized 05:09 05:39 06:11 06:41 07:11 07:41 08:11 17:11 17:41 18:11 18:41 19:11 19:41
cooperate 05:16 05:46 06:21 06:51 07:21 07:51 08:21 17:21 17:51 18:21 18:51 19:21 19:51
4
stop mon-fri
Min:15
government computers 04:32 05:07 05:37 06:03 06:33 07:03 07:26 08:03 18:33 19:03 19:33
WASHINGTON 04:34 05:09 05:39 06:05 06:35 07:05 07:28 08:05 18:35 19:05 19:35
suspected 04:38 05:13 05:43 06:11 06:41 07:11 07:34 08:11 18:41 19:11 19:41
Chinese 04:43 05:18 05:48 06:15 06:45 07:15 07:24 07:38 08:15 18:45 19:15 19:45
hackers  04:50 05:25 05:55 06:25 06:55 07:25 07:34 07:48 08:25 18:55 19:25 19:55

代码:

private static void refill_time_table(String path, String key) {
    int five = 5;
    File file = new File(path + File.separator + key);

    StringBuilder sb = new StringBuilder(key);
    sb.deleteCharAt(5);
    String resultString = sb.toString();

    String str = Integer.toString(five);
    String newName = resultString.substring(0, 5) + str
            + resultString.substring(5, resultString.length());
    tempArray.add(newName);
    try (PrintWriter writer = new PrintWriter(path + File.separator
            + newName);

            Scanner scanner = new Scanner(file)) {
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (!line.startsWith("Min:")) {

                writer.println(line);
                //writer.flush();
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

最佳答案

将所有行添加到集合中,跟踪这些标记的索引并交换列表元素:

    List<String> l = new ArrayList<String>();

    try {

        Scanner sc = new Scanner(new File("D:\\temp\\dd.txt"));

        String TokenA = "stop mon-fri", TokenB = "Min";
        int indexA = 0, indexB = 0;

        while (sc.hasNextLine()) {
            String temp = sc.nextLine();
            l.add(temp);

            if (temp.equals(TokenA))
                indexA = l.size();
            else if (temp.contains(TokenB)) {
                indexB = l.size() - 1;

                while (indexB > indexA)
                    Collections.swap(l, indexB, --indexB);
            }
        }

        sc.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

//在此处添加代码 - 将 ArrayList 按原样打印到文件

关于java - 通过查找特定行将其写在文本文档中的特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30682659/

相关文章:

java - 我可以在 Java 中的线程中运行线程吗?

c# - 在C#和Java中计算CRC32校验和有什么区别吗?

java - 可选的异常捕获

java - Java 中 Stack 的 EmptyStackException 错误

java - Spring REST - RequestBody 内的数组仅包含一个元素

java - java代码性能分析工具

java - 托管属性从 jsf 1.2 迁移到 2.1

启动tomcat加载servlet时出现java.lang.ClassCastException

java - Java怎么知道一个类可以是一个数组呢?

java - OSGI 环境中的 Spring 应用程序