Java 通过查找点状线,将其写在文本文档中的点状位置

Java 通过查找点状线,将其写在文本文档中的点状位置,java,Java,在文本文档中找到这一行后,我正在尝试执行以下操作:30,我想在新文本文档中这一行之后写入它,或者如果在同一文本文档中可能,则删除空的换行符 谢谢你的帮助 简单: 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:0

在文本文档中找到这一行后,我正在尝试执行以下操作:30,我想在新文本文档中这一行之后写入它,或者如果在同一文本文档中可能,则删除空的换行符

谢谢你的帮助

简单:

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

}
结果应如下所示

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
代码:

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

}

您应该考虑使用
队列来重新组织行。逐行读取
队列
。找到
Stop mon-fri
并搜索下一个
Min:
时停止。复制它,然后将其从原始文件中删除,以便继续将行馈送到
队列


更新:您应该只删除原始文本中
Min:
第一个案例。每次阅读
stop mon fri
时,还应将
队列的内容附加到新文件中,将所有行添加到集合中,跟踪这些令牌的索引和交换列表元素:

    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();
    }
listl=newarraylist();
试一试{
Scanner sc=新扫描仪(新文件(“D:\\temp\\dd.txt”);
字符串TokenA=“停止周一至周五”,TokenB=“分钟”;
int indexA=0,indexB=0;
while(sc.hasNextLine()){
字符串温度=sc.nextLine();
l、 添加(临时);
如果(温度等于(令牌A))
indexA=l.尺寸();
否则如果(临时包含(令牌B)){
indexB=l.size()-1;
while(indexB>indexA)
集合交换(l,indexB,--indexB);
}
}
sc.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}

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

您在哪里遇到问题?我发现思考一下如果它们是物理对象,您将如何处理这些问题很有帮助。如果每一行都是一张纸条,你会如何重新排列它们?您现有的代码将删除所有
Min:
行,但这不是您想要的。@stvcisco:问题是,我不知道如何使用扫描仪返回特定行
stop:mon-fri
当我找到该行时,
Min:
?您不能轻松地“返回”。取而代之的是,在浏览列表时查找它,一旦找到它,就停止写行,直到找到“Min:”。因此,不要写出接下来的几行,而是将它们存储在临时位置(一个
Vector
或一个
StringBuffer
),然后当你找到“Min:”行时,写出它,然后转储这些临时行并继续。我是否可以在每次读取
stop
时转储队列,因为数字4并不总是相同的,它会随着文件的不同而变化?@TheBook Yes,你可以。我认为这是对的,但事实并非如此,因为这些行正在交换和排序,就像结果简单一样。@我在发布解决方案后的某个时候纠正了交换逻辑。
while(indexB>indexA)Collections.swap(l,indexB,--indexB)现在检查它。