java - 将线放在之前的位置

标签 java

在文本文档中找到此行 Min:30Min:15 后,我尝试执行以下操作,我想将此行写入此行 在新文本文档中停止 mon-fri ,或者如果可能的话在同一文本文档中停止,然后删除找到 Min: 行的空换行符。在当前状态下,该行正在被更改,并且不像结果中那样简单定位。我该如何修复它?

感谢任何帮助。

简单:

4
stop mon-fri
Chinese 
death Radbruch-Platz 
operator 
Min:30
apologized 
cooperate 
4
stop mon-fri
government computers 
WASHINGTON 
suspected 
Min:15
Chinese 
hackers  

结果应如下所示

4
stop mon-fri
Min:30
dominant 2
death Radbruch-Platz 
operator 
apologized 
cooperate 
4
stop mon-fri
Min:15
government computers
WASHINGTON 
suspected 
Chinese 
hackers 

代码:

try (PrintWriter writer = new PrintWriter(path + File.separator + newName);
     Scanner scanner = new Scanner(file)) {

    ArrayList<String> store = new ArrayList<String>();
    int indexA = 0, indexB = 0;

    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        store.add(line);
        if (line.startsWith("stop")) {
            indexA = store.size() ;
        } else if (line.startsWith("Min:")) {
            indexB = store.size() - 1;
            Collections.swap(store, indexA, indexB);
        }
    }
    for (String element : store) {
        writer.println(element);
    }
    scanner.close();
} catch (Exception e) {
    e.printStackTrace();
}

最佳答案

由于您读取整个文件只是为了在执行修改后将其写回,因此我将仅使用 Files.readAllLines()将所有信息放入 List<String>而不是Scanner .

将数据放入 List<String> 后,只需迭代它,您就可以应用与查找您感兴趣的行的索引相同的条件检查。

一旦有了索引,就不要使用 Collections.swap()我会使用 .set()你的List<String>提供,然后只需执行删除操作即可删除您不想保留在数据中的行。

完成修改 List<String> 后,你可以这样做Files.write()将其写回同一个文件。

代码示例:

public static void main(String[] args) throws Exception {
    List<String> myFileLines = Files.readAllLines(Paths.get("MyFile.txt"));

    // Printing the file before modifications for test purposes
    System.out.println("Before:");
    myFileLines.stream().forEach(line -> System.out.println(line));

    int stopMonFriIndex = -1;
    int minIndex = -1;
    for (int i = 0; i < myFileLines.size(); i++) {
        if (myFileLines.get(i).startsWith("stop")) {
            stopMonFriIndex = i;
        } else if (myFileLines.get(i).startsWith("Min:")) {
            minIndex = i;
        } else if (stopMonFriIndex > -1 && minIndex > -1) {
            // Set the Min: line after the stop line
            myFileLines.set(stopMonFriIndex + 1, myFileLines.get(minIndex));
            // Remove the Min: line
            myFileLines.remove(minIndex);
            // Reset indexes
            stopMonFriIndex = -1;
            minIndex = -1;
        }
    }

    // Print the file after modifications for test purposes
    System.out.println("\r\nAfter:");
    myFileLines.stream().forEach(line -> System.out.println(line));

    // Write the data back to the same file
    Files.write(Paths.get("MyFile.txt"), myFileLines);
}

结果(来自显示):

Before:
4
stop mon-fri
Chinese 
death Radbruch-Platz 
operator 
Min:30
apologized 
cooperate 
4
stop mon-fri
government computers 
WASHINGTON 
suspected 
Min:15
Chinese 
hackers 

After:
4
stop mon-fri
Min:30
death Radbruch-Platz 
operator 
apologized 
cooperate 
4
stop mon-fri
Min:15
WASHINGTON 
suspected 
Chinese 
hackers

关于java - 将线放在之前的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30687459/

相关文章:

java - 当 git push 到远程仓库时出现错误 "File java_pid66619.hprof is 661.61 MB; this exceeds GitHub' s 文件大小限制为 100.00 MB"

java - 如何使用现有基类对象构造派生类

java - 如何中断或停止当前正在运行的 quartz 作业?

java - 即使我的主题需要白色文本,如何使用白色文本颜色停止 Facebook SDK 对话框

java - 如何调用同一个JAVA类的返回值

java - m2eclipse 依赖版本不是最新的

java - 如何在 Solr 中获取最后的索引记录?

java - 使用字符串(文本)传递泛型类型(反射?)

java - 抽象类的简单Q迭代器<Object E>

java - 为 9999 年 12 月 31 日(UTC)创建 java.sql.Date 的最简单方法?