Java 把线放在前面的位置

Java 把线放在前面的位置,java,Java,在文本文档中找到这一行Min:30或Min:15后,我尝试执行以下操作。我想在新文本文档中的这一行stop mon-fri后写入,或者如果可能,在同一文本文档中写入,然后删除找到Min:行的空换行。在当前状态下,线路正在更改,而不是像简单结果中那样定位。我怎样才能修好它 谢谢你的帮助 简单: 4 stop mon-fri Chinese death Radbruch-Platz operator Min:30 apologized cooperate 4 stop mon-fri go

在文本文档中找到这一行
Min:30
Min:15
后,我尝试执行以下操作。我想在新文本文档中的这一行
stop 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  
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();
}
结果应如下所示

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

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  
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();
}
try(PrintWriter-writer=newprintwriter(path+File.separator+newName);
扫描仪=新扫描仪(文件)){
ArrayList store=新的ArrayList();
int indexA=0,indexB=0;
while(scanner.hasNextLine()){
字符串行=scanner.nextLine();
存储。添加(行);
if(行开始带(“停止”)){
indexA=store.size();
}else if(行startsWith(“Min:)){
indexB=store.size()-1;
集合。交换(存储、indexA、indexB);
}
}
for(字符串元素:存储){
writer.println(元素);
}
scanner.close();
}捕获(例外e){
e、 printStackTrace();
}

由于您读取整个文件只是为了在执行修改后将其写出来,因此我只使用
Files.readAllLines()
将所有信息放入
列表
,而不是
扫描仪

一旦您的数据在
列表中
,只需对其进行迭代,就可以应用与查找感兴趣行的索引相同的条件检查

一旦您有了索引,就不用使用
集合.swap()
了,我会使用
.set()
您的
列表提供的,然后只需执行删除操作,删除您不想保留在数据中的行

修改完
列表后,您可以执行
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);
}

你指的是哪个分号?这是新的try块,如果我想使用这个新的try符号,我必须在那里使用分号。